|
| 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 | +} |
0 commit comments