| Total Complexity | 118 |
| Total Lines | 869 |
| Duplicated Lines | 0 % |
| Changes | 0 | ||
Complex classes like Creator often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use Creator, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 34 | trait Creator |
||
| 35 | { |
||
| 36 | use ObjectInitialisation; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * The errors that can occur. |
||
| 40 | * |
||
| 41 | * @var array |
||
| 42 | */ |
||
| 43 | protected static $lastErrors; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new Carbon instance. |
||
| 47 | * |
||
| 48 | * Please see the testing aids section (specifically static::setTestNow()) |
||
| 49 | * for more on the possibility of this constructor returning a test instance. |
||
| 50 | * |
||
| 51 | * @param string|null $time |
||
| 52 | * @param DateTimeZone|string|null $tz |
||
| 53 | * |
||
| 54 | * @throws InvalidFormatException |
||
| 55 | */ |
||
| 56 | public function __construct($time = null, $tz = null) |
||
| 57 | { |
||
| 58 | if ($time instanceof DateTimeInterface) { |
||
| 59 | $time = $this->constructTimezoneFromDateTime($time, $tz)->format('Y-m-d H:i:s.u'); |
||
| 60 | } |
||
| 61 | |||
| 62 | if (is_int($time)) { |
||
| 63 | $time = "@$time"; |
||
| 64 | } |
||
| 65 | |||
| 66 | // If the class has a test now set and we are trying to create a now() |
||
| 67 | // instance then override as required |
||
| 68 | $isNow = empty($time) || $time === 'now'; |
||
| 69 | |||
| 70 | if (method_exists(static::class, 'hasTestNow') && |
||
| 71 | method_exists(static::class, 'getTestNow') && |
||
| 72 | static::hasTestNow() && |
||
| 73 | ($isNow || static::hasRelativeKeywords($time)) |
||
| 74 | ) { |
||
| 75 | static::mockConstructorParameters($time, $tz); |
||
| 76 | } |
||
| 77 | |||
| 78 | // Work-around for PHP bug https://bugs.php.net/bug.php?id=67127 |
||
| 79 | if (strpos((string) .1, '.') === false) { |
||
| 80 | $locale = setlocale(LC_NUMERIC, '0'); |
||
| 81 | setlocale(LC_NUMERIC, 'C'); |
||
| 82 | } |
||
| 83 | |||
| 84 | try { |
||
| 85 | parent::__construct($time ?: 'now', static::safeCreateDateTimeZone($tz) ?: null); |
||
| 86 | } catch (Exception $exception) { |
||
| 87 | throw new InvalidFormatException($exception->getMessage(), 0, $exception); |
||
| 88 | } |
||
| 89 | |||
| 90 | $this->constructedObjectId = spl_object_hash($this); |
||
| 91 | |||
| 92 | if (isset($locale)) { |
||
| 93 | setlocale(LC_NUMERIC, $locale); |
||
| 94 | } |
||
| 95 | |||
| 96 | static::setLastErrors(parent::getLastErrors()); |
||
| 97 | } |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Get timezone from a datetime instance. |
||
| 101 | * |
||
| 102 | * @param DateTimeInterface $date |
||
| 103 | * @param DateTimeZone|string|null $tz |
||
| 104 | * |
||
| 105 | * @return DateTimeInterface |
||
| 106 | */ |
||
| 107 | private function constructTimezoneFromDateTime(DateTimeInterface $date, &$tz) |
||
| 108 | { |
||
| 109 | if ($tz !== null) { |
||
| 110 | $safeTz = static::safeCreateDateTimeZone($tz); |
||
| 111 | |||
| 112 | if ($safeTz) { |
||
| 113 | return $date->setTimezone($safeTz); |
||
| 114 | } |
||
| 115 | |||
| 116 | return $date; |
||
| 117 | } |
||
| 118 | |||
| 119 | $tz = $date->getTimezone(); |
||
| 120 | |||
| 121 | return $date; |
||
| 122 | } |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Update constructedObjectId on cloned. |
||
| 126 | */ |
||
| 127 | public function __clone() |
||
| 128 | { |
||
| 129 | $this->constructedObjectId = spl_object_hash($this); |
||
| 130 | } |
||
| 131 | |||
| 132 | /** |
||
| 133 | * Create a Carbon instance from a DateTime one. |
||
| 134 | * |
||
| 135 | * @param DateTimeInterface $date |
||
| 136 | * |
||
| 137 | * @return static |
||
| 138 | */ |
||
| 139 | public static function instance($date) |
||
| 140 | { |
||
| 141 | if ($date instanceof static) { |
||
| 142 | return clone $date; |
||
| 143 | } |
||
| 144 | |||
| 145 | static::expectDateTime($date); |
||
| 146 | |||
| 147 | $instance = new static($date->format('Y-m-d H:i:s.u'), $date->getTimezone()); |
||
| 148 | |||
| 149 | if ($date instanceof CarbonInterface || $date instanceof Options) { |
||
| 150 | $settings = $date->getSettings(); |
||
| 151 | |||
| 152 | if (!$date->hasLocalTranslator()) { |
||
| 153 | unset($settings['locale']); |
||
| 154 | } |
||
| 155 | |||
| 156 | $instance->settings($settings); |
||
| 157 | } |
||
| 158 | |||
| 159 | return $instance; |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create a carbon instance from a string. |
||
| 164 | * |
||
| 165 | * This is an alias for the constructor that allows better fluent syntax |
||
| 166 | * as it allows you to do Carbon::parse('Monday next week')->fn() rather |
||
| 167 | * than (new Carbon('Monday next week'))->fn(). |
||
| 168 | * |
||
| 169 | * @param string|DateTimeInterface|null $time |
||
| 170 | * @param DateTimeZone|string|null $tz |
||
| 171 | * |
||
| 172 | * @throws InvalidFormatException |
||
| 173 | * |
||
| 174 | * @return static |
||
| 175 | */ |
||
| 176 | public static function rawParse($time = null, $tz = null) |
||
| 177 | { |
||
| 178 | if ($time instanceof DateTimeInterface) { |
||
| 179 | return static::instance($time); |
||
| 180 | } |
||
| 181 | |||
| 182 | try { |
||
| 183 | return new static($time, $tz); |
||
| 184 | } catch (Exception $exception) { |
||
| 185 | $date = @static::now($tz)->change($time); |
||
| 186 | |||
| 187 | if (!$date) { |
||
| 188 | throw new InvalidFormatException("Could not parse '$time': ".$exception->getMessage(), 0, $exception); |
||
| 189 | } |
||
| 190 | |||
| 191 | return $date; |
||
| 192 | } |
||
| 193 | } |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Create a carbon instance from a string. |
||
| 197 | * |
||
| 198 | * This is an alias for the constructor that allows better fluent syntax |
||
| 199 | * as it allows you to do Carbon::parse('Monday next week')->fn() rather |
||
| 200 | * than (new Carbon('Monday next week'))->fn(). |
||
| 201 | * |
||
| 202 | * @param string|DateTimeInterface|null $time |
||
| 203 | * @param DateTimeZone|string|null $tz |
||
| 204 | * |
||
| 205 | * @throws InvalidFormatException |
||
| 206 | * |
||
| 207 | * @return static |
||
| 208 | */ |
||
| 209 | public static function parse($time = null, $tz = null) |
||
| 210 | { |
||
| 211 | $function = static::$parseFunction; |
||
| 212 | |||
| 213 | if (!$function) { |
||
| 214 | return static::rawParse($time, $tz); |
||
| 215 | } |
||
| 216 | |||
| 217 | if (is_string($function) && method_exists(static::class, $function)) { |
||
| 218 | $function = [static::class, $function]; |
||
| 219 | } |
||
| 220 | |||
| 221 | return $function(...func_get_args()); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). |
||
| 226 | * |
||
| 227 | * @param string $time date/time string in the given language (may also contain English). |
||
| 228 | * @param string|null $locale if locale is null or not specified, current global locale will be |
||
| 229 | * used instead. |
||
| 230 | * @param DateTimeZone|string|null $tz optional timezone for the new instance. |
||
| 231 | * |
||
| 232 | * @throws InvalidFormatException |
||
| 233 | * |
||
| 234 | * @return static |
||
| 235 | */ |
||
| 236 | public static function parseFromLocale($time, $locale = null, $tz = null) |
||
| 237 | { |
||
| 238 | return static::rawParse(static::translateTimeString($time, $locale, 'en'), $tz); |
||
| 239 | } |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Get a Carbon instance for the current date and time. |
||
| 243 | * |
||
| 244 | * @param DateTimeZone|string|null $tz |
||
| 245 | * |
||
| 246 | * @return static |
||
| 247 | */ |
||
| 248 | public static function now($tz = null) |
||
| 249 | { |
||
| 250 | return new static(null, $tz); |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Create a Carbon instance for today. |
||
| 255 | * |
||
| 256 | * @param DateTimeZone|string|null $tz |
||
| 257 | * |
||
| 258 | * @return static |
||
| 259 | */ |
||
| 260 | public static function today($tz = null) |
||
| 261 | { |
||
| 262 | return static::rawParse('today', $tz); |
||
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Create a Carbon instance for tomorrow. |
||
| 267 | * |
||
| 268 | * @param DateTimeZone|string|null $tz |
||
| 269 | * |
||
| 270 | * @return static |
||
| 271 | */ |
||
| 272 | public static function tomorrow($tz = null) |
||
| 273 | { |
||
| 274 | return static::rawParse('tomorrow', $tz); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Create a Carbon instance for yesterday. |
||
| 279 | * |
||
| 280 | * @param DateTimeZone|string|null $tz |
||
| 281 | * |
||
| 282 | * @return static |
||
| 283 | */ |
||
| 284 | public static function yesterday($tz = null) |
||
| 285 | { |
||
| 286 | return static::rawParse('yesterday', $tz); |
||
| 287 | } |
||
| 288 | |||
| 289 | /** |
||
| 290 | * Create a Carbon instance for the greatest supported date. |
||
| 291 | * |
||
| 292 | * @return static |
||
| 293 | */ |
||
| 294 | public static function maxValue() |
||
| 295 | { |
||
| 296 | if (self::$PHPIntSize === 4) { |
||
| 297 | // 32 bit |
||
| 298 | return static::createFromTimestamp(PHP_INT_MAX); // @codeCoverageIgnore |
||
| 299 | } |
||
| 300 | |||
| 301 | // 64 bit |
||
| 302 | return static::create(9999, 12, 31, 23, 59, 59); |
||
| 303 | } |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Create a Carbon instance for the lowest supported date. |
||
| 307 | * |
||
| 308 | * @return static |
||
| 309 | */ |
||
| 310 | public static function minValue() |
||
| 311 | { |
||
| 312 | if (self::$PHPIntSize === 4) { |
||
| 313 | // 32 bit |
||
| 314 | return static::createFromTimestamp(~PHP_INT_MAX); // @codeCoverageIgnore |
||
| 315 | } |
||
| 316 | |||
| 317 | // 64 bit |
||
| 318 | return static::create(1, 1, 1, 0, 0, 0); |
||
| 319 | } |
||
| 320 | |||
| 321 | private static function assertBetween($unit, $value, $min, $max) |
||
| 322 | { |
||
| 323 | if (static::isStrictModeEnabled() && ($value < $min || $value > $max)) { |
||
| 324 | throw new OutOfRangeException($unit, $min, $max, $value); |
||
| 325 | } |
||
| 326 | } |
||
| 327 | |||
| 328 | private static function createNowInstance($tz) |
||
| 329 | { |
||
| 330 | if (!static::hasTestNow()) { |
||
| 331 | return static::now($tz); |
||
| 332 | } |
||
| 333 | |||
| 334 | $now = static::getTestNow(); |
||
| 335 | |||
| 336 | if ($now instanceof Closure) { |
||
| 337 | return $now(static::now($tz)); |
||
| 338 | } |
||
| 339 | |||
| 340 | return $now; |
||
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Create a new Carbon instance from a specific date and time. |
||
| 345 | * |
||
| 346 | * If any of $year, $month or $day are set to null their now() values will |
||
| 347 | * be used. |
||
| 348 | * |
||
| 349 | * If $hour is null it will be set to its now() value and the default |
||
| 350 | * values for $minute and $second will be their now() values. |
||
| 351 | * |
||
| 352 | * If $hour is not null then the default values for $minute and $second |
||
| 353 | * will be 0. |
||
| 354 | * |
||
| 355 | * @param int|null $year |
||
| 356 | * @param int|null $month |
||
| 357 | * @param int|null $day |
||
| 358 | * @param int|null $hour |
||
| 359 | * @param int|null $minute |
||
| 360 | * @param int|null $second |
||
| 361 | * @param DateTimeZone|string|null $tz |
||
| 362 | * |
||
| 363 | * @throws InvalidFormatException |
||
| 364 | * |
||
| 365 | * @return static|false |
||
| 366 | */ |
||
| 367 | public static function create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) |
||
| 368 | { |
||
| 369 | if (is_string($year) && !is_numeric($year)) { |
||
| 370 | return static::parse($year, $tz ?: (is_string($month) || $month instanceof DateTimeZone ? $month : null)); |
||
| 371 | } |
||
| 372 | |||
| 373 | $defaults = null; |
||
| 374 | $getDefault = function ($unit) use ($tz, &$defaults) { |
||
| 375 | if ($defaults === null) { |
||
| 376 | $now = self::createNowInstance($tz); |
||
| 377 | |||
| 378 | $defaults = array_combine([ |
||
| 379 | 'year', |
||
| 380 | 'month', |
||
| 381 | 'day', |
||
| 382 | 'hour', |
||
| 383 | 'minute', |
||
| 384 | 'second', |
||
| 385 | ], explode('-', $now->rawFormat('Y-n-j-G-i-s.u'))); |
||
| 386 | } |
||
| 387 | |||
| 388 | return $defaults[$unit]; |
||
| 389 | }; |
||
| 390 | |||
| 391 | $year = $year === null ? $getDefault('year') : $year; |
||
| 392 | $month = $month === null ? $getDefault('month') : $month; |
||
| 393 | $day = $day === null ? $getDefault('day') : $day; |
||
| 394 | $hour = $hour === null ? $getDefault('hour') : $hour; |
||
| 395 | $minute = $minute === null ? $getDefault('minute') : $minute; |
||
| 396 | $second = (float) ($second === null ? $getDefault('second') : $second); |
||
| 397 | |||
| 398 | self::assertBetween('month', $month, 0, 99); |
||
| 399 | self::assertBetween('day', $day, 0, 99); |
||
| 400 | self::assertBetween('hour', $hour, 0, 99); |
||
| 401 | self::assertBetween('minute', $minute, 0, 99); |
||
| 402 | self::assertBetween('second', $second, 0, 99); |
||
| 403 | |||
| 404 | $fixYear = null; |
||
| 405 | |||
| 406 | if ($year < 0) { |
||
| 407 | $fixYear = $year; |
||
| 408 | $year = 0; |
||
| 409 | } elseif ($year > 9999) { |
||
| 410 | $fixYear = $year - 9999; |
||
| 411 | $year = 9999; |
||
| 412 | } |
||
| 413 | |||
| 414 | $second = ($second < 10 ? '0' : '').number_format($second, 6); |
||
| 415 | $instance = static::rawCreateFromFormat('!Y-n-j G:i:s.u', sprintf('%s-%s-%s %s:%02s:%02s', $year, $month, $day, $hour, $minute, $second), $tz); |
||
| 416 | |||
| 417 | if ($fixYear !== null) { |
||
| 418 | $instance = $instance->addYears($fixYear); |
||
| 419 | } |
||
| 420 | |||
| 421 | return $instance; |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Create a new safe Carbon instance from a specific date and time. |
||
| 426 | * |
||
| 427 | * If any of $year, $month or $day are set to null their now() values will |
||
| 428 | * be used. |
||
| 429 | * |
||
| 430 | * If $hour is null it will be set to its now() value and the default |
||
| 431 | * values for $minute and $second will be their now() values. |
||
| 432 | * |
||
| 433 | * If $hour is not null then the default values for $minute and $second |
||
| 434 | * will be 0. |
||
| 435 | * |
||
| 436 | * If one of the set values is not valid, an InvalidDateException |
||
| 437 | * will be thrown. |
||
| 438 | * |
||
| 439 | * @param int|null $year |
||
| 440 | * @param int|null $month |
||
| 441 | * @param int|null $day |
||
| 442 | * @param int|null $hour |
||
| 443 | * @param int|null $minute |
||
| 444 | * @param int|null $second |
||
| 445 | * @param DateTimeZone|string|null $tz |
||
| 446 | * |
||
| 447 | * @throws InvalidDateException |
||
| 448 | * |
||
| 449 | * @return static|false |
||
| 450 | */ |
||
| 451 | public static function createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) |
||
| 452 | { |
||
| 453 | $fields = static::getRangesByUnit(); |
||
| 454 | |||
| 455 | foreach ($fields as $field => $range) { |
||
| 456 | if ($$field !== null && (!is_int($$field) || $$field < $range[0] || $$field > $range[1])) { |
||
| 457 | if (static::isStrictModeEnabled()) { |
||
| 458 | throw new InvalidDateException($field, $$field); |
||
| 459 | } |
||
| 460 | |||
| 461 | return false; |
||
| 462 | } |
||
| 463 | } |
||
| 464 | |||
| 465 | $instance = static::create($year, $month, $day, $hour, $minute, $second, $tz); |
||
| 466 | |||
| 467 | foreach (array_reverse($fields) as $field => $range) { |
||
| 468 | if ($$field !== null && (!is_int($$field) || $$field !== $instance->$field)) { |
||
| 469 | if (static::isStrictModeEnabled()) { |
||
| 470 | throw new InvalidDateException($field, $$field); |
||
| 471 | } |
||
| 472 | |||
| 473 | return false; |
||
| 474 | } |
||
| 475 | } |
||
| 476 | |||
| 477 | return $instance; |
||
| 478 | } |
||
| 479 | |||
| 480 | /** |
||
| 481 | * Create a Carbon instance from just a date. The time portion is set to now. |
||
| 482 | * |
||
| 483 | * @param int|null $year |
||
| 484 | * @param int|null $month |
||
| 485 | * @param int|null $day |
||
| 486 | * @param DateTimeZone|string|null $tz |
||
| 487 | * |
||
| 488 | * @throws InvalidFormatException |
||
| 489 | * |
||
| 490 | * @return static |
||
| 491 | */ |
||
| 492 | public static function createFromDate($year = null, $month = null, $day = null, $tz = null) |
||
| 493 | { |
||
| 494 | return static::create($year, $month, $day, null, null, null, $tz); |
||
| 495 | } |
||
| 496 | |||
| 497 | /** |
||
| 498 | * Create a Carbon instance from just a date. The time portion is set to midnight. |
||
| 499 | * |
||
| 500 | * @param int|null $year |
||
| 501 | * @param int|null $month |
||
| 502 | * @param int|null $day |
||
| 503 | * @param DateTimeZone|string|null $tz |
||
| 504 | * |
||
| 505 | * @throws InvalidFormatException |
||
| 506 | * |
||
| 507 | * @return static |
||
| 508 | */ |
||
| 509 | public static function createMidnightDate($year = null, $month = null, $day = null, $tz = null) |
||
| 510 | { |
||
| 511 | return static::create($year, $month, $day, 0, 0, 0, $tz); |
||
| 512 | } |
||
| 513 | |||
| 514 | /** |
||
| 515 | * Create a Carbon instance from just a time. The date portion is set to today. |
||
| 516 | * |
||
| 517 | * @param int|null $hour |
||
| 518 | * @param int|null $minute |
||
| 519 | * @param int|null $second |
||
| 520 | * @param DateTimeZone|string|null $tz |
||
| 521 | * |
||
| 522 | * @throws InvalidFormatException |
||
| 523 | * |
||
| 524 | * @return static |
||
| 525 | */ |
||
| 526 | public static function createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) |
||
| 527 | { |
||
| 528 | return static::create(null, null, null, $hour, $minute, $second, $tz); |
||
| 529 | } |
||
| 530 | |||
| 531 | /** |
||
| 532 | * Create a Carbon instance from a time string. The date portion is set to today. |
||
| 533 | * |
||
| 534 | * @param string $time |
||
| 535 | * @param DateTimeZone|string|null $tz |
||
| 536 | * |
||
| 537 | * @throws InvalidFormatException |
||
| 538 | * |
||
| 539 | * @return static |
||
| 540 | */ |
||
| 541 | public static function createFromTimeString($time, $tz = null) |
||
| 542 | { |
||
| 543 | return static::today($tz)->setTimeFromTimeString($time); |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * @param string $format Datetime format |
||
| 548 | * @param string $time |
||
| 549 | * @param DateTimeZone|string|false|null $originalTz |
||
| 550 | * |
||
| 551 | * @return DateTimeInterface|false |
||
| 552 | */ |
||
| 553 | private static function createFromFormatAndTimezone($format, $time, $originalTz) |
||
| 554 | { |
||
| 555 | // Work-around for https://bugs.php.net/bug.php?id=75577 |
||
| 556 | // @codeCoverageIgnoreStart |
||
| 557 | if (version_compare(PHP_VERSION, '7.3.0-dev', '<')) { |
||
| 558 | $format = str_replace('.v', '.u', $format); |
||
| 559 | } |
||
| 560 | // @codeCoverageIgnoreEnd |
||
| 561 | |||
| 562 | if ($originalTz === null) { |
||
| 563 | return parent::createFromFormat($format, "$time"); |
||
| 564 | } |
||
| 565 | |||
| 566 | $tz = is_int($originalTz) |
||
| 567 | ? @timezone_name_from_abbr('', (int) ($originalTz * 3600), 1) |
||
| 568 | : $originalTz; |
||
| 569 | |||
| 570 | $tz = static::safeCreateDateTimeZone($tz, $originalTz); |
||
| 571 | |||
| 572 | if ($tz === false) { |
||
| 573 | return false; |
||
| 574 | } |
||
| 575 | |||
| 576 | return parent::createFromFormat($format, "$time", $tz); |
||
| 577 | } |
||
| 578 | |||
| 579 | /** |
||
| 580 | * Create a Carbon instance from a specific format. |
||
| 581 | * |
||
| 582 | * @param string $format Datetime format |
||
| 583 | * @param string $time |
||
| 584 | * @param DateTimeZone|string|false|null $tz |
||
| 585 | * |
||
| 586 | * @throws InvalidFormatException |
||
| 587 | * |
||
| 588 | * @return static|false |
||
| 589 | */ |
||
| 590 | public static function rawCreateFromFormat($format, $time, $tz = null) |
||
| 591 | { |
||
| 592 | if (preg_match('/(?<!\\\\)(?:\\\\{2})*(a|A)/', $format, $aMatches, PREG_OFFSET_CAPTURE) && |
||
| 593 | preg_match('/(?<!\\\\)(?:\\\\{2})*(h|g|H|G)/', $format, $hMatches, PREG_OFFSET_CAPTURE) && |
||
| 594 | $aMatches[1][1] < $hMatches[1][1] && |
||
| 595 | preg_match('/(am|pm|AM|PM)/', $time) |
||
| 596 | ) { |
||
| 597 | $format = preg_replace('/^(.*)(?<!\\\\)((?:\\\\{2})*)(a|A)(.*)$/U', '$1$2$4 $3', $format); |
||
| 598 | $time = preg_replace('/^(.*)(am|pm|AM|PM)(.*)$/U', '$1$3 $2', $time); |
||
| 599 | } |
||
| 600 | |||
| 601 | // First attempt to create an instance, so that error messages are based on the unmodified format. |
||
| 602 | $date = self::createFromFormatAndTimezone($format, $time, $tz); |
||
| 603 | $lastErrors = parent::getLastErrors(); |
||
| 604 | /** @var \Carbon\CarbonImmutable|\Carbon\Carbon|null $mock */ |
||
| 605 | $mock = static::getMockedTestNow($tz); |
||
| 606 | |||
| 607 | if ($mock && $date instanceof DateTimeInterface) { |
||
| 608 | // Set timezone from mock if custom timezone was neither given directly nor as a part of format. |
||
| 609 | // First let's skip the part that will be ignored by the parser. |
||
| 610 | $nonEscaped = '(?<!\\\\)(\\\\{2})*'; |
||
| 611 | |||
| 612 | $nonIgnored = preg_replace("/^.*{$nonEscaped}!/s", '', $format); |
||
| 613 | |||
| 614 | if ($tz === null && !preg_match("/{$nonEscaped}[eOPT]/", $nonIgnored)) { |
||
| 615 | $tz = clone $mock->getTimezone(); |
||
| 616 | } |
||
| 617 | |||
| 618 | // Set microseconds to zero to match behavior of DateTime::createFromFormat() |
||
| 619 | // See https://bugs.php.net/bug.php?id=74332 |
||
| 620 | $mock = $mock->copy()->microsecond(0); |
||
| 621 | |||
| 622 | // Prepend mock datetime only if the format does not contain non escaped unix epoch reset flag. |
||
| 623 | if (!preg_match("/{$nonEscaped}[!|]/", $format)) { |
||
| 624 | $format = static::MOCK_DATETIME_FORMAT.' '.$format; |
||
| 625 | $time = ($mock instanceof self ? $mock->rawFormat(static::MOCK_DATETIME_FORMAT) : $mock->format(static::MOCK_DATETIME_FORMAT)).' '.$time; |
||
| 626 | } |
||
| 627 | |||
| 628 | // Regenerate date from the modified format to base result on the mocked instance instead of now. |
||
| 629 | $date = self::createFromFormatAndTimezone($format, $time, $tz); |
||
| 630 | } |
||
| 631 | |||
| 632 | if ($date instanceof DateTimeInterface) { |
||
| 633 | $instance = static::instance($date); |
||
| 634 | $instance::setLastErrors($lastErrors); |
||
| 635 | |||
| 636 | return $instance; |
||
| 637 | } |
||
| 638 | |||
| 639 | if (static::isStrictModeEnabled()) { |
||
| 640 | throw new InvalidFormatException(implode(PHP_EOL, $lastErrors['errors'])); |
||
| 641 | } |
||
| 642 | |||
| 643 | return false; |
||
| 644 | } |
||
| 645 | |||
| 646 | /** |
||
| 647 | * Create a Carbon instance from a specific format. |
||
| 648 | * |
||
| 649 | * @param string $format Datetime format |
||
| 650 | * @param string $time |
||
| 651 | * @param DateTimeZone|string|false|null $tz |
||
| 652 | * |
||
| 653 | * @throws InvalidFormatException |
||
| 654 | * |
||
| 655 | * @return static|false |
||
| 656 | */ |
||
| 657 | public static function createFromFormat($format, $time, $tz = null) |
||
| 658 | { |
||
| 659 | $function = static::$createFromFormatFunction; |
||
| 660 | |||
| 661 | if (!$function) { |
||
| 662 | return static::rawCreateFromFormat($format, $time, $tz); |
||
| 663 | } |
||
| 664 | |||
| 665 | if (is_string($function) && method_exists(static::class, $function)) { |
||
| 666 | $function = [static::class, $function]; |
||
| 667 | } |
||
| 668 | |||
| 669 | return $function(...func_get_args()); |
||
| 670 | } |
||
| 671 | |||
| 672 | /** |
||
| 673 | * Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). |
||
| 674 | * |
||
| 675 | * @param string $format Datetime format |
||
| 676 | * @param string $time |
||
| 677 | * @param DateTimeZone|string|false|null $tz optional timezone |
||
| 678 | * @param string|null $locale locale to be used for LTS, LT, LL, LLL, etc. macro-formats (en by fault, unneeded if no such macro-format in use) |
||
| 679 | * @param \Symfony\Component\Translation\TranslatorInterface $translator optional custom translator to use for macro-formats |
||
| 680 | * |
||
| 681 | * @throws InvalidFormatException |
||
| 682 | * |
||
| 683 | * @return static|false |
||
| 684 | */ |
||
| 685 | public static function createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) |
||
| 686 | { |
||
| 687 | $format = preg_replace_callback('/(?<!\\\\)(\\\\{2})*(LTS|LT|[Ll]{1,4})/', function ($match) use ($locale, $translator) { |
||
| 688 | [$code] = $match; |
||
| 689 | |||
| 690 | static $formats = null; |
||
| 691 | |||
| 692 | if ($formats === null) { |
||
| 693 | $translator = $translator ?: Translator::get($locale); |
||
| 694 | |||
| 695 | $formats = [ |
||
| 696 | 'LT' => static::getTranslationMessageWith($translator, 'formats.LT', $locale, 'h:mm A'), |
||
| 697 | 'LTS' => static::getTranslationMessageWith($translator, 'formats.LTS', $locale, 'h:mm:ss A'), |
||
| 698 | 'L' => static::getTranslationMessageWith($translator, 'formats.L', $locale, 'MM/DD/YYYY'), |
||
| 699 | 'LL' => static::getTranslationMessageWith($translator, 'formats.LL', $locale, 'MMMM D, YYYY'), |
||
| 700 | 'LLL' => static::getTranslationMessageWith($translator, 'formats.LLL', $locale, 'MMMM D, YYYY h:mm A'), |
||
| 701 | 'LLLL' => static::getTranslationMessageWith($translator, 'formats.LLLL', $locale, 'dddd, MMMM D, YYYY h:mm A'), |
||
| 702 | ]; |
||
| 703 | } |
||
| 704 | |||
| 705 | return $formats[$code] ?? preg_replace_callback( |
||
| 706 | '/MMMM|MM|DD|dddd/', |
||
| 707 | function ($code) { |
||
| 708 | return mb_substr($code[0], 1); |
||
| 709 | }, |
||
| 710 | $formats[strtoupper($code)] ?? '' |
||
| 711 | ); |
||
| 712 | }, $format); |
||
| 713 | |||
| 714 | $format = preg_replace_callback('/(?<!\\\\)(\\\\{2})*('.CarbonInterface::ISO_FORMAT_REGEXP.'|[A-Za-z])/', function ($match) { |
||
| 715 | [$code] = $match; |
||
| 716 | |||
| 717 | static $replacements = null; |
||
| 718 | |||
| 719 | if ($replacements === null) { |
||
| 720 | $replacements = [ |
||
| 721 | 'OD' => 'd', |
||
| 722 | 'OM' => 'M', |
||
| 723 | 'OY' => 'Y', |
||
| 724 | 'OH' => 'G', |
||
| 725 | 'Oh' => 'g', |
||
| 726 | 'Om' => 'i', |
||
| 727 | 'Os' => 's', |
||
| 728 | 'D' => 'd', |
||
| 729 | 'DD' => 'd', |
||
| 730 | 'Do' => 'd', |
||
| 731 | 'd' => '!', |
||
| 732 | 'dd' => '!', |
||
| 733 | 'ddd' => 'D', |
||
| 734 | 'dddd' => 'D', |
||
| 735 | 'DDD' => 'z', |
||
| 736 | 'DDDD' => 'z', |
||
| 737 | 'DDDo' => 'z', |
||
| 738 | 'e' => '!', |
||
| 739 | 'E' => '!', |
||
| 740 | 'H' => 'G', |
||
| 741 | 'HH' => 'H', |
||
| 742 | 'h' => 'g', |
||
| 743 | 'hh' => 'h', |
||
| 744 | 'k' => 'G', |
||
| 745 | 'kk' => 'G', |
||
| 746 | 'hmm' => 'gi', |
||
| 747 | 'hmmss' => 'gis', |
||
| 748 | 'Hmm' => 'Gi', |
||
| 749 | 'Hmmss' => 'Gis', |
||
| 750 | 'm' => 'i', |
||
| 751 | 'mm' => 'i', |
||
| 752 | 'a' => 'a', |
||
| 753 | 'A' => 'a', |
||
| 754 | 's' => 's', |
||
| 755 | 'ss' => 's', |
||
| 756 | 'S' => '*', |
||
| 757 | 'SS' => '*', |
||
| 758 | 'SSS' => '*', |
||
| 759 | 'SSSS' => '*', |
||
| 760 | 'SSSSS' => '*', |
||
| 761 | 'SSSSSS' => 'u', |
||
| 762 | 'SSSSSSS' => 'u*', |
||
| 763 | 'SSSSSSSS' => 'u*', |
||
| 764 | 'SSSSSSSSS' => 'u*', |
||
| 765 | 'M' => 'm', |
||
| 766 | 'MM' => 'm', |
||
| 767 | 'MMM' => 'M', |
||
| 768 | 'MMMM' => 'M', |
||
| 769 | 'Mo' => 'm', |
||
| 770 | 'Q' => '!', |
||
| 771 | 'Qo' => '!', |
||
| 772 | 'G' => '!', |
||
| 773 | 'GG' => '!', |
||
| 774 | 'GGG' => '!', |
||
| 775 | 'GGGG' => '!', |
||
| 776 | 'GGGGG' => '!', |
||
| 777 | 'g' => '!', |
||
| 778 | 'gg' => '!', |
||
| 779 | 'ggg' => '!', |
||
| 780 | 'gggg' => '!', |
||
| 781 | 'ggggg' => '!', |
||
| 782 | 'W' => '!', |
||
| 783 | 'WW' => '!', |
||
| 784 | 'Wo' => '!', |
||
| 785 | 'w' => '!', |
||
| 786 | 'ww' => '!', |
||
| 787 | 'wo' => '!', |
||
| 788 | 'x' => 'U???', |
||
| 789 | 'X' => 'U', |
||
| 790 | 'Y' => 'Y', |
||
| 791 | 'YY' => 'y', |
||
| 792 | 'YYYY' => 'Y', |
||
| 793 | 'YYYYY' => 'Y', |
||
| 794 | 'YYYYYY' => 'Y', |
||
| 795 | 'z' => 'e', |
||
| 796 | 'zz' => 'e', |
||
| 797 | 'Z' => 'e', |
||
| 798 | 'ZZ' => 'e', |
||
| 799 | ]; |
||
| 800 | } |
||
| 801 | |||
| 802 | $format = $replacements[$code] ?? '?'; |
||
| 803 | |||
| 804 | if ($format === '!') { |
||
| 805 | throw new InvalidFormatException("Format $code not supported for creation."); |
||
| 806 | } |
||
| 807 | |||
| 808 | return $format; |
||
| 809 | }, $format); |
||
| 810 | |||
| 811 | return static::rawCreateFromFormat($format, $time, $tz); |
||
| 812 | } |
||
| 813 | |||
| 814 | /** |
||
| 815 | * Create a Carbon instance from a specific format and a string in a given language. |
||
| 816 | * |
||
| 817 | * @param string $format Datetime format |
||
| 818 | * @param string $locale |
||
| 819 | * @param string $time |
||
| 820 | * @param DateTimeZone|string|false|null $tz |
||
| 821 | * |
||
| 822 | * @throws InvalidFormatException |
||
| 823 | * |
||
| 824 | * @return static|false |
||
| 825 | */ |
||
| 826 | public static function createFromLocaleFormat($format, $locale, $time, $tz = null) |
||
| 827 | { |
||
| 828 | return static::rawCreateFromFormat($format, static::translateTimeString($time, $locale, 'en'), $tz); |
||
| 829 | } |
||
| 830 | |||
| 831 | /** |
||
| 832 | * Create a Carbon instance from a specific ISO format and a string in a given language. |
||
| 833 | * |
||
| 834 | * @param string $format Datetime ISO format |
||
| 835 | * @param string $locale |
||
| 836 | * @param string $time |
||
| 837 | * @param DateTimeZone|string|false|null $tz |
||
| 838 | * |
||
| 839 | * @throws InvalidFormatException |
||
| 840 | * |
||
| 841 | * @return static|false |
||
| 842 | */ |
||
| 843 | public static function createFromLocaleIsoFormat($format, $locale, $time, $tz = null) |
||
| 844 | { |
||
| 845 | $time = static::translateTimeString($time, $locale, 'en', CarbonInterface::TRANSLATE_MONTHS | CarbonInterface::TRANSLATE_DAYS | CarbonInterface::TRANSLATE_MERIDIEM); |
||
| 846 | |||
| 847 | return static::createFromIsoFormat($format, $time, $tz, $locale); |
||
| 848 | } |
||
| 849 | |||
| 850 | /** |
||
| 851 | * Make a Carbon instance from given variable if possible. |
||
| 852 | * |
||
| 853 | * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals |
||
| 854 | * and recurrences). Throw an exception for invalid format, but otherwise return null. |
||
| 855 | * |
||
| 856 | * @param mixed $var |
||
| 857 | * |
||
| 858 | * @throws InvalidFormatException |
||
| 859 | * |
||
| 860 | * @return static|null |
||
| 861 | */ |
||
| 862 | public static function make($var) |
||
| 863 | { |
||
| 864 | if ($var instanceof DateTimeInterface) { |
||
| 865 | return static::instance($var); |
||
| 866 | } |
||
| 867 | |||
| 868 | $date = null; |
||
| 869 | |||
| 870 | if (is_string($var)) { |
||
| 871 | $var = trim($var); |
||
| 872 | |||
| 873 | if (is_string($var) && |
||
| 874 | !preg_match('/^P[0-9T]/', $var) && |
||
| 875 | !preg_match('/^R[0-9]/', $var) && |
||
| 876 | preg_match('/[a-z0-9]/i', $var) |
||
| 877 | ) { |
||
| 878 | $date = static::parse($var); |
||
| 879 | } |
||
| 880 | } |
||
| 881 | |||
| 882 | return $date; |
||
| 883 | } |
||
| 884 | |||
| 885 | /** |
||
| 886 | * Set last errors. |
||
| 887 | * |
||
| 888 | * @param array $lastErrors |
||
| 889 | * |
||
| 890 | * @return void |
||
| 891 | */ |
||
| 892 | private static function setLastErrors(array $lastErrors) |
||
| 893 | { |
||
| 894 | static::$lastErrors = $lastErrors; |
||
| 895 | } |
||
| 896 | |||
| 897 | /** |
||
| 898 | * {@inheritdoc} |
||
| 899 | */ |
||
| 900 | public static function getLastErrors() |
||
| 901 | { |
||
| 902 | return static::$lastErrors; |
||
| 903 | } |
||
| 905 |