| 1: | <?php declare(strict_types=1); |
| 2: | |
| 3: | namespace Salient\Core\Date; |
| 4: | |
| 5: | use Salient\Contract\Core\DateParserInterface; |
| 6: | use DateTimeImmutable; |
| 7: | use DateTimeZone; |
| 8: | |
| 9: | |
| 10: | |
| 11: | |
| 12: | |
| 13: | |
| 14: | final class DateFormatParser implements DateParserInterface |
| 15: | { |
| 16: | private string $Format; |
| 17: | |
| 18: | |
| 19: | |
| 20: | |
| 21: | |
| 22: | |
| 23: | |
| 24: | |
| 25: | |
| 26: | public function __construct(string $format) |
| 27: | { |
| 28: | |
| 29: | |
| 30: | if (strcspn($format, '!|') === strlen($format)) { |
| 31: | $format .= '|'; |
| 32: | } |
| 33: | $this->Format = $format; |
| 34: | } |
| 35: | |
| 36: | |
| 37: | |
| 38: | |
| 39: | public function parse(string $value, ?DateTimeZone $timezone = null): ?DateTimeImmutable |
| 40: | { |
| 41: | $date = DateTimeImmutable::createFromFormat($this->Format, $value, $timezone); |
| 42: | if ($date === false) { |
| 43: | return null; |
| 44: | } |
| 45: | return $date; |
| 46: | } |
| 47: | } |
| 48: | |