Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -195,9 +195,11 @@ print ReceiptCode::of('PO-20250312-0001')->number;
print ReceiptCode::of('PO-20250312-0001')->nextCode();
//=> PO-20250312-0002

print ReceiptCode::make()->nextCode();
print ReceiptCode::of()->nextCode();
//=> PO-[Today's ymd]-0001

print ReceiptCode::of(prefix: 'CT')->nextCode();
//=> CT-[Today's ymd]-0001
```

## Formatting
Expand Down
35 changes: 13 additions & 22 deletions src/ReceiptCode.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@

class ReceiptCode implements Stringable
{
/**
* Prefix of the receipt code
*/
public string $prefix = 'PO';

/**
* Year month day of the receipt code
*/
Expand All @@ -33,7 +28,11 @@ class ReceiptCode implements Stringable
* @param string|null $code ReceiptCode
*/
private function __construct(
?string $code = null,
/**
* Prefix of the receipt code
*/
public string $prefix,
?string $code = null
) {
if (! is_null($code)) {
$this->code($code);
Expand Down Expand Up @@ -62,9 +61,10 @@ public function code(string $code): static
*
* @return string Next code
*
* @example ReceiptCode::make()->nextCode() => PO-20250312-0001
* @example ReceiptCode::make()->code('PO-20250312-0001')->nextCode() => PO-20250312-0002
* @example ReceiptCode::make()->code('PO-20250312-9999')->nextCode() => PO-20250312-10000
* @example ReceiptCode::of()->nextCode() => PO-20250312-0001
* @example ReceiptCode::of('PO-20250312-0001')->nextCode() => PO-20250312-0002
* @example ReceiptCode::of('PO-20250312-9999')->nextCode() => PO-20250312-10000
* @example ReceiptCode::of('PO-20250312-9999', prefix: 'CT')->nextCode() => CT-20250312-10000
*/
public function nextCode(): string
{
Expand All @@ -82,22 +82,13 @@ public function nextCode(): string
/**
* Create a new instance of ReceiptCode.
*
* @param string|null $code ReceiptCode
* @return static Provides a new instance of ReceiptCode
*/
public static function of(?string $code = null): self
{
return is_null($code) ? self::make() : new self($code);
}

/**
* Create a new instance of ReceiptCode.
*
* @param string|null $code receipt code
* @param string|null $prefix prefix of the receipt code, default is 'PO'
* @return static Provides a new instance of ReceiptCode
*/
public static function make(): self
public static function of(?string $code = null, ?string $prefix = 'PO'): self
{
return new self;
return is_null($code) ? new self(prefix: $prefix) : new self($prefix, $code);
}

/**
Expand Down
19 changes: 11 additions & 8 deletions tests/ReceiptCodeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,25 @@ public function test_of_method()

public function test_code_method()
{
$this->assertEquals('PO-20250312-0001', ReceiptCode::make()->code('PO-20250312-0001')->code);
$this->assertEquals('PO', ReceiptCode::make()->code('PO-20250312-0001')->prefix);
$this->assertEquals('20250312', ReceiptCode::make()->code('PO-20250312-0001')->ymd);
$this->assertEquals('0001', ReceiptCode::make()->code('PO-20250312-0001')->number);
$this->assertEquals('PO-20250312-0001', ReceiptCode::of('PO-20250312-0001')->code);
$this->assertEquals('PO', ReceiptCode::of('PO-20250312-0001')->prefix);
$this->assertEquals('20250312', ReceiptCode::of('PO-20250312-0001')->ymd);
$this->assertEquals('0001', ReceiptCode::of('PO-20250312-0001')->number);
}

public function test_next_code_method()
{
$this->assertEquals('PO-20250312-0002', ReceiptCode::make()->code('PO-20250312-0001')->nextCode());
$this->assertEquals('PO-20250312-10000', ReceiptCode::make()->code('PO-20250312-9999')->nextCode());
$this->assertEquals('PO-20250312-0002', ReceiptCode::of('PO-20250312-0001')->nextCode());
$this->assertEquals('PO-20250312-10000', ReceiptCode::of('PO-20250312-9999')->nextCode());
}

public function test_no_code()
{
$this->assertEquals('PO-'.date('Ymd').'-0001', ReceiptCode::make()->nextCode());

$this->assertEquals('PO-'.date('Ymd').'-0001', ReceiptCode::of()->nextCode());
}

public function test_prefix()
{
$this->assertEquals('CT', ReceiptCode::of(prefix: 'CT')->prefix);
}
}