Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
CanCreateInstanceFromDateTimeObject
83.33% covered (warning)
83.33%
5 / 6
50.00% covered (danger)
50.00%
1 / 2
2.02
0.00% covered (danger)
0.00%
0 / 1
 getDateFormat
n/a
0 / 0
n/a
0 / 0
0
 createFromDateTimeObject
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 createFromCurrentTime
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2namespace Apie\DateValueObjects\Concerns;
3
4use Apie\Core\ApieLib;
5use Apie\Core\Attributes\CmsSingleInput;
6use Apie\Core\Dto\CmsInputOption;
7use DateTime;
8use DateTimeImmutable;
9use DateTimeInterface;
10use 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'))]
16trait 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}