Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
77.78% |
14 / 18 |
|
55.56% |
5 / 9 |
CRAP | |
0.00% |
0 / 1 |
| UnixTimestamp | |
77.78% |
14 / 18 |
|
55.56% |
5 / 9 |
12.33 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| toDate | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| fromNative | |
88.89% |
8 / 9 |
|
0.00% |
0 / 1 |
3.01 | |||
| toNative | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromDateTimeObject | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createFromCurrentTime | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getDateFormat | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| jsonSerialize | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| __toString | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | namespace Apie\DateValueObjects; |
| 3 | |
| 4 | use Apie\Core\Exceptions\InvalidTypeException; |
| 5 | use Apie\Core\ValueObjects\Exceptions\InvalidStringForValueObjectException; |
| 6 | use Apie\Core\ValueObjects\Utils; |
| 7 | use Apie\DateValueObjects\Concerns\CanHaveDayIntervals; |
| 8 | use Apie\DateValueObjects\Concerns\CanHaveMonthIntervals; |
| 9 | use Apie\DateValueObjects\Concerns\CanHaveTimeIntervals; |
| 10 | use Apie\DateValueObjects\Concerns\CanHaveYearIntervals; |
| 11 | use Apie\DateValueObjects\Interfaces\WorksWithDays; |
| 12 | use Apie\DateValueObjects\Interfaces\WorksWithMonths; |
| 13 | use Apie\DateValueObjects\Interfaces\WorksWithTimeIntervals; |
| 14 | use Apie\DateValueObjects\Interfaces\WorksWithYears; |
| 15 | use DateTime; |
| 16 | use DateTimeImmutable; |
| 17 | use DateTimeInterface; |
| 18 | use DateTimeZone; |
| 19 | use ReflectionClass; |
| 20 | |
| 21 | /** |
| 22 | * Contains a Unix timestamp. |
| 23 | * |
| 24 | * Example '1654579007' |
| 25 | */ |
| 26 | final class UnixTimestamp implements WorksWithDays, WorksWithMonths, WorksWithYears, WorksWithTimeIntervals |
| 27 | { |
| 28 | use CanHaveDayIntervals; |
| 29 | use CanHaveMonthIntervals; |
| 30 | use CanHaveTimeIntervals; |
| 31 | use CanHaveYearIntervals; |
| 32 | |
| 33 | private DateTimeImmutable $date; |
| 34 | |
| 35 | private int $day; |
| 36 | |
| 37 | public function __construct(string|int $timestamp) |
| 38 | { |
| 39 | $this->date = new DateTimeImmutable("@$timestamp", new DateTimeZone('GMT')); |
| 40 | $this->day = (int) $this->date->format('d'); |
| 41 | } |
| 42 | |
| 43 | public function toDate(): DateTimeImmutable |
| 44 | { |
| 45 | return $this->date; |
| 46 | } |
| 47 | |
| 48 | public static function fromNative(mixed $input): self |
| 49 | { |
| 50 | if ($input instanceof DateTimeInterface) { |
| 51 | return new self($input->getTimestamp()); |
| 52 | } |
| 53 | try { |
| 54 | return new self(Utils::toInt($input)); |
| 55 | } catch (InvalidTypeException $invalidType) { |
| 56 | throw new InvalidStringForValueObjectException( |
| 57 | Utils::toString($input), |
| 58 | new ReflectionClass(__CLASS__), |
| 59 | $invalidType |
| 60 | ); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | public function toNative(): string |
| 65 | { |
| 66 | return (string) $this->date->getTimestamp(); |
| 67 | } |
| 68 | |
| 69 | public static function createFromDateTimeObject(DateTimeInterface $dateTime): self |
| 70 | { |
| 71 | return new self($dateTime->getTimestamp()); |
| 72 | } |
| 73 | |
| 74 | public static function createFromCurrentTime(): self |
| 75 | { |
| 76 | return self::createFromDateTimeObject(new DateTime()); |
| 77 | } |
| 78 | |
| 79 | public static function getDateFormat(): string |
| 80 | { |
| 81 | return 'U'; |
| 82 | } |
| 83 | |
| 84 | public function jsonSerialize(): string |
| 85 | { |
| 86 | return $this->toNative(); |
| 87 | } |
| 88 | |
| 89 | public function __toString(): string |
| 90 | { |
| 91 | return $this->toNative(); |
| 92 | } |
| 93 | } |