| 1 |
<?php |
| 2 |
|
| 3 |
declare(strict_types=1); |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
namespace Carbon\Traits; |
| 15 |
|
| 16 |
use Carbon\Carbon; |
| 17 |
use Carbon\CarbonImmutable; |
| 18 |
use Carbon\CarbonInterface; |
| 19 |
use Carbon\CarbonInterval; |
| 20 |
use Carbon\CarbonPeriod; |
| 21 |
use Carbon\CarbonPeriodImmutable; |
| 22 |
use Carbon\Exceptions\UnitException; |
| 23 |
use Closure; |
| 24 |
use DateTime; |
| 25 |
use DateTimeImmutable; |
| 26 |
use DateTimeInterface; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
trait Converter |
| 39 |
{ |
| 40 |
use ToStringFormat; |
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public function format(string $format): string |
| 48 |
{ |
| 49 |
$function = $this->localFormatFunction |
| 50 |
?? $this->getFactory()->getSettings()['formatFunction'] |
| 51 |
?? static::$formatFunction; |
| 52 |
|
| 53 |
if (!$function) { |
| 54 |
return $this->rawFormat($format); |
| 55 |
} |
| 56 |
|
| 57 |
if (\is_string($function) && method_exists($this, $function)) { |
| 58 |
$function = [$this, $function]; |
| 59 |
} |
| 60 |
|
| 61 |
return $function(...\func_get_args()); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function rawFormat(string $format): string |
| 68 |
{ |
| 69 |
return parent::format($format); |
| 70 |
} |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
public function __toString(): string |
| 81 |
{ |
| 82 |
$format = $this->localToStringFormat |
| 83 |
?? $this->getFactory()->getSettings()['toStringFormat'] |
| 84 |
?? null; |
| 85 |
|
| 86 |
return $format instanceof Closure |
| 87 |
? $format($this) |
| 88 |
: $this->rawFormat($format ?: ( |
| 89 |
\defined('static::DEFAULT_TO_STRING_FORMAT') |
| 90 |
? static::DEFAULT_TO_STRING_FORMAT |
| 91 |
: CarbonInterface::DEFAULT_TO_STRING_FORMAT |
| 92 |
)); |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public function toDateString(): string |
| 104 |
{ |
| 105 |
return $this->rawFormat('Y-m-d'); |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
public function toFormattedDateString(): string |
| 117 |
{ |
| 118 |
return $this->rawFormat('M j, Y'); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
public function toFormattedDayDateString(): string |
| 130 |
{ |
| 131 |
return $this->rawFormat('D, M j, Y'); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public function toTimeString(string $unitPrecision = 'second'): string |
| 143 |
{ |
| 144 |
return $this->rawFormat(static::getTimeFormatByPrecision($unitPrecision)); |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public function toDateTimeString(string $unitPrecision = 'second'): string |
| 156 |
{ |
| 157 |
return $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision)); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public static function getTimeFormatByPrecision(string $unitPrecision): string |
| 166 |
{ |
| 167 |
return match (static::singularUnit($unitPrecision)) { |
| 168 |
'minute' => 'H:i', |
| 169 |
'second' => 'H:i:s', |
| 170 |
'm', 'millisecond' => 'H:i:s.v', |
| 171 |
'ยต', 'microsecond' => 'H:i:s.u', |
| 172 |
default => throw new UnitException('Precision unit expected among: minute, second, millisecond and microsecond.'), |
| 173 |
}; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
public function toDateTimeLocalString(string $unitPrecision = 'second'): string |
| 187 |
{ |
| 188 |
return $this->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision)); |
| 189 |
} |
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
public function toDayDateTimeString(): string |
| 200 |
{ |
| 201 |
return $this->rawFormat('D, M j, Y g:i A'); |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
public function toAtomString(): string |
| 213 |
{ |
| 214 |
return $this->rawFormat(DateTime::ATOM); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
public function toCookieString(): string |
| 226 |
{ |
| 227 |
return $this->rawFormat(DateTimeInterface::COOKIE); |
| 228 |
} |
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public function toIso8601String(): string |
| 239 |
{ |
| 240 |
return $this->toAtomString(); |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public function toRfc822String(): string |
| 252 |
{ |
| 253 |
return $this->rawFormat(DateTimeInterface::RFC822); |
| 254 |
} |
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
public function toIso8601ZuluString(string $unitPrecision = 'second'): string |
| 265 |
{ |
| 266 |
return $this->avoidMutation() |
| 267 |
->utc() |
| 268 |
->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision).'\Z'); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
public function toRfc850String(): string |
| 280 |
{ |
| 281 |
return $this->rawFormat(DateTimeInterface::RFC850); |
| 282 |
} |
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
public function toRfc1036String(): string |
| 293 |
{ |
| 294 |
return $this->rawFormat(DateTimeInterface::RFC1036); |
| 295 |
} |
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public function toRfc1123String(): string |
| 306 |
{ |
| 307 |
return $this->rawFormat(DateTimeInterface::RFC1123); |
| 308 |
} |
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
public function toRfc2822String(): string |
| 319 |
{ |
| 320 |
return $this->rawFormat(DateTimeInterface::RFC2822); |
| 321 |
} |
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
public function toRfc3339String(bool $extended = false): string |
| 333 |
{ |
| 334 |
return $this->rawFormat($extended ? DateTimeInterface::RFC3339_EXTENDED : DateTimeInterface::RFC3339); |
| 335 |
} |
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
public function toRssString(): string |
| 346 |
{ |
| 347 |
return $this->rawFormat(DateTimeInterface::RSS); |
| 348 |
} |
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
public function toW3cString(): string |
| 359 |
{ |
| 360 |
return $this->rawFormat(DateTimeInterface::W3C); |
| 361 |
} |
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
public function toRfc7231String(): string |
| 372 |
{ |
| 373 |
return $this->avoidMutation() |
| 374 |
->setTimezone('GMT') |
| 375 |
->rawFormat(\defined('static::RFC7231_FORMAT') ? static::RFC7231_FORMAT : CarbonInterface::RFC7231_FORMAT); |
| 376 |
} |
| 377 |
|
| 378 |
|
| 379 |
|
| 380 |
|
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
public function toArray(): array |
| 387 |
{ |
| 388 |
return [ |
| 389 |
'year' => $this->year, |
| 390 |
'month' => $this->month, |
| 391 |
'day' => $this->day, |
| 392 |
'dayOfWeek' => $this->dayOfWeek, |
| 393 |
'dayOfYear' => $this->dayOfYear, |
| 394 |
'hour' => $this->hour, |
| 395 |
'minute' => $this->minute, |
| 396 |
'second' => $this->second, |
| 397 |
'micro' => $this->micro, |
| 398 |
'timestamp' => $this->timestamp, |
| 399 |
'formatted' => $this->rawFormat(\defined('static::DEFAULT_TO_STRING_FORMAT') ? static::DEFAULT_TO_STRING_FORMAT : CarbonInterface::DEFAULT_TO_STRING_FORMAT), |
| 400 |
'timezone' => $this->timezone, |
| 401 |
]; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
public function toObject(): object |
| 413 |
{ |
| 414 |
return (object) $this->toArray(); |
| 415 |
} |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
public function toString(): string |
| 426 |
{ |
| 427 |
return $this->avoidMutation()->locale('en')->isoFormat('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ'); |
| 428 |
} |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
public function toISOString(bool $keepOffset = false): ?string |
| 443 |
{ |
| 444 |
if (!$this->isValid()) { |
| 445 |
return null; |
| 446 |
} |
| 447 |
|
| 448 |
$yearFormat = $this->year < 0 || $this->year > 9999 ? 'YYYYYY' : 'YYYY'; |
| 449 |
$timezoneFormat = $keepOffset ? 'Z' : '[Z]'; |
| 450 |
$date = $keepOffset ? $this : $this->avoidMutation()->utc(); |
| 451 |
|
| 452 |
return $date->isoFormat("$yearFormat-MM-DD[T]HH:mm:ss.SSSSSS$timezoneFormat"); |
| 453 |
} |
| 454 |
|
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
|
| 461 |
|
| 462 |
|
| 463 |
public function toJSON(): ?string |
| 464 |
{ |
| 465 |
return $this->toISOString(); |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
|
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
public function toDateTime(): DateTime |
| 477 |
{ |
| 478 |
return DateTime::createFromFormat('U.u', $this->rawFormat('U.u')) |
| 479 |
->setTimezone($this->getTimezone()); |
| 480 |
} |
| 481 |
|
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
public function toDateTimeImmutable(): DateTimeImmutable |
| 491 |
{ |
| 492 |
return DateTimeImmutable::createFromFormat('U.u', $this->rawFormat('U.u')) |
| 493 |
->setTimezone($this->getTimezone()); |
| 494 |
} |
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
public function toDate(): DateTime |
| 507 |
{ |
| 508 |
return $this->toDateTime(); |
| 509 |
} |
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
|
| 518 |
public function toPeriod($end = null, $interval = null, $unit = null): CarbonPeriod |
| 519 |
{ |
| 520 |
if ($unit) { |
| 521 |
$interval = CarbonInterval::make("$interval ".static::pluralUnit($unit)); |
| 522 |
} |
| 523 |
|
| 524 |
$isDefaultInterval = !$interval; |
| 525 |
$interval ??= CarbonInterval::day(); |
| 526 |
$class = $this->isMutable() ? CarbonPeriod::class : CarbonPeriodImmutable::class; |
| 527 |
|
| 528 |
if (\is_int($end) || (\is_string($end) && ctype_digit($end))) { |
| 529 |
$end = (int) $end; |
| 530 |
} |
| 531 |
|
| 532 |
$end ??= 1; |
| 533 |
|
| 534 |
if (!\is_int($end)) { |
| 535 |
$end = $this->resolveCarbon($end); |
| 536 |
} |
| 537 |
|
| 538 |
return new $class( |
| 539 |
raw: [$this, CarbonInterval::make($interval), $end], |
| 540 |
dateClass: static::class, |
| 541 |
isDefaultInterval: $isDefaultInterval, |
| 542 |
); |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
public function range($end = null, $interval = null, $unit = null): CarbonPeriod |
| 553 |
{ |
| 554 |
return $this->toPeriod($end, $interval, $unit); |
| 555 |
} |
| 556 |
} |