Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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 \Illuminate\Support\Carbon create($year = 0, $month = 1, $day = 1, $hour = 0, $minute = 0, $second = 0, $tz = null)
13
 * @method \Illuminate\Support\Carbon createFromDate($year = null, $month = null, $day = null, $tz = null)
14
 * @method \Illuminate\Support\Carbon|false createFromFormat($format, $time, $tz = null)
15
 * @method \Illuminate\Support\Carbon createFromTime($hour = 0, $minute = 0, $second = 0, $tz = null)
16
 * @method \Illuminate\Support\Carbon createFromTimeString($time, $tz = null)
17
 * @method \Illuminate\Support\Carbon createFromTimestamp($timestamp, $tz = null)
18
 * @method \Illuminate\Support\Carbon createFromTimestampMs($timestamp, $tz = null)
19
 * @method \Illuminate\Support\Carbon createFromTimestampUTC($timestamp)
20
 * @method \Illuminate\Support\Carbon createMidnightDate($year = null, $month = null, $day = null, $tz = null)
21
 * @method \Illuminate\Support\Carbon|false createSafe($year = null, $month = null, $day = null, $hour = null, $minute = null, $second = null, $tz = null)
22
 * @method void disableHumanDiffOption($humanDiffOption)
23
 * @method void enableHumanDiffOption($humanDiffOption)
24
 * @method mixed executeWithLocale($locale, $func)
25
 * @method \Illuminate\Support\Carbon fromSerialized($value)
26
 * @method array getAvailableLocales()
27
 * @method array getDays()
28
 * @method int getHumanDiffOptions()
29
 * @method array getIsoUnits()
30
 * @method array getLastErrors()
31
 * @method string getLocale()
32
 * @method int getMidDayAt()
33
 * @method \Illuminate\Support\Carbon|null getTestNow()
34
 * @method \Symfony\Component\Translation\TranslatorInterface getTranslator()
35
 * @method int getWeekEndsAt()
36
 * @method int getWeekStartsAt()
37
 * @method array getWeekendDays()
38
 * @method bool hasFormat($date, $format)
39
 * @method bool hasMacro($name)
40
 * @method bool hasRelativeKeywords($time)
41
 * @method bool hasTestNow()
42
 * @method \Illuminate\Support\Carbon instance($date)
43
 * @method bool isImmutable()
44
 * @method bool isModifiableUnit($unit)
45
 * @method bool isMutable()
46
 * @method bool isStrictModeEnabled()
47
 * @method bool localeHasDiffOneDayWords($locale)
48
 * @method bool localeHasDiffSyntax($locale)
49
 * @method bool localeHasDiffTwoDayWords($locale)
50
 * @method bool localeHasPeriodSyntax($locale)
51
 * @method bool localeHasShortUnits($locale)
52
 * @method void macro($name, $macro)
53
 * @method \Illuminate\Support\Carbon|null make($var)
54
 * @method \Illuminate\Support\Carbon maxValue()
55
 * @method \Illuminate\Support\Carbon minValue()
56
 * @method void mixin($mixin)
57
 * @method \Illuminate\Support\Carbon now($tz = null)
58
 * @method \Illuminate\Support\Carbon parse($time = null, $tz = null)
59
 * @method string pluralUnit(string $unit)
60
 * @method void resetMonthsOverflow()
61
 * @method void resetToStringFormat()
62
 * @method void resetYearsOverflow()
63
 * @method void serializeUsing($callback)
64
 * @method void setHumanDiffOptions($humanDiffOptions)
65
 * @method bool setLocale($locale)
66
 * @method void setMidDayAt($hour)
67
 * @method void setTestNow($testNow = null)
68
 * @method void setToStringFormat($format)
69
 * @method void setTranslator(\Symfony\Component\Translation\TranslatorInterface $translator)
70
 * @method void setUtf8($utf8)
71
 * @method void setWeekEndsAt($day)
72
 * @method void setWeekStartsAt($day)
73
 * @method void setWeekendDays($days)
74
 * @method bool shouldOverflowMonths()
75
 * @method bool shouldOverflowYears()
76
 * @method string singularUnit(string $unit)
77
 * @method \Illuminate\Support\Carbon today($tz = null)
78
 * @method \Illuminate\Support\Carbon tomorrow($tz = null)
79
 * @method void useMonthsOverflow($monthsOverflow = true)
80
 * @method void useStrictMode($strictModeEnabled = true)
81
 * @method void useYearsOverflow($yearsOverflow = true)
82
 * @method \Illuminate\Support\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 the date can be created using the 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
}