Skip to content

Commit

Permalink
Fixed the Timehash order number generator Carbon 3 compatibility issue
Browse files Browse the repository at this point in the history
  • Loading branch information
fulopattila122 committed Mar 13, 2024
1 parent 79fc7a1 commit 3237040
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/Order/Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
- Changed minimal Enum requirement to v4.2
- Upgraded to Konekt Address and User modules to v3
- Added the `currency` field to the orders table
- Added Carbon 3 compatibility
- BC: Added the `$hooks` and `$itemHooks` parameters to the `OrderFactory` interface
- BC: Changed the `OrderItem` interface into Configurable
- BC: Added 7 methods to the `OrderItem` interface
Expand Down
4 changes: 2 additions & 2 deletions src/Order/Generators/TimeHashGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public function generateNumber(Order $order = null): string
$number = sprintf(
'%s-%s-%s%s%s',
$this->getYearAndDayHash($date),
str_pad(base_convert((string) $date->secondsSinceMidnight(), 10, 36), 4, '0', STR_PAD_LEFT),
str_pad(base_convert((string) (int) $date->secondsSinceMidnight(), 10, 36), 4, '0', STR_PAD_LEFT),
str_pad(base_convert((string) $this->getRapidMicroNumber(), 10, 36), 2, '0', STR_PAD_LEFT),
base_convert((string) random_int(0, 35), 10, 36), //always one char
((string) $this->getSlowMicroNumber())[0]
Expand All @@ -129,7 +129,7 @@ public function generateNumber(Order $order = null): string
*/
protected function getYearAndDayHash(Carbon $date)
{
return str_pad(base_convert((string) $date->diffInDays($this->startBaseDate), 10, 36), 3, '0', STR_PAD_LEFT);
return str_pad(base_convert((string) (int) $date->diffInDays($this->startBaseDate, true), 10, 36), 3, '0', STR_PAD_LEFT);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/Order/Tests/TimeHashTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@

class TimeHashTest extends TestCase
{
/** @var TimeHashGenerator */
protected $generator;
protected TimeHashGenerator $generator;

public function setUp(): void
{
Expand Down Expand Up @@ -133,7 +132,9 @@ public function length_is_13_in_edge_cases_as_well($date)
Carbon::setTestNow($date);

for ($i = 0; $i < 10; $i++) {
$this->assertEquals(13, strlen($this->generator->generateNumber()));
$number = $this->generator->generateNumber();
$len = strlen($number);
$this->assertEquals(13, $len, sprintf('The generated id `%s` should be 13 char long but it is %d', $number, $len));
}
}

Expand Down

0 comments on commit 3237040

Please sign in to comment.