Factory.php 49 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <?php
  2. /**
  3. * This file is part of the Carbon package.
  4. *
  5. * (c) Brian Nesbitt <brian@nesbot.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Carbon;
  11. /**
  12. * A factory to generate Carbon instances with common settings.
  13. *
  14. * <autodoc generated by `composer phpdoc`>
  15. *
  16. * @method Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null) Create a new Carbon instance from a specific date and time.
  17. * If any of $year, $month or $day are set to null their now() values will
  18. * be used.
  19. * If $hour is null it will be set to its now() value and the default
  20. * values for $minute and $second will be their now() values.
  21. * If $hour is not null then the default values for $minute and $second
  22. * will be 0.
  23. * @method Carbon createFromDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to now.
  24. * @method Carbon|false createFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format.
  25. * @method Carbon|false createFromIsoFormat($format, $time, $tz = null, $locale = 'en', $translator = null) Create a Carbon instance from a specific ISO format (same replacements as ->isoFormat()).
  26. * @method Carbon|false createFromLocaleFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific format and a string in a given language.
  27. * @method Carbon|false createFromLocaleIsoFormat($format, $locale, $time, $tz = null) Create a Carbon instance from a specific ISO format and a string in a given language.
  28. * @method Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null) Create a Carbon instance from just a time. The date portion is set to today.
  29. * @method Carbon createFromTimeString($time, $tz = null) Create a Carbon instance from a time string. The date portion is set to today.
  30. * @method Carbon createFromTimestamp($timestamp, $tz = null) Create a Carbon instance from a timestamp.
  31. * @method Carbon createFromTimestampMs($timestamp, $tz = null) Create a Carbon instance from a timestamp in milliseconds.
  32. * @method Carbon createFromTimestampUTC($timestamp) Create a Carbon instance from an UTC timestamp.
  33. * @method Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null) Create a Carbon instance from just a date. The time portion is set to midnight.
  34. * @method Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null) Create a new safe Carbon instance from a specific date and time.
  35. * If any of $year, $month or $day are set to null their now() values will
  36. * be used.
  37. * If $hour is null it will be set to its now() value and the default
  38. * values for $minute and $second will be their now() values.
  39. * If $hour is not null then the default values for $minute and $second
  40. * will be 0.
  41. * If one of the set values is not valid, an \InvalidArgumentException
  42. * will be thrown.
  43. * @method Carbon disableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  44. * You should rather use the ->settings() method.
  45. * @method Carbon enableHumanDiffOption($humanDiffOption) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  46. * You should rather use the ->settings() method.
  47. * @method mixed executeWithLocale($locale, $func) Set the current locale to the given, execute the passed function, reset the locale to previous one,
  48. * then return the result of the closure (or null if the closure was void).
  49. * @method Carbon fromSerialized($value) Create an instance from a serialized string.
  50. * @method void genericMacro($macro, $priority = 0) Register a custom macro.
  51. * @method array getAvailableLocales() Returns the list of internally available locales and already loaded custom locales.
  52. * (It will ignore custom translator dynamic loading.)
  53. * @method Language[] getAvailableLocalesInfo() Returns list of Language object for each available locale. This object allow you to get the ISO name, native
  54. * name, region and variant of the locale.
  55. * @method array getDays() Get the days of the week
  56. * @method string|null getFallbackLocale() Get the fallback locale.
  57. * @method array getFormatsToIsoReplacements() List of replacements from date() format to isoFormat().
  58. * @method int getHumanDiffOptions() Return default humanDiff() options (merged flags as integer).
  59. * @method array getIsoUnits() Returns list of locale units for ISO formatting.
  60. * @method Carbon getLastErrors() {@inheritdoc}
  61. * @method string getLocale() Get the current translator locale.
  62. * @method int getMidDayAt() get midday/noon hour
  63. * @method Carbon getTestNow() Get the Carbon instance (real or mock) to be returned when a "now"
  64. * instance is created.
  65. * @method string getTranslationMessageWith($translator, string $key, string $locale = null, string $default = null) Returns raw translation message for a given key.
  66. * @method \Symfony\Component\Translation\TranslatorInterface getTranslator() Get the default translator instance in use.
  67. * @method int getWeekEndsAt() Get the last day of week
  68. * @method int getWeekStartsAt() Get the first day of week
  69. * @method array getWeekendDays() Get weekend days
  70. * @method bool hasFormat($date, $format) Checks if the (date)time string is in a given format.
  71. * @method bool hasMacro($name) Checks if macro is registered.
  72. * @method bool hasRelativeKeywords($time) Determine if a time string will produce a relative date.
  73. * @method bool hasTestNow() Determine if there is a valid test instance set. A valid test instance
  74. * is anything that is not null.
  75. * @method Carbon instance($date) Create a Carbon instance from a DateTime one.
  76. * @method bool isImmutable() Returns true if the current class/instance is immutable.
  77. * @method bool isModifiableUnit($unit) Returns true if a property can be changed via setter.
  78. * @method Carbon isMutable()
  79. * @method bool isStrictModeEnabled() Returns true if the strict mode is globally in use, false else.
  80. * (It can be overridden in specific instances.)
  81. * @method bool localeHasDiffOneDayWords($locale) Returns true if the given locale is internally supported and has words for 1-day diff (just now, yesterday, tomorrow).
  82. * Support is considered enabled if the 3 words are translated in the given locale.
  83. * @method bool localeHasDiffSyntax($locale) Returns true if the given locale is internally supported and has diff syntax support (ago, from now, before, after).
  84. * Support is considered enabled if the 4 sentences are translated in the given locale.
  85. * @method bool localeHasDiffTwoDayWords($locale) Returns true if the given locale is internally supported and has words for 2-days diff (before yesterday, after tomorrow).
  86. * Support is considered enabled if the 2 words are translated in the given locale.
  87. * @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).
  88. * Support is considered enabled if the 4 sentences are translated in the given locale.
  89. * @method bool localeHasShortUnits($locale) Returns true if the given locale is internally supported and has short-units support.
  90. * Support is considered enabled if either year, day or hour has a short variant translated.
  91. * @method void macro($name, $macro) Register a custom macro.
  92. * @method Carbon|null make($var) Make a Carbon instance from given variable if possible.
  93. * Always return a new instance. Parse only strings and only these likely to be dates (skip intervals
  94. * and recurrences). Throw an exception for invalid format, but otherwise return null.
  95. * @method Carbon maxValue() Create a Carbon instance for the greatest supported date.
  96. * @method Carbon minValue() Create a Carbon instance for the lowest supported date.
  97. * @method void mixin($mixin) Mix another object into the class.
  98. * @method Carbon now($tz = null) Get a Carbon instance for the current date and time.
  99. * @method Carbon parse($time = null, $tz = null) Create a carbon instance from a string.
  100. * This is an alias for the constructor that allows better fluent syntax
  101. * as it allows you to do Carbon::parse('Monday next week')->fn() rather
  102. * than (new Carbon('Monday next week'))->fn().
  103. * @method Carbon parseFromLocale($time, $locale, $tz = null) Create a carbon instance from a localized string (in French, Japanese, Arabic, etc.).
  104. * @method string pluralUnit(string $unit) Returns standardized plural of a given singular/plural unit name (in English).
  105. * @method Carbon|false rawCreateFromFormat($format, $time, $tz = null) Create a Carbon instance from a specific format.
  106. * @method Carbon rawParse($time = null, $tz = null) Create a carbon instance from a string.
  107. * This is an alias for the constructor that allows better fluent syntax
  108. * as it allows you to do Carbon::parse('Monday next week')->fn() rather
  109. * than (new Carbon('Monday next week'))->fn().
  110. * @method Carbon resetMacros() Remove all macros and generic macros.
  111. * @method void resetMonthsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  112. * You should rather use the ->settings() method.
  113. * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants
  114. * are available for quarters, years, decade, centuries, millennia (singular and plural forms).
  115. * @method void resetToStringFormat() Reset the format used to the default when type juggling a Carbon instance to a string
  116. * @method void resetYearsOverflow() @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  117. * You should rather use the ->settings() method.
  118. * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants
  119. * are available for quarters, years, decade, centuries, millennia (singular and plural forms).
  120. * @method void serializeUsing($callback) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  121. * You should rather transform Carbon object before the serialization.
  122. * JSON serialize all Carbon instances using the given callback.
  123. * @method Carbon setFallbackLocale($locale) Set the fallback locale.
  124. * @method Carbon setHumanDiffOptions($humanDiffOptions) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  125. * You should rather use the ->settings() method.
  126. * @method bool setLocale($locale) Set the current translator locale and indicate if the source locale file exists.
  127. * Pass 'auto' as locale to use closest language from the current LC_TIME locale.
  128. * @method void setMidDayAt($hour) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  129. * You should rather consider mid-day is always 12pm, then if you need to test if it's an other
  130. * hour, test it explicitly:
  131. * $date->format('G') == 13
  132. * or to set explicitly to a given hour:
  133. * $date->setTime(13, 0, 0, 0)
  134. * Set midday/noon hour
  135. * @method Carbon setTestNow($testNow = null) Set a Carbon instance (real or mock) to be returned when a "now"
  136. * instance is created. The provided instance will be returned
  137. * specifically under the following conditions:
  138. * - A call to the static now() method, ex. Carbon::now()
  139. * - When a null (or blank string) is passed to the constructor or parse(), ex. new Carbon(null)
  140. * - When the string "now" is passed to the constructor or parse(), ex. new Carbon('now')
  141. * - When a string containing the desired time is passed to Carbon::parse().
  142. * Note the timezone parameter was left out of the examples above and
  143. * has no affect as the mock value will be returned regardless of its value.
  144. * To clear the test instance call this method using the default
  145. * parameter of null.
  146. * /!\ Use this method for unit tests only.
  147. * @method void setToStringFormat($format) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  148. * You should rather let Carbon object being casted to string with DEFAULT_TO_STRING_FORMAT, and
  149. * use other method or custom format passed to format() method if you need to dump an other string
  150. * format.
  151. * Set the default format used when type juggling a Carbon instance to a string
  152. * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator) Set the default translator instance to use.
  153. * @method Carbon setUtf8($utf8) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  154. * You should rather use UTF-8 language packages on every machine.
  155. * Set if UTF8 will be used for localized date/time.
  156. * @method void setWeekEndsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  157. * Use $weekStartsAt optional parameter instead when using startOfWeek, floorWeek, ceilWeek
  158. * or roundWeek method. You can also use the 'first_day_of_week' locale setting to change the
  159. * start of week according to current locale selected and implicitly the end of week.
  160. * Set the last day of week
  161. * @method void setWeekStartsAt($day) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  162. * Use $weekEndsAt optional parameter instead when using endOfWeek method. You can also use the
  163. * 'first_day_of_week' locale setting to change the start of week according to current locale
  164. * selected and implicitly the end of week.
  165. * Set the first day of week
  166. * @method void setWeekendDays($days) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  167. * You should rather consider week-end is always saturday and sunday, and if you have some custom
  168. * week-end days to handle, give to those days an other name and create a macro for them:
  169. * ```
  170. * Carbon::macro('isDayOff', function ($date) {
  171. * return $date->isSunday() || $date->isMonday();
  172. * });
  173. * Carbon::macro('isNotDayOff', function ($date) {
  174. * return !$date->isDayOff();
  175. * });
  176. * if ($someDate->isDayOff()) ...
  177. * if ($someDate->isNotDayOff()) ...
  178. * // Add 5 not-off days
  179. * $count = 5;
  180. * while ($someDate->isDayOff() || ($count-- > 0)) {
  181. * $someDate->addDay();
  182. * }
  183. * ```
  184. * Set weekend days
  185. * @method bool shouldOverflowMonths() Get the month overflow global behavior (can be overridden in specific instances).
  186. * @method bool shouldOverflowYears() Get the month overflow global behavior (can be overridden in specific instances).
  187. * @method string singularUnit(string $unit) Returns standardized singular of a given singular/plural unit name (in English).
  188. * @method Carbon today($tz = null) Create a Carbon instance for today.
  189. * @method Carbon tomorrow($tz = null) Create a Carbon instance for tomorrow.
  190. * @method string translateTimeString($timeString, $from = null, $to = null, $mode = 15) Translate a time string from a locale to an other.
  191. * @method string translateWith(\Symfony\Component\Translation\TranslatorInterface $translator, string $key, array $parameters = [], $number = null) Translate using translation string or callback available.
  192. * @method void useMonthsOverflow($monthsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  193. * You should rather use the ->settings() method.
  194. * Or you can use method variants: addMonthsWithOverflow/addMonthsNoOverflow, same variants
  195. * are available for quarters, years, decade, centuries, millennia (singular and plural forms).
  196. * @method Carbon useStrictMode($strictModeEnabled = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  197. * You should rather use the ->settings() method.
  198. * @method void useYearsOverflow($yearsOverflow = true) @deprecated To avoid conflict between different third-party libraries, static setters should not be used.
  199. * You should rather use the ->settings() method.
  200. * Or you can use method variants: addYearsWithOverflow/addYearsNoOverflow, same variants
  201. * are available for quarters, years, decade, centuries, millennia (singular and plural forms).
  202. * @method Carbon yesterday($tz = null) Create a Carbon instance for yesterday.
  203. *
  204. * </autodoc>
  205. */
  206. class Factory
  207. {
  208. protected $className = Carbon::class;
  209. protected $settings = [];
  210. public function __construct(array $settings = [], string $className = null)
  211. {
  212. if ($className) {
  213. $this->className = $className;
  214. }
  215. $this->settings = $settings;
  216. }
  217. public function getClassName()
  218. {
  219. return $this->className;
  220. }
  221. public function setClassName(string $className)
  222. {
  223. $this->className = $className;
  224. return $this;
  225. }
  226. public function className(string $className = null)
  227. {
  228. return $className === null ? $this->getClassName() : $this->setClassName($className);
  229. }
  230. public function getSettings()
  231. {
  232. return $this->settings;
  233. }
  234. public function setSettings(array $settings)
  235. {
  236. $this->settings = $settings;
  237. return $this;
  238. }
  239. public function settings(array $settings = null)
  240. {
  241. return $settings === null ? $this->getSettings() : $this->setSettings($settings);
  242. }
  243. public function mergeSettings(array $settings)
  244. {
  245. $this->settings = array_merge($this->settings, $settings);
  246. return $this;
  247. }
  248. public function __call($name, $arguments)
  249. {
  250. $result = $this->className::$name(...$arguments);
  251. return $result instanceof CarbonInterface ? $result->settings($this->settings) : $result;
  252. }
  253. }