genkgo /
camt
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Genkgo\Camt\Decoder; |
||
| 6 | |||
| 7 | use DateTimeImmutable; |
||
| 8 | use InvalidArgumentException; |
||
| 9 | |||
| 10 | class Date implements DateDecoderInterface |
||
| 11 | { |
||
| 12 | private ?string $format = null; |
||
| 13 | |||
| 14 | public function decode(string $date): DateTimeImmutable |
||
| 15 | { |
||
| 16 | if ($this->format === null) { |
||
| 17 | 30 | $result = new DateTimeImmutable($date); |
|
| 18 | } else { |
||
| 19 | 30 | $result = DateTimeImmutable::createFromFormat($this->format, $date); |
|
| 20 | 30 | } |
|
| 21 | |||
| 22 | if ($result === false) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 23 | throw new InvalidArgumentException("Cannot decode date {$date}"); |
||
| 24 | } |
||
| 25 | 30 | ||
| 26 | return $result; |
||
| 27 | } |
||
| 28 | |||
| 29 | 30 | public static function fromFormat(string $format): self |
|
| 30 | { |
||
| 31 | $decoder = new self(); |
||
| 32 | $decoder->format = $format; |
||
| 33 | |||
| 34 | return $decoder; |
||
| 35 | } |
||
| 36 | } |
||
| 37 |