| Total Complexity | 9 |
| Total Lines | 57 |
| Duplicated Lines | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 1 |
| 1 | <?php |
||
| 13 | abstract class DateFormatValueObject implements DateFormatValueObjectInterface |
||
| 14 | { |
||
| 15 | private $value; |
||
| 16 | |||
| 17 | private $format; |
||
| 18 | |||
| 19 | /** |
||
| 20 | * @throws Exception if value is invalid |
||
| 21 | */ |
||
| 22 | public function __construct(?DateTimeImmutable $value, string $format = DateTime::ATOM) |
||
| 23 | { |
||
| 24 | try { |
||
| 25 | $this->guard($value); |
||
| 26 | } catch (Throwable $e) { |
||
| 27 | throw $this->throwException($value, $e); |
||
| 28 | } |
||
| 29 | $this->value = $value; |
||
| 30 | $this->format = $format; |
||
| 31 | } |
||
| 32 | |||
| 33 | public function getValue(): ?string |
||
| 34 | { |
||
| 35 | if ($this->value === null) { |
||
| 36 | return null; |
||
| 37 | } |
||
| 38 | return $this->value->format($this->format); |
||
| 39 | } |
||
| 40 | |||
| 41 | public function equals(DateFormatValueObjectInterface $other): bool |
||
| 57 | } |
||
| 58 | |||
| 59 | public function getDateTime(): ?DateTimeImmutable |
||
| 62 | } |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @throws Throwable if value is invalid |
||
| 66 | */ |
||
| 67 | abstract protected function guard(?DateTimeImmutable $value): void; |
||
| 68 | |||
| 69 | abstract protected function throwException(?DateTimeImmutable $value, Throwable $e): Exception; |
||
| 70 | } |
||
| 71 |