|
1 | 1 | <?php |
2 | | - |
3 | | - |
4 | 2 | namespace spaf\simputils\models; |
5 | 3 |
|
6 | 4 | use DateTimeInterface; |
|
14 | 12 | use spaf\simputils\traits\ForOutputsTrait; |
15 | 13 | use function date_interval_create_from_date_string; |
16 | 14 | use function is_null; |
| 15 | +use function is_string; |
17 | 16 | use function json_encode; |
18 | 17 | use function trim; |
19 | 18 |
|
|
32 | 31 | * @property-read \spaf\simputils\models\Date|string $date Date part (stringifiable object) |
33 | 32 | * |
34 | 33 | * @property-read \spaf\simputils\models\Time|string $time Time part |
35 | | - * @property-read \spaf\simputils\models\DateTimeZone $tz |
| 34 | + * @property \spaf\simputils\models\DateTimeZone|string $tz Timezone change |
36 | 35 | * |
37 | 36 | * @property int $week ISO 8601 week number of year, weeks starting on Monday |
38 | 37 | * @property-read int $doy The day of the year (starting from 0) |
@@ -148,13 +147,22 @@ public function getTimezone(): DateTimeZone|false { |
148 | 147 | return new DateTimeZone(parent::getTimezone()->getName()); |
149 | 148 | } |
150 | 149 |
|
| 150 | + /** |
| 151 | + * @param \spaf\simputils\models\DateTimeZone|string $timezone Time zone |
| 152 | + * |
| 153 | + * @return static |
| 154 | + */ |
151 | 155 | #[Property('tz')] |
152 | 156 | #[\ReturnTypeWillChange] |
153 | | - public function setTimezone($timezone) { |
| 157 | + public function setTimezone($timezone): static { |
154 | 158 | // IMP This method is original native PHP method, and it expects to return something, |
155 | 159 | // And when it's used as a method it works exactly the same, but in case of |
156 | 160 | // property - it will be used ONLY for setting, without returning anything. |
157 | 161 | // This is why return-type signature has no definition! |
| 162 | + $class_tz = PHP::redef(DateTimeZone::class); |
| 163 | + if (is_string($timezone)) { |
| 164 | + $timezone = new $class_tz($timezone); |
| 165 | + } |
158 | 166 | return parent::setTimezone($timezone); |
159 | 167 | } |
160 | 168 |
|
@@ -350,6 +358,18 @@ public function period( |
350 | 358 | return $left->walk($right, $step); |
351 | 359 | } |
352 | 360 |
|
| 361 | + /** |
| 362 | + * Shortcut for "format" |
| 363 | + * |
| 364 | + * @param string $format Output format |
| 365 | + * |
| 366 | + * @return string |
| 367 | + */ |
| 368 | + #[Shortcut('static::format()')] |
| 369 | + function f($format) { |
| 370 | + return $this->format($format); |
| 371 | + } |
| 372 | + |
353 | 373 | public function toJson(?bool $pretty = null, bool $with_class = false): string { |
354 | 374 | // TODO Implement optional choice of "for_*" |
355 | 375 | return json_encode($this->for_system); |
|
0 commit comments