From 36c6e53f073fa75c785ecb1aa5d6e7f58621c6b6 Mon Sep 17 00:00:00 2001 From: Sam Lee Date: Fri, 21 Feb 2025 17:24:37 +0900 Subject: [PATCH] Set `prefix` of receipt code changeable --- README.md | 4 +++- src/ReceiptCode.php | 35 +++++++++++++---------------------- tests/ReceiptCodeTest.php | 19 +++++++++++-------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index a75ac1e..d0c2a49 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/ReceiptCode.php b/src/ReceiptCode.php index 2de20ee..b3a9913 100644 --- a/src/ReceiptCode.php +++ b/src/ReceiptCode.php @@ -7,11 +7,6 @@ class ReceiptCode implements Stringable { - /** - * Prefix of the receipt code - */ - public string $prefix = 'PO'; - /** * Year month day of the receipt code */ @@ -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); @@ -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 { @@ -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); } /** diff --git a/tests/ReceiptCodeTest.php b/tests/ReceiptCodeTest.php index 499ee0a..6892bf5 100644 --- a/tests/ReceiptCodeTest.php +++ b/tests/ReceiptCodeTest.php @@ -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); + } }