Skip to content

Commit 5542a26

Browse files
authored
Merge pull request #47 from PcComponentes/feature/implements-time-funtions-on-vos
Override all date time factory functions
2 parents 04450b9 + 1c01be1 commit 5542a26

5 files changed

Lines changed: 212 additions & 29 deletions

File tree

docker-compose.yml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
version: '3.7'
2-
31
services:
42
php:
53
build: .

grumphp.yml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,8 @@ grumphp:
55
phpstan:
66
autoload_file: phpstan.neon
77
configuration: ~
8-
level: 2
8+
level: 3
99
jsonlint: ~
10-
phpcpd:
11-
exclude:
12-
- 'var'
13-
- 'vendor'
14-
min_lines: 60
1510
phpcs:
1611
standard:
1712
- 'phpcs.xml.dist'

src/Domain/Model/ValueObject/CollectionValueObject.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public function first()
9595
return $this->items[array_key_first($this->items)] ?? null;
9696
}
9797

98+
public function value(): array
99+
{
100+
return $this->items;
101+
}
102+
98103
protected function addItem($item): static
99104
{
100105
$items = $this->items;
@@ -109,9 +114,4 @@ protected function removeItem($item): static
109114
static fn ($current) => $current !== $item,
110115
);
111116
}
112-
113-
public function value(): array
114-
{
115-
return $this->items;
116-
}
117117
}

src/Domain/Model/ValueObject/DateTimeValueObject.php

Lines changed: 102 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,14 @@ class DateTimeValueObject extends \DateTimeImmutable implements ValueObject
77
{
88
private const TIME_ZONE = 'UTC';
99
private const TIME_FORMAT = 'Y-m-d\TH:i:s.uP';
10+
private const FORMAT = \DATE_ATOM;
1011

1112
final private function __construct($time, $timezone)
1213
{
1314
parent::__construct($time, $timezone);
1415
}
1516

16-
final public static function from(string $str): static
17+
public static function from(string $str): static
1718
{
1819
$timeZone = new \DateTimeZone(self::TIME_ZONE);
1920

@@ -25,16 +26,51 @@ final public static function now(): static
2526
return static::from('now');
2627
}
2728

28-
final public static function fromFormat(string $format, string $str): static
29+
final public static function createFromMutable(\DateTime $object): static
2930
{
30-
$dateTime = \DateTimeImmutable::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
31+
return static::from($object->format(self::TIME_FORMAT));
32+
}
33+
34+
final public static function createFromInterface(\DateTimeInterface $object): static
35+
{
36+
return static::from($object->format(self::TIME_FORMAT));
37+
}
38+
39+
final public static function createFromFormat(
40+
string $format,
41+
string $datetime,
42+
?\DateTimeZone $timezone = null
43+
): static | false {
44+
$datetime = parent::createFromFormat($format, $datetime, $timezone);
45+
46+
if (false === $datetime) {
47+
return false;
48+
}
3149

32-
return static::from($dateTime->format(self::TIME_FORMAT));
50+
$timeZone = new \DateTimeZone(self::TIME_ZONE);
51+
52+
return static::createFromInterface($datetime->setTimezone($timeZone));
3353
}
3454

35-
final public static function fromTimestamp(int $timestamp): static
55+
final public static function fromFormat(string $format, string $str): static | false
3656
{
37-
return self::fromFormat('U', (string) $timestamp);
57+
return static::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
58+
}
59+
60+
final public static function createFromTimestamp(float|int $timestamp): static
61+
{
62+
$dateTime = \is_int($timestamp)
63+
? self::fromFormat('U', (string) $timestamp)
64+
: self::fromFormat('U.u', \number_format($timestamp, 6, '.', ''));
65+
66+
\assert(false !== $dateTime, 'Unexpected error on create date time from timestamp');
67+
68+
return $dateTime;
69+
}
70+
71+
final public static function fromTimestamp(int|float $timestamp): static
72+
{
73+
return static::createFromTimestamp($timestamp);
3874
}
3975

4076
final public function jsonSerialize(): string
@@ -44,6 +80,65 @@ final public function jsonSerialize(): string
4480

4581
final public function value(): string
4682
{
47-
return $this->format(\DATE_ATOM);
83+
return $this->format(self::FORMAT);
84+
}
85+
86+
final public function modify(string $modifier): static | false
87+
{
88+
$dateTime = parent::modify($modifier);
89+
90+
if (false === $dateTime) {
91+
return false;
92+
}
93+
94+
return static::createFromInterface($dateTime);
95+
}
96+
97+
final public function add(\DateInterval $interval): static
98+
{
99+
return parent::add($interval);
100+
}
101+
102+
final public function setDate(int $year, int $month, int $day): static | false
103+
{
104+
$dateTime = parent::setDate($year, $month, $day);
105+
106+
if (false === $dateTime) {
107+
return false;
108+
}
109+
110+
return $dateTime;
111+
}
112+
113+
final public function setISODate(int $year, int $week, int $dayOfWeek = 1): static | false
114+
{
115+
$dateTime = parent::setISODate($year, $week, $dayOfWeek);
116+
117+
if (false === $dateTime) {
118+
return false;
119+
}
120+
121+
return $dateTime;
122+
}
123+
124+
final public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static | false
125+
{
126+
$dateTime = parent::setTime($hour, $minute, $second, $microsecond);
127+
128+
if (false === $dateTime) {
129+
return false;
130+
}
131+
132+
return $dateTime;
133+
}
134+
135+
final public function setTimestamp(int $timestamp): static
136+
{
137+
return parent::setTimestamp($timestamp);
138+
}
139+
140+
final public function setTimezone(\DateTimeZone $timezone): static
141+
{
142+
return parent::setTimezone($timezone);
48143
}
49144
}

src/Domain/Model/ValueObject/DateValueObject.php

Lines changed: 104 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,35 +6,71 @@
66
class DateValueObject extends \DateTimeImmutable implements ValueObject
77
{
88
private const TIME_ZONE = 'UTC';
9+
private const TIME_FORMAT = 'Y-m-d';
910
private const FORMAT = 'Y-m-d';
1011

11-
final private function __construct(string $time, \DateTimeZone $timezone)
12+
final private function __construct($time, $timezone)
1213
{
1314
parent::__construct($time, $timezone);
1415
}
1516

16-
final public static function from(string $str): static
17+
public static function from(string $str): static
1718
{
18-
return new static($str, new \DateTimeZone(self::TIME_ZONE));
19+
$timeZone = new \DateTimeZone(self::TIME_ZONE);
20+
21+
return (new static($str, $timeZone))->setTimezone($timeZone);
1922
}
2023

2124
final public static function now(): static
2225
{
2326
return static::from('now');
2427
}
2528

26-
final public static function fromFormat(string $format, string $str): static
29+
final public static function createFromMutable(\DateTime $object): static
30+
{
31+
return static::from($object->format(self::TIME_FORMAT));
32+
}
33+
34+
final public static function createFromInterface(\DateTimeInterface $object): static
35+
{
36+
return static::from($object->format(self::TIME_FORMAT));
37+
}
38+
39+
final public static function createFromFormat(
40+
string $format,
41+
string $datetime,
42+
?\DateTimeZone $timezone = null
43+
): static | false {
44+
$datetime = parent::createFromFormat($format, $datetime, $timezone);
45+
46+
if (false === $datetime) {
47+
return false;
48+
}
49+
50+
$timeZone = new \DateTimeZone(self::TIME_ZONE);
51+
52+
return static::createFromInterface($datetime->setTimezone($timeZone));
53+
}
54+
55+
final public static function fromFormat(string $format, string $str): static | false
56+
{
57+
return static::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
58+
}
59+
60+
final public static function createFromTimestamp(float|int $timestamp): static
2761
{
28-
$dateTime = \DateTimeImmutable::createFromFormat($format, $str, new \DateTimeZone(self::TIME_ZONE));
62+
$dateTime = \is_int($timestamp)
63+
? self::fromFormat('U', (string) $timestamp)
64+
: self::fromFormat('U.u', \number_format($timestamp, 6, '.', ''));
2965

30-
\assert($dateTime instanceof \DateTimeImmutable);
66+
\assert(false !== $dateTime, 'Unexpected error on create date time from timestamp');
3167

32-
return static::from($dateTime->format(self::FORMAT));
68+
return $dateTime;
3369
}
3470

35-
final public static function fromTimestamp(int $timestamp): static
71+
final public static function fromTimestamp(int|float $timestamp): static
3672
{
37-
return self::fromFormat('U', (string) $timestamp);
73+
return static::createFromTimestamp($timestamp);
3874
}
3975

4076
final public function jsonSerialize(): string
@@ -46,4 +82,63 @@ final public function value(): string
4682
{
4783
return $this->format(self::FORMAT);
4884
}
85+
86+
final public function modify(string $modifier): static | false
87+
{
88+
$dateTime = parent::modify($modifier);
89+
90+
if (false === $dateTime) {
91+
return false;
92+
}
93+
94+
return static::createFromInterface($dateTime);
95+
}
96+
97+
final public function add(\DateInterval $interval): static
98+
{
99+
return parent::add($interval);
100+
}
101+
102+
final public function setDate(int $year, int $month, int $day): static | false
103+
{
104+
$dateTime = parent::setDate($year, $month, $day);
105+
106+
if (false === $dateTime) {
107+
return false;
108+
}
109+
110+
return $dateTime;
111+
}
112+
113+
final public function setISODate(int $year, int $week, int $dayOfWeek = 1): static | false
114+
{
115+
$dateTime = parent::setISODate($year, $week, $dayOfWeek);
116+
117+
if (false === $dateTime) {
118+
return false;
119+
}
120+
121+
return $dateTime;
122+
}
123+
124+
final public function setTime(int $hour, int $minute, int $second = 0, int $microsecond = 0): static | false
125+
{
126+
$dateTime = parent::setTime($hour, $minute, $second, $microsecond);
127+
128+
if (false === $dateTime) {
129+
return false;
130+
}
131+
132+
return $dateTime;
133+
}
134+
135+
final public function setTimestamp(int $timestamp): static
136+
{
137+
return parent::setTimestamp($timestamp);
138+
}
139+
140+
final public function setTimezone(\DateTimeZone $timezone): static
141+
{
142+
return parent::setTimezone($timezone);
143+
}
49144
}

0 commit comments

Comments
 (0)