Debian Codegraph @

lib/3rdParty/composer/illuminate/support/DateFactory.php

raw symbols SCIP
1 <?php
2
3 namespace Illuminate\Support;
4
5 use Carbon\Factory;
6 use InvalidArgumentException;
7
8 /**
9 * @see https://carbon.nesbot.com/docs/
10 * @see https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Factory.php
11 *
12 * @method bool canBeCreatedFromFormat(?string $date, string $format)
13 * @method \Illuminate\Support\Carbon|null create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $timezone = null)
14 * @method \Illuminate\Support\Carbon createFromDate($year = null, $month = null, $day = null, $timezone = null)
15 * @method \Illuminate\Support\Carbon|null createFromFormat($format, $time, $timezone = null)
16 * @method \Illuminate\Support\Carbon|null createFromIsoFormat(string $format, string $time, $timezone = null, ?string $locale = 'en', ?\Symfony\Contracts\Translation\TranslatorInterface $translator = null)
17 * @method \Illuminate\Support\Carbon|null createFromLocaleFormat(string $format, string $locale, string $time, $timezone = null)
18 * @method \Illuminate\Support\Carbon|null createFromLocaleIsoFormat(string $format, string $locale, string $time, $timezone = null)
19 * @method \Illuminate\Support\Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $timezone = null)
20 * @method \Illuminate\Support\Carbon createFromTimeString(string $time, \DateTimeZone|string|int|null $timezone = null)
21 * @method \Illuminate\Support\Carbon createFromTimestamp(string|int|float $timestamp, \DateTimeZone|string|int|null $timezone = null)
22 * @method \Illuminate\Support\Carbon createFromTimestampMs(string|int|float $timestamp, \DateTimeZone|string|int|null $timezone = null)
23 * @method \Illuminate\Support\Carbon createFromTimestampMsUTC($timestamp)
24 * @method \Illuminate\Support\Carbon createFromTimestampUTC(string|int|float $timestamp)
25 * @method \Illuminate\Support\Carbon createMidnightDate($year = null, $month = null, $day = null, $timezone = null)
26 * @method \Illuminate\Support\Carbon|null createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $timezone = null)
27 * @method \Illuminate\Support\Carbon createStrict(?int $year = 0, ?int $month = 1, ?int $day = 1, ?int $hour = 0, ?int $minute = 0, ?int $second = 0, $timezone = null)
28 * @method void disableHumanDiffOption($humanDiffOption)
29 * @method void enableHumanDiffOption($humanDiffOption)
30 * @method mixed executeWithLocale(string $locale, callable $func)
31 * @method \Illuminate\Support\Carbon fromSerialized($value)
32 * @method array getAvailableLocales()
33 * @method array getAvailableLocalesInfo()
34 * @method array getDays()
35 * @method ?string getFallbackLocale()
36 * @method array getFormatsToIsoReplacements()
37 * @method int getHumanDiffOptions()
38 * @method array getIsoUnits()
39 * @method array|false getLastErrors()
40 * @method string getLocale()
41 * @method int getMidDayAt()
42 * @method string getTimeFormatByPrecision(string $unitPrecision)
43 * @method string|\Closure|null getTranslationMessageWith($translator, string $key, ?string $locale = null, ?string $default = null)
44 * @method \Illuminate\Support\Carbon|null getTestNow()
45 * @method \Symfony\Contracts\Translation\TranslatorInterface getTranslator()
46 * @method int getWeekEndsAt(?string $locale = null)
47 * @method int getWeekStartsAt(?string $locale = null)
48 * @method array getWeekendDays()
49 * @method bool hasFormat(string $date, string $format)
50 * @method bool hasFormatWithModifiers(string $date, string $format)
51 * @method bool hasMacro($name)
52 * @method bool hasRelativeKeywords(?string $time)
53 * @method bool hasTestNow()
54 * @method \Illuminate\Support\Carbon instance(\DateTimeInterface $date)
55 * @method bool isImmutable()
56 * @method bool isModifiableUnit($unit)
57 * @method bool isMutable()
58 * @method bool isStrictModeEnabled()
59 * @method bool localeHasDiffOneDayWords(string $locale)
60 * @method bool localeHasDiffSyntax(string $locale)
61 * @method bool localeHasDiffTwoDayWords(string $locale)
62 * @method bool localeHasPeriodSyntax($locale)
63 * @method bool localeHasShortUnits(string $locale)
64 * @method void macro(string $name, ?callable $macro)
65 * @method \Illuminate\Support\Carbon|null make($var, \DateTimeZone|string|null $timezone = null)
66 * @method void mixin(object|string $mixin)
67 * @method \Illuminate\Support\Carbon now(\DateTimeZone|string|int|null $timezone = null)
68 * @method \Illuminate\Support\Carbon parse(\DateTimeInterface|\Carbon\WeekDay|\Carbon\Month|string|int|float|null $time, \DateTimeZone|string|int|null $timezone = null)
69 * @method \Illuminate\Support\Carbon parseFromLocale(string $time, ?string $locale = null, \DateTimeZone|string|int|null $timezone = null)
70 * @method string pluralUnit(string $unit)
71 * @method \Illuminate\Support\Carbon|null rawCreateFromFormat(string $format, string $time, $timezone = null)
72 * @method \Illuminate\Support\Carbon rawParse(\DateTimeInterface|\Carbon\WeekDay|\Carbon\Month|string|int|float|null $time, \DateTimeZone|string|int|null $timezone = null)
73 * @method void resetMonthsOverflow()
74 * @method void resetToStringFormat()
75 * @method void resetYearsOverflow()
76 * @method void serializeUsing($callback)
77 * @method void setFallbackLocale(string $locale)
78 * @method void setHumanDiffOptions($humanDiffOptions)
79 * @method void setLocale(string $locale)
80 * @method void setMidDayAt($hour)
81 * @method void setTestNow(mixed $testNow = null)
82 * @method void setTestNowAndTimezone(mixed $testNow = null, $timezone = null)
83 * @method void setToStringFormat(string|\Closure|null $format)
84 * @method void setTranslator(\Symfony\Contracts\Translation\TranslatorInterface $translator)
85 * @method void setWeekEndsAt($day)
86 * @method void setWeekStartsAt($day)
87 * @method void setWeekendDays($days)
88 * @method bool shouldOverflowMonths()
89 * @method bool shouldOverflowYears()
90 * @method string singularUnit(string $unit)
91 * @method void sleep(int|float $seconds)
92 * @method \Illuminate\Support\Carbon today(\DateTimeZone|string|int|null $timezone = null)
93 * @method \Illuminate\Support\Carbon tomorrow(\DateTimeZone|string|int|null $timezone = null)
94 * @method string translateTimeString(string $timeString, ?string $from = null, ?string $to = null, int $mode = \Carbon\CarbonInterface::TRANSLATE_ALL)
95 * @method string translateWith(\Symfony\Contracts\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null)
96 * @method void useMonthsOverflow($monthsOverflow = true)
97 * @method void useStrictMode($strictModeEnabled = true)
98 * @method void useYearsOverflow($yearsOverflow = true)
99 * @method mixed withTestNow(mixed $testNow, callable $callback)
100 * @method static withTimeZone(\DateTimeZone|string|int|null $timezone)
101 * @method \Illuminate\Support\Carbon yesterday(\DateTimeZone|string|int|null $timezone = null)
102 */
103 class DateFactory
104 {
105 /**
106 * The default class that will be used for all created dates.
107 *
108 * @var string
109 */
110 const DEFAULT_CLASS_NAME = Carbon::class;
111
112 /**
113 * The type (class) of dates that should be created.
114 *
115 * @var string
116 */
117 protected static $dateClass;
118
119 /**
120 * This callable may be used to intercept date creation.
121 *
122 * @var callable
123 */
124 protected static $callable;
125
126 /**
127 * The Carbon factory that should be used when creating dates.
128 *
129 * @var object
130 */
131 protected static $factory;
132
133 /**
134 * Use the given handler when generating dates (class name, callable, or factory).
135 *
136 * @param mixed $handler
137 * @return mixed
138 *
139 * @throws \InvalidArgumentException
140 */
141 public static function use($handler)
142 {
143 if (is_callable($handler) && is_object($handler)) {
144 return static::useCallable($handler);
145 } elseif (is_string($handler)) {
146 return static::useClass($handler);
147 } elseif ($handler instanceof Factory) {
148 return static::useFactory($handler);
149 }
150
151 throw new InvalidArgumentException('Invalid date creation handler. Please provide a class name, callable, or Carbon factory.');
152 }
153
154 /**
155 * Use the default date class when generating dates.
156 *
157 * @return void
158 */
159 public static function useDefault()
160 {
161 static::$dateClass = null;
162 static::$callable = null;
163 static::$factory = null;
164 }
165
166 /**
167 * Execute the given callable on each date creation.
168 *
169 * @param callable $callable
170 * @return void
171 */
172 public static function useCallable(callable $callable)
173 {
174 static::$callable = $callable;
175
176 static::$dateClass = null;
177 static::$factory = null;
178 }
179
180 /**
181 * Use the given date type (class) when generating dates.
182 *
183 * @param string $dateClass
184 * @return void
185 */
186 public static function useClass($dateClass)
187 {
188 static::$dateClass = $dateClass;
189
190 static::$factory = null;
191 static::$callable = null;
192 }
193
194 /**
195 * Use the given Carbon factory when generating dates.
196 *
197 * @param object $factory
198 * @return void
199 */
200 public static function useFactory($factory)
201 {
202 static::$factory = $factory;
203
204 static::$dateClass = null;
205 static::$callable = null;
206 }
207
208 /**
209 * Handle dynamic calls to generate dates.
210 *
211 * @param string $method
212 * @param array $parameters
213 * @return mixed
214 *
215 * @throws \RuntimeException
216 */
217 public function __call($method, $parameters)
218 {
219 $defaultClassName = static::DEFAULT_CLASS_NAME;
220
221 // Using callable to generate dates...
222 if (static::$callable) {
223 return call_user_func(static::$callable, $defaultClassName::$method(...$parameters));
224 }
225
226 // Using Carbon factory to generate dates...
227 if (static::$factory) {
228 return static::$factory->$method(...$parameters);
229 }
230
231 $dateClass = static::$dateClass ?: $defaultClassName;
232
233 // Check if the date can be created using the public class method...
234 if (method_exists($dateClass, $method) ||
235 method_exists($dateClass, 'hasMacro') && $dateClass::hasMacro($method)) {
236 return $dateClass::$method(...$parameters);
237 }
238
239 // If that fails, create the date with the default class...
240 $date = $defaultClassName::$method(...$parameters);
241
242 // If the configured class has an "instance" method, we'll try to pass our date into there...
243 if (method_exists($dateClass, 'instance')) {
244 return $dateClass::instance($date);
245 }
246
247 // Otherwise, assume the configured class has a DateTime compatible constructor...
248 return new $dateClass($date->format('Y-m-d H:i:s.u'), $date->getTimezone());
249 }
250 }