Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
DateString
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
2 / 2
2
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Support\Models;
6
7use DateTime;
8use Stringable;
9
10/**
11 * Parse a date string and create normalized formats.
12 */
13class DateString implements Stringable
14{
15    /** Date format constants */
16    final public const DATETIME_FORMAT = 'c';
17    final public const SENTENCE_FORMAT = 'l M jS, Y, \a\t g:ia';
18    final public const SHORT_FORMAT = 'M jS, Y';
19
20    /** The original date string. */
21    public readonly string $string;
22
23    /** The parsed date object. */
24    public readonly DateTime $dateTimeObject;
25
26    /** The machine-readable datetime string. */
27    public readonly string $datetime;
28
29    /** The human-readable sentence string. */
30    public readonly string $sentence;
31
32    /** Shorter version of the sentence string. */
33    public readonly string $short;
34
35    public function __construct(string $string)
36    {
37        $this->string = $string;
38        $this->dateTimeObject = new DateTime($this->string);
39
40        $this->datetime = $this->dateTimeObject->format(self::DATETIME_FORMAT);
41        $this->sentence = $this->dateTimeObject->format(self::SENTENCE_FORMAT);
42        $this->short = $this->dateTimeObject->format(self::SHORT_FORMAT);
43    }
44
45    public function __toString(): string
46    {
47        return $this->short;
48    }
49}