Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| IsDateValueObject | |
100.00% |
12 / 12 |
|
100.00% |
2 / 2 |
5 | |
100.00% |
1 / 1 |
| convert | |
100.00% |
11 / 11 |
|
100.00% |
1 / 1 |
4 | |||
| toDate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| isStrictFormat | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| getDateFormat | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| 1 | <?php |
| 2 | namespace Apie\DateValueObjects\Concerns; |
| 3 | |
| 4 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 5 | use Apie\Core\ValueObjects\IsStringValueObject; |
| 6 | use DateTimeImmutable; |
| 7 | use ReflectionClass; |
| 8 | |
| 9 | /** |
| 10 | * Trait that can be used for value objects to convert a string into a Date value object in a |
| 11 | * certain format. |
| 12 | */ |
| 13 | trait IsDateValueObject |
| 14 | { |
| 15 | use IsStringValueObject; |
| 16 | |
| 17 | private DateTimeImmutable $date; |
| 18 | |
| 19 | private int $day; |
| 20 | |
| 21 | /** |
| 22 | * Parses the string into a proper format and stores it internally as a date object. |
| 23 | */ |
| 24 | protected function convert(string $input): string |
| 25 | { |
| 26 | $input = trim($input); |
| 27 | $format = self::getDateFormat(); |
| 28 | $date = DateTimeImmutable::createFromFormat($format, $input); |
| 29 | if (!$date instanceof DateTimeImmutable) { |
| 30 | throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__)); |
| 31 | } |
| 32 | $newInput = $date->format($format); |
| 33 | if ($this->isStrictFormat() && $newInput !== $input) { |
| 34 | throw new InvalidStringForValueObjectException($input, new ReflectionClass(__CLASS__)); |
| 35 | } |
| 36 | // these are stored specifically for switching months and remembering the current day. |
| 37 | $this->day = (int) $date->format('d'); |
| 38 | $this->date = $date; |
| 39 | return $newInput; |
| 40 | } |
| 41 | |
| 42 | public function toDate(): DateTimeImmutable |
| 43 | { |
| 44 | return $this->date; |
| 45 | } |
| 46 | |
| 47 | /** |
| 48 | * A class using this trait should tell the trait whether an extra check is required to see |
| 49 | * if the input format is exactly the same as what was given. |
| 50 | * |
| 51 | * If this returns false it means we tolerate input as '2012-12-32', while it throws an error |
| 52 | * if this method returns true. |
| 53 | * |
| 54 | * It also means that days and months also require a correct '0'. |
| 55 | */ |
| 56 | abstract protected function isStrictFormat(): bool; |
| 57 | |
| 58 | /** |
| 59 | * The string should be in the format of this date string. |
| 60 | * |
| 61 | * @see https://www.php.net/manual/en/datetime.format.php#refsect1-datetime.format-parameters |
| 62 | */ |
| 63 | abstract public static function getDateFormat(): string; |
| 64 | } |