66class 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