| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | /** |
| 6 | * This file is part of the Carbon package. |
| 7 | * |
| 8 | * (c) Brian Nesbitt <[email protected]> |
| 9 | * |
| 10 | * For the full copyright and license information, please view the LICENSE |
| 11 | * file that was distributed with this source code. |
| 12 | */ |
| 13 | |
| 14 | namespace Carbon; |
| 15 | |
| 16 | use Closure; |
| 17 | use DateTimeInterface; |
| 18 | use DateTimeZone; |
| 19 | use Symfony\Component\Clock\ClockInterface; |
| 20 | use Symfony\Component\Clock\NativeClock; |
| 21 | use Symfony\Contracts\Translation\TranslatorInterface; |
| 22 | |
| 23 | /** |
| 24 | * A factory to generate CarbonImmutable instances with common settings. |
| 25 | * |
| 26 | * <autodoc generated by `composer phpdoc`> |
| 27 | * |
| 28 | * @method bool canBeCreatedFromFormat(?string $date, string $format) Checks if the (date)time string is in a given format and valid to create a |
| 29 | * new instance. |
| 30 | * @method ?CarbonImmutable create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $timezone = null) Create a new Carbon instance from a specific date and time. |
| 31 | * If any of $year, $month or $day are set to null their now() values will |
| 32 | * be used. |
| 33 | * If $hour is null it will be set to its now() value and the default |
| 34 | * values for $minute and $second will be their now() values. |
| 35 | * If $hour is not null then the default values for $minute and $second |
| 36 | * will be 0. |
| 37 | * @method CarbonImmutable createFromDate($year = null, $month = null, $day = null, $timezone = null) Create a Carbon instance from just a date. The time portion is set to now. |
| 38 | * @method ?CarbonImmutable createFromFormat($format, $time, $timezone = null) Create a Carbon instance from a specific format. |
| 39 | * @method ?CarbonImmutable createFromIsoFormat(string $format, string $time, $timezone = null, ?string $locale = 'en', ?TranslatorInterface $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()). |
| 40 | * @method ?CarbonImmutable createFromLocaleFormat(string $format, string $locale, string $time, $timezone = null) Create a Carbon instance from a specific format and a string in a given language. |
| 41 | * @method ?CarbonImmutable createFromLocaleIsoFormat(string $format, string $locale, string $time, $timezone = null) Create a Carbon instance from a specific ISO format and a string in a given language. |
| 42 | * @method CarbonImmutable createFromTime($hour = 0, $minute = 0, $second = 0, $timezone = null) Create a Carbon instance from just a time. The date portion is set to today. |
| 43 | * @method CarbonImmutable createFromTimeString(string $time, DateTimeZone|string|int|null $timezone = null) Create a Carbon instance from a time string. The date portion is set to today. |
| 44 | * @method CarbonImmutable createFromTimestamp(string|int|float $timestamp, DateTimeZone|string|int|null $timezone = null) Create a Carbon instance from a timestamp and set the timezone (UTC by default). |
| 45 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 46 | * @method CarbonImmutable createFromTimestampMs(string|int|float $timestamp, DateTimeZone|string|int|null $timezone = null) Create a Carbon instance from a timestamp in milliseconds. |
| 47 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 48 | * @method CarbonImmutable createFromTimestampMsUTC($timestamp) Create a Carbon instance from a timestamp in milliseconds. |
| 49 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 50 | * @method CarbonImmutable createFromTimestampUTC(string|int|float $timestamp) Create a Carbon instance from a timestamp keeping the timezone to UTC. |
| 51 | * Timestamp input can be given as int, float or a string containing one or more numbers. |
| 52 | * @method CarbonImmutable createMidnightDate($year = null, $month = null, $day = null, $timezone = null) Create a Carbon instance from just a date. The time portion is set to midnight. |
| 53 | * @method ?CarbonImmutable createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null) Create a new safe Carbon instance from a specific date and time. |
| 54 | * If any of $year, $month or $day are set to null their now() values will |
| 55 | * be used. |
| 56 | * If $hour is null it will be set to its now() value and the default |
| 57 | * values for $minute and $second will be their now() values. |
| 58 | * If $hour is not null then the default values for $minute and $second |
| 59 | * will be 0. |
| 60 | * If one of the set values is not valid, an InvalidDateException |
| 61 | * will be thrown. |
| 62 | * @method CarbonImmutable createStrict(?int $year = 0, ?int $month = 1, ?int $day = 1, ?int $hour = 0, ?int $minute = 0, ?int $second = 0, $timezone = null) Create a new Carbon instance from a specific date and time using strict validation. |
| 63 | * @method mixed executeWithLocale(string $locale, callable $func) Set the current locale to the given, execute the passed function, reset the locale to previous one, |
| 64 | * then return the result of the closure (or null if the closure was void). |
| 65 | * @method CarbonImmutable fromSerialized($value, array $options = []) Create an instance from a serialized string. |
| 66 | * If $value is not from a trusted source, consider using the allowed_classes option to limit |
| 67 | * the types of objects that can be built, for instance: |
| 68 | * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales. |
| 69 | * (It will ignore custom translator dynamic loading.) |
| 70 | * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native |
| 71 | * name, region and variant of the locale. |
| 72 | * @method array getDays() Get the days of the week. |
| 73 | * @method ?string getFallbackLocale() Get the fallback locale. |
| 74 | * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat(). |
| 75 | * @method array getIsoUnits() Returns list of locale units for ISO formatting. |
| 76 | * @method array|false getLastErrors() {@inheritdoc} |
| 77 | * @method string getLocale() Get the current translator locale. |
| 78 | * @method int getMidDayAt() get midday/noon hour |
| 79 | * @method string getTimeFormatByPrecision(string $unitPrecision) Return a format from H:i to H:i:s.u according to given unit precision. |
| 80 | * @method string|Closure|null getTranslationMessageWith($translator, string $key, ?string $locale = null, ?string $default = null) Returns raw translation message for a given key. |
| 81 | * @method int getWeekEndsAt(?string $locale = null) Get the last day of week. |
| 82 | * @method int getWeekStartsAt(?string $locale = null) Get the first day of week. |
| 83 | * @method bool hasRelativeKeywords(?string $time) Determine if a time string will produce a relative date. |
| 84 | * @method CarbonImmutable instance(DateTimeInterface $date) Create a Carbon instance from a DateTime one. |
| 85 | * @method bool isImmutable() Returns true if the current class/instance is immutable. |
| 86 | * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter. |
| 87 | * @method bool isMutable() Returns true if the current class/instance is mutable. |
| 88 | * @method bool localeHasDiffOneDayWords(string $locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow). |
| 89 | * Support is considered enabled if the 3 words are translated in the given locale. |
| 90 | * @method bool localeHasDiffSyntax(string $locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after). |
| 91 | * Support is considered enabled if the 4 sentences are translated in the given locale. |
| 92 | * @method bool localeHasDiffTwoDayWords(string $locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow). |
| 93 | * Support is considered enabled if the 2 words are translated in the given locale. |
| 94 | * @method bool localeHasPeriodSyntax($locale) Returns true if the given locale is internally supported and has period syntax support (X times, every X, from X, to X). |
| 95 | * Support is considered enabled if the 4 sentences are translated in the given locale. |
| 96 | * @method bool localeHasShortUnits(string $locale) Returns true if the given locale is internally supported and has short-units support. |
| 97 | * Support is considered enabled if either year, day or hour has a short variant translated. |
| 98 | * @method ?CarbonImmutable make($var, DateTimeZone|string|null $timezone = null) Make a Carbon instance from given variable if possible. |
| 99 | * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals |
| 100 | * and recurrences). Throw an exception for invalid format, but otherwise return null. |
| 101 | * @method void mixin(object|string $mixin) Mix another object into the class. |
| 102 | * @method CarbonImmutable parse(DateTimeInterface|WeekDay|Month|string|int|float|null $time, DateTimeZone|string|int|null $timezone = null) Create a carbon instance from a string. |
| 103 | * This is an alias for the constructor that allows better fluent syntax |
| 104 | * as it allows you to do Carbon::parse('Monday next week')->fn() rather |
| 105 | * than (new Carbon('Monday next week'))->fn(). |
| 106 | * @method CarbonImmutable parseFromLocale(string $time, ?string $locale = null, DateTimeZone|string|int|null $timezone = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.). |
| 107 | * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English). |
| 108 | * @method ?CarbonImmutable rawCreateFromFormat(string $format, string $time, $timezone = null) Create a Carbon instance from a specific format. |
| 109 | * @method CarbonImmutable rawParse(DateTimeInterface|WeekDay|Month|string|int|float|null $time, DateTimeZone|string|int|null $timezone = null) Create a carbon instance from a string. |
| 110 | * This is an alias for the constructor that allows better fluent syntax |
| 111 | * as it allows you to do Carbon::parse('Monday next week')->fn() rather |
| 112 | * than (new Carbon('Monday next week'))->fn(). |
| 113 | * @method void setFallbackLocale(string $locale) Set the fallback locale. |
| 114 | * @method void setLocale(string $locale) Set the current translator locale and indicate if the source locale file exists. |
| 115 | * Pass 'auto' as locale to use the closest language to the current LC_TIME locale. |
| 116 | * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used. |
| 117 | * You should rather consider mid-day is always 12pm, then if you need to test if it's an other |
| 118 | * hour, test it explicitly: |
| 119 | * $date->format('G') == 13 |
| 120 | * or to set explicitly to a given hour: |
| 121 | * $date->setTime(13, 0, 0, 0) |
| 122 | * Set midday/noon hour |
| 123 | * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English). |
| 124 | * @method CarbonImmutable today(DateTimeZone|string|int|null $timezone = null) Create a Carbon instance for today. |
| 125 | * @method CarbonImmutable tomorrow(DateTimeZone|string|int|null $timezone = null) Create a Carbon instance for tomorrow. |
| 126 | * @method string translateTimeString(string $timeString, ?string $from = null, ?string $to = null, int $mode = CarbonInterface::TRANSLATE_ALL) Translate a time string from a locale to an other. |
| 127 | * @method string translateWith(TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available. |
| 128 | * @method CarbonImmutable yesterday(DateTimeZone|string|int|null $timezone = null) Create a Carbon instance for yesterday. |
| 129 | * |
| 130 | * </autodoc> |
| 131 | */ |
| 132 | class FactoryImmutable extends Factory implements ClockInterface |
| 133 | { |
| 134 | protected string $className = CarbonImmutable::class; |
| 135 | |
| 136 | private static ?self $defaultInstance = null; |
| 137 | |
| 138 | private static ?WrapperClock $currentClock = null; |
| 139 | |
| 140 | /** |
| 141 | * @internal Instance used for static calls, such as Carbon::getTranslator(), CarbonImmutable::setTestNow(), etc. |
| 142 | */ |
| 143 | public static function getDefaultInstance(): self |
| 144 | { |
| 145 | return self::$defaultInstance ??= new self(); |
| 146 | } |
| 147 | |
| 148 | /** |
| 149 | * @internal Instance used for static calls possibly called by non-static methods. |
| 150 | */ |
| 151 | public static function getInstance(): Factory |
| 152 | { |
| 153 | return self::$currentClock?->getFactory() ?? self::getDefaultInstance(); |
| 154 | } |
| 155 | |
| 156 | /** |
| 157 | * @internal Set instance before creating new dates. |
| 158 | */ |
| 159 | public static function setCurrentClock(ClockInterface|Factory|DateTimeInterface|null $currentClock): void |
| 160 | { |
| 161 | if ($currentClock && !($currentClock instanceof WrapperClock)) { |
| 162 | $currentClock = new WrapperClock($currentClock); |
| 163 | } |
| 164 | |
| 165 | self::$currentClock = $currentClock; |
| 166 | } |
| 167 | |
| 168 | /** |
| 169 | * @internal Instance used to link new object to their factory creator. |
| 170 | */ |
| 171 | public static function getCurrentClock(): ?WrapperClock |
| 172 | { |
| 173 | return self::$currentClock; |
| 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Get a Carbon instance for the current date and time. |
| 178 | */ |
| 179 | public function now(DateTimeZone|string|int|null $timezone = null): CarbonImmutable |
| 180 | { |
| 181 | return $this->__call('now', [$timezone]); |
| 182 | } |
| 183 | |
| 184 | public function sleep(int|float $seconds): void |
| 185 | { |
| 186 | if ($this->hasTestNow()) { |
| 187 | $this->setTestNow($this->getTestNow()->avoidMutation()->addSeconds($seconds)); |
| 188 | |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | (new NativeClock('UTC'))->sleep($seconds); |
| 193 | } |
| 194 | } |