Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| DateString | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
7 / 7 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Hyde\Framework\Models; |
| 4 | |
| 5 | use DateTime; |
| 6 | use Hyde\Framework\Exceptions\CouldNotParseDateStringException; |
| 7 | |
| 8 | /** |
| 9 | * Parse a date string and create normalized formats. |
| 10 | * |
| 11 | * @see \Tests\Unit\DateStringTest |
| 12 | */ |
| 13 | class DateString |
| 14 | { |
| 15 | /** The original date string. */ |
| 16 | public string $string; |
| 17 | |
| 18 | /** The parsed date object. */ |
| 19 | public DateTime $dateTimeObject; |
| 20 | |
| 21 | /** The machine-readable datetime string. */ |
| 22 | public string $datetime; |
| 23 | |
| 24 | /** The human-readable sentence string. */ |
| 25 | public string $sentence; |
| 26 | |
| 27 | /** Shorter version of the sentence string. */ |
| 28 | public string $short; |
| 29 | |
| 30 | /** |
| 31 | * @param string $string |
| 32 | * |
| 33 | * @throws \Hyde\Framework\Exceptions\CouldNotParseDateStringException |
| 34 | */ |
| 35 | public function __construct(string $string) |
| 36 | { |
| 37 | $this->string = $string; |
| 38 | |
| 39 | try { |
| 40 | $this->dateTimeObject = new DateTime($this->string); |
| 41 | } catch (\Exception $e) { |
| 42 | throw new CouldNotParseDateStringException($e->getMessage()); |
| 43 | } |
| 44 | |
| 45 | $this->datetime = $this->dateTimeObject->format('c'); |
| 46 | $this->sentence = $this->dateTimeObject->format('l M jS, Y, \a\t g:ia'); |
| 47 | $this->short = $this->dateTimeObject->format('M jS, Y'); |
| 48 | } |
| 49 | } |