Skip to content

Commit 277e7da

Browse files
committed
Add receipt code classes
1 parent f107f53 commit 277e7da

3 files changed

Lines changed: 173 additions & 0 deletions

File tree

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ We have provided the API Documentation on the web. For more information, please
2121
- [x] Set good code parser
2222
- [x] Complex good code parser
2323
- [x] Option Good code parser(No code, it matched by name)
24+
- [x] Receipt code parser
2425

2526
## Install
2627

@@ -172,6 +173,33 @@ print SetGood::ofArray(['43' => 3, '253' => 3])->code();
172173
//=> SET43x3zz253x3
173174
```
174175

176+
### Receipt Code
177+
178+
```php
179+
<?php
180+
181+
use Cable8mm\GoodCode\ReceiptCode;
182+
183+
print ReceiptCode::of('PO-20250312-0001')->code;
184+
//=> PO-20250312-0001
185+
186+
print ReceiptCode::of('PO-20250312-0001')->prefix;
187+
//=> PO
188+
189+
print ReceiptCode::of('PO-20250312-0001')->ymd;
190+
//=> 20250312
191+
192+
print ReceiptCode::of('PO-20250312-0001')->number;
193+
//=> 0001
194+
195+
print ReceiptCode::of('PO-20250312-0001')->nextCode();
196+
//=> PO-20250312-0002
197+
198+
print ReceiptCode::make()->nextCode();
199+
//=> PO-[Today's ymd]-0001
200+
201+
```
202+
175203
## Formatting
176204

177205
```sh

src/ReceiptCode.php

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
<?php
2+
3+
namespace Cable8mm\GoodCode;
4+
5+
use InvalidArgumentException;
6+
use Stringable;
7+
8+
class ReceiptCode implements Stringable
9+
{
10+
/**
11+
* Prefix of the receipt code
12+
*/
13+
public string $prefix = 'PO';
14+
15+
/**
16+
* Year month day of the receipt code
17+
*/
18+
public string $ymd;
19+
20+
/**
21+
* Number of the receipt code
22+
*/
23+
public int $number;
24+
25+
/**
26+
* Receipt code
27+
*/
28+
public string $code;
29+
30+
/**
31+
* ReceiptCode constructor.
32+
*
33+
* @param string|null $code ReceiptCode
34+
*/
35+
private function __construct(
36+
?string $code = null,
37+
) {
38+
if (! is_null($code)) {
39+
$this->code($code);
40+
}
41+
}
42+
43+
/**
44+
* Parse code and set `code`, `prefix`, `ymd` and `number`.
45+
*
46+
* @param string $code ReceiptCode
47+
* @return static Provides a fluent interface
48+
*/
49+
public function code(string $code): static
50+
{
51+
if (preg_match('/^([^\-]+)\-([^\-]+)\-(.+)$/', $code, $matches) === false) {
52+
throw new InvalidArgumentException('Invalid code format.');
53+
}
54+
55+
[$this->code, $this->prefix, $this->ymd, $this->number] = $matches;
56+
57+
return $this;
58+
}
59+
60+
/**
61+
* Get next code.
62+
*
63+
* @return string Next code
64+
*
65+
* @example ReceiptCode::make()->nextCode() => PO-20250312-0001
66+
* @example ReceiptCode::make()->code('PO-20250312-0001')->nextCode() => PO-20250312-0002
67+
* @example ReceiptCode::make()->code('PO-20250312-9999')->nextCode() => PO-20250312-10000
68+
*/
69+
public function nextCode(): string
70+
{
71+
if (! isset($this->code)) {
72+
return $this->prefix.'-'.date('Ymd').'-0001';
73+
}
74+
75+
if ($this->ymd === $this->ymd) {
76+
return $this->prefix.'-'.$this->ymd.'-'.str_pad($this->number + 1, 4, '0', STR_PAD_LEFT);
77+
}
78+
79+
return $this->prefix.'-'.date('Ymd').'-0001';
80+
}
81+
82+
/**
83+
* Create a new instance of ReceiptCode.
84+
*
85+
* @param string $code ReceiptCode
86+
* @return static Provides a new instance of ReceiptCode
87+
*/
88+
public static function of(string $code): self
89+
{
90+
return new self($code);
91+
}
92+
93+
/**
94+
* Create a new instance of ReceiptCode.
95+
*
96+
* @return static Provides a new instance of ReceiptCode
97+
*/
98+
public static function make(): self
99+
{
100+
return new self;
101+
}
102+
103+
/**
104+
* Get the string representation of the object.
105+
*
106+
* @return string The method returns the `code` representation
107+
*/
108+
public function __toString(): string
109+
{
110+
return $this->code ?? '';
111+
}
112+
}

tests/ReceiptCodeTest.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace Cable8mm\GoodCode\Tests;
4+
5+
use Cable8mm\GoodCode\ReceiptCode;
6+
use PHPUnit\Framework\TestCase;
7+
8+
class ReceiptCodeTest extends TestCase
9+
{
10+
public function test_of_method()
11+
{
12+
$this->assertEquals('PO-20250312-0001', ReceiptCode::of('PO-20250312-0001')->code);
13+
}
14+
15+
public function test_code_method()
16+
{
17+
$this->assertEquals('PO-20250312-0001', ReceiptCode::make()->code('PO-20250312-0001')->code);
18+
$this->assertEquals('PO', ReceiptCode::make()->code('PO-20250312-0001')->prefix);
19+
$this->assertEquals('20250312', ReceiptCode::make()->code('PO-20250312-0001')->ymd);
20+
$this->assertEquals('0001', ReceiptCode::make()->code('PO-20250312-0001')->number);
21+
}
22+
23+
public function test_next_code_method()
24+
{
25+
$this->assertEquals('PO-20250312-0002', ReceiptCode::make()->code('PO-20250312-0001')->nextCode());
26+
$this->assertEquals('PO-20250312-10000', ReceiptCode::make()->code('PO-20250312-9999')->nextCode());
27+
}
28+
29+
public function test_no_code()
30+
{
31+
$this->assertEquals('PO-'.date('Ymd').'-0001', ReceiptCode::make()->nextCode());
32+
}
33+
}

0 commit comments

Comments
 (0)