You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateFactory.php 7.8KB

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