Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
83.33% |
5 / 6 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
| CanCreateInstanceFromDateTimeObject | |
83.33% |
5 / 6 |
|
50.00% |
1 / 2 |
2.02 | |
0.00% |
0 / 1 |
| getDateFormat | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| createFromDateTimeObject | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| createFromCurrentTime | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\DateValueObjects\Concerns; |
| 3 | |
| 4 | use Apie\Core\ApieLib; |
| 5 | use Apie\Core\Attributes\CmsSingleInput; |
| 6 | use Apie\Core\Dto\CmsInputOption; |
| 7 | use DateTime; |
| 8 | use DateTimeImmutable; |
| 9 | use DateTimeInterface; |
| 10 | use ReflectionClass; |
| 11 | |
| 12 | /** |
| 13 | * Adds method to date value objects to convert a PHP Datetime object in the value object. |
| 14 | */ |
| 15 | #[CmsSingleInput(['datetime'], new CmsInputOption(dateFormatMethod: 'getDateFormat'))] |
| 16 | trait CanCreateInstanceFromDateTimeObject |
| 17 | { |
| 18 | /** |
| 19 | * @see IsDateValueObject |
| 20 | */ |
| 21 | abstract public static function getDateFormat(): string; |
| 22 | |
| 23 | public static function createFromDateTimeObject(DateTimeInterface $dateTime): self |
| 24 | { |
| 25 | $refl = new ReflectionClass(__CLASS__); |
| 26 | $instance = $refl->newInstanceWithoutConstructor(); |
| 27 | $instance->date = DateTimeImmutable::createFromInterface($dateTime); |
| 28 | $instance->internal = $instance->date->format(self::getDateFormat()); |
| 29 | return $instance; |
| 30 | } |
| 31 | |
| 32 | public static function createFromCurrentTime(): self |
| 33 | { |
| 34 | return self::createFromDateTimeObject(ApieLib::getPsrClock()->now()); |
| 35 | } |
| 36 | } |