View file vendor/illuminate/support/DateFactory.php

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