Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/**
4
 * This file is part of the Carbon package.
5
 *
6
 * (c) Brian Nesbitt <brian@nesbot.com>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
 
12
namespace Carbon\Traits;
13
 
14
use Carbon\Carbon;
15
use Carbon\CarbonImmutable;
16
use Carbon\CarbonInterface;
17
use Carbon\CarbonInterval;
18
use Carbon\CarbonPeriod;
19
use Carbon\Exceptions\UnitException;
20
use Closure;
21
use DateTime;
22
use DateTimeImmutable;
23
use ReturnTypeWillChange;
24
 
25
/**
26
 * Trait Converter.
27
 *
28
 * Change date into different string formats and types and
29
 * handle the string cast.
30
 *
31
 * Depends on the following methods:
32
 *
33
 * @method static copy()
34
 */
35
trait Converter
36
{
37
    use ToStringFormat;
38
 
39
    /**
40
     * Returns the formatted date string on success or FALSE on failure.
41
     *
42
     * @see https://php.net/manual/en/datetime.format.php
43
     *
44
     * @param string $format
45
     *
46
     * @return string
47
     */
48
    #[ReturnTypeWillChange]
49
    public function format($format)
50
    {
51
        $function = $this->localFormatFunction ?: static::$formatFunction;
52
 
53
        if (!$function) {
54
            return $this->rawFormat($format);
55
        }
56
 
57
        if (\is_string($function) && method_exists($this, $function)) {
58
            $function = [$this, $function];
59
        }
60
 
61
        return $function(...\func_get_args());
62
    }
63
 
64
    /**
65
     * @see https://php.net/manual/en/datetime.format.php
66
     *
67
     * @param string $format
68
     *
69
     * @return string
70
     */
71
    public function rawFormat($format)
72
    {
73
        return parent::format($format);
74
    }
75
 
76
    /**
77
     * Format the instance as a string using the set format
78
     *
79
     * @example
80
     * ```
81
     * echo Carbon::now(); // Carbon instances can be cast to string
82
     * ```
83
     *
84
     * @return string
85
     */
86
    public function __toString()
87
    {
88
        $format = $this->localToStringFormat ?? static::$toStringFormat;
89
 
90
        return $format instanceof Closure
91
            ? $format($this)
92
            : $this->rawFormat($format ?: (
93
                \defined('static::DEFAULT_TO_STRING_FORMAT')
94
                    ? static::DEFAULT_TO_STRING_FORMAT
95
                    : CarbonInterface::DEFAULT_TO_STRING_FORMAT
96
            ));
97
    }
98
 
99
    /**
100
     * Format the instance as date
101
     *
102
     * @example
103
     * ```
104
     * echo Carbon::now()->toDateString();
105
     * ```
106
     *
107
     * @return string
108
     */
109
    public function toDateString()
110
    {
111
        return $this->rawFormat('Y-m-d');
112
    }
113
 
114
    /**
115
     * Format the instance as a readable date
116
     *
117
     * @example
118
     * ```
119
     * echo Carbon::now()->toFormattedDateString();
120
     * ```
121
     *
122
     * @return string
123
     */
124
    public function toFormattedDateString()
125
    {
126
        return $this->rawFormat('M j, Y');
127
    }
128
 
129
    /**
130
     * Format the instance with the day, and a readable date
131
     *
132
     * @example
133
     * ```
134
     * echo Carbon::now()->toFormattedDayDateString();
135
     * ```
136
     *
137
     * @return string
138
     */
139
    public function toFormattedDayDateString(): string
140
    {
141
        return $this->rawFormat('D, M j, Y');
142
    }
143
 
144
    /**
145
     * Format the instance as time
146
     *
147
     * @example
148
     * ```
149
     * echo Carbon::now()->toTimeString();
150
     * ```
151
     *
152
     * @param string $unitPrecision
153
     *
154
     * @return string
155
     */
156
    public function toTimeString($unitPrecision = 'second')
157
    {
158
        return $this->rawFormat(static::getTimeFormatByPrecision($unitPrecision));
159
    }
160
 
161
    /**
162
     * Format the instance as date and time
163
     *
164
     * @example
165
     * ```
166
     * echo Carbon::now()->toDateTimeString();
167
     * ```
168
     *
169
     * @param string $unitPrecision
170
     *
171
     * @return string
172
     */
173
    public function toDateTimeString($unitPrecision = 'second')
174
    {
175
        return $this->rawFormat('Y-m-d '.static::getTimeFormatByPrecision($unitPrecision));
176
    }
177
 
178
    /**
179
     * Return a format from H:i to H:i:s.u according to given unit precision.
180
     *
181
     * @param string $unitPrecision "minute", "second", "millisecond" or "microsecond"
182
     *
183
     * @return string
184
     */
185
    public static function getTimeFormatByPrecision($unitPrecision)
186
    {
187
        switch (static::singularUnit($unitPrecision)) {
188
            case 'minute':
189
                return 'H:i';
190
            case 'second':
191
                return 'H:i:s';
192
            case 'm':
193
            case 'millisecond':
194
                return 'H:i:s.v';
195
            case 'µ':
196
            case 'microsecond':
197
                return 'H:i:s.u';
198
        }
199
 
200
        throw new UnitException('Precision unit expected among: minute, second, millisecond and microsecond.');
201
    }
202
 
203
    /**
204
     * Format the instance as date and time T-separated with no timezone
205
     *
206
     * @example
207
     * ```
208
     * echo Carbon::now()->toDateTimeLocalString();
209
     * echo "\n";
210
     * echo Carbon::now()->toDateTimeLocalString('minute'); // You can specify precision among: minute, second, millisecond and microsecond
211
     * ```
212
     *
213
     * @param string $unitPrecision
214
     *
215
     * @return string
216
     */
217
    public function toDateTimeLocalString($unitPrecision = 'second')
218
    {
219
        return $this->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision));
220
    }
221
 
222
    /**
223
     * Format the instance with day, date and time
224
     *
225
     * @example
226
     * ```
227
     * echo Carbon::now()->toDayDateTimeString();
228
     * ```
229
     *
230
     * @return string
231
     */
232
    public function toDayDateTimeString()
233
    {
234
        return $this->rawFormat('D, M j, Y g:i A');
235
    }
236
 
237
    /**
238
     * Format the instance as ATOM
239
     *
240
     * @example
241
     * ```
242
     * echo Carbon::now()->toAtomString();
243
     * ```
244
     *
245
     * @return string
246
     */
247
    public function toAtomString()
248
    {
249
        return $this->rawFormat(DateTime::ATOM);
250
    }
251
 
252
    /**
253
     * Format the instance as COOKIE
254
     *
255
     * @example
256
     * ```
257
     * echo Carbon::now()->toCookieString();
258
     * ```
259
     *
260
     * @return string
261
     */
262
    public function toCookieString()
263
    {
264
        return $this->rawFormat(DateTime::COOKIE);
265
    }
266
 
267
    /**
268
     * Format the instance as ISO8601
269
     *
270
     * @example
271
     * ```
272
     * echo Carbon::now()->toIso8601String();
273
     * ```
274
     *
275
     * @return string
276
     */
277
    public function toIso8601String()
278
    {
279
        return $this->toAtomString();
280
    }
281
 
282
    /**
283
     * Format the instance as RFC822
284
     *
285
     * @example
286
     * ```
287
     * echo Carbon::now()->toRfc822String();
288
     * ```
289
     *
290
     * @return string
291
     */
292
    public function toRfc822String()
293
    {
294
        return $this->rawFormat(DateTime::RFC822);
295
    }
296
 
297
    /**
298
     * Convert the instance to UTC and return as Zulu ISO8601
299
     *
300
     * @example
301
     * ```
302
     * echo Carbon::now()->toIso8601ZuluString();
303
     * ```
304
     *
305
     * @param string $unitPrecision
306
     *
307
     * @return string
308
     */
309
    public function toIso8601ZuluString($unitPrecision = 'second')
310
    {
311
        return $this->avoidMutation()
312
            ->utc()
313
            ->rawFormat('Y-m-d\T'.static::getTimeFormatByPrecision($unitPrecision).'\Z');
314
    }
315
 
316
    /**
317
     * Format the instance as RFC850
318
     *
319
     * @example
320
     * ```
321
     * echo Carbon::now()->toRfc850String();
322
     * ```
323
     *
324
     * @return string
325
     */
326
    public function toRfc850String()
327
    {
328
        return $this->rawFormat(DateTime::RFC850);
329
    }
330
 
331
    /**
332
     * Format the instance as RFC1036
333
     *
334
     * @example
335
     * ```
336
     * echo Carbon::now()->toRfc1036String();
337
     * ```
338
     *
339
     * @return string
340
     */
341
    public function toRfc1036String()
342
    {
343
        return $this->rawFormat(DateTime::RFC1036);
344
    }
345
 
346
    /**
347
     * Format the instance as RFC1123
348
     *
349
     * @example
350
     * ```
351
     * echo Carbon::now()->toRfc1123String();
352
     * ```
353
     *
354
     * @return string
355
     */
356
    public function toRfc1123String()
357
    {
358
        return $this->rawFormat(DateTime::RFC1123);
359
    }
360
 
361
    /**
362
     * Format the instance as RFC2822
363
     *
364
     * @example
365
     * ```
366
     * echo Carbon::now()->toRfc2822String();
367
     * ```
368
     *
369
     * @return string
370
     */
371
    public function toRfc2822String()
372
    {
373
        return $this->rawFormat(DateTime::RFC2822);
374
    }
375
 
376
    /**
377
     * Format the instance as RFC3339
378
     *
379
     * @param bool $extended
380
     *
381
     * @example
382
     * ```
383
     * echo Carbon::now()->toRfc3339String() . "\n";
384
     * echo Carbon::now()->toRfc3339String(true) . "\n";
385
     * ```
386
     *
387
     * @return string
388
     */
389
    public function toRfc3339String($extended = false)
390
    {
391
        $format = DateTime::RFC3339;
392
        if ($extended) {
393
            $format = DateTime::RFC3339_EXTENDED;
394
        }
395
 
396
        return $this->rawFormat($format);
397
    }
398
 
399
    /**
400
     * Format the instance as RSS
401
     *
402
     * @example
403
     * ```
404
     * echo Carbon::now()->toRssString();
405
     * ```
406
     *
407
     * @return string
408
     */
409
    public function toRssString()
410
    {
411
        return $this->rawFormat(DateTime::RSS);
412
    }
413
 
414
    /**
415
     * Format the instance as W3C
416
     *
417
     * @example
418
     * ```
419
     * echo Carbon::now()->toW3cString();
420
     * ```
421
     *
422
     * @return string
423
     */
424
    public function toW3cString()
425
    {
426
        return $this->rawFormat(DateTime::W3C);
427
    }
428
 
429
    /**
430
     * Format the instance as RFC7231
431
     *
432
     * @example
433
     * ```
434
     * echo Carbon::now()->toRfc7231String();
435
     * ```
436
     *
437
     * @return string
438
     */
439
    public function toRfc7231String()
440
    {
441
        return $this->avoidMutation()
442
            ->setTimezone('GMT')
443
            ->rawFormat(\defined('static::RFC7231_FORMAT') ? static::RFC7231_FORMAT : CarbonInterface::RFC7231_FORMAT);
444
    }
445
 
446
    /**
447
     * Get default array representation.
448
     *
449
     * @example
450
     * ```
451
     * var_dump(Carbon::now()->toArray());
452
     * ```
453
     *
454
     * @return array
455
     */
456
    public function toArray()
457
    {
458
        return [
459
            'year' => $this->year,
460
            'month' => $this->month,
461
            'day' => $this->day,
462
            'dayOfWeek' => $this->dayOfWeek,
463
            'dayOfYear' => $this->dayOfYear,
464
            'hour' => $this->hour,
465
            'minute' => $this->minute,
466
            'second' => $this->second,
467
            'micro' => $this->micro,
468
            'timestamp' => $this->timestamp,
469
            'formatted' => $this->rawFormat(\defined('static::DEFAULT_TO_STRING_FORMAT') ? static::DEFAULT_TO_STRING_FORMAT : CarbonInterface::DEFAULT_TO_STRING_FORMAT),
470
            'timezone' => $this->timezone,
471
        ];
472
    }
473
 
474
    /**
475
     * Get default object representation.
476
     *
477
     * @example
478
     * ```
479
     * var_dump(Carbon::now()->toObject());
480
     * ```
481
     *
482
     * @return object
483
     */
484
    public function toObject()
485
    {
486
        return (object) $this->toArray();
487
    }
488
 
489
    /**
490
     * Returns english human readable complete date string.
491
     *
492
     * @example
493
     * ```
494
     * echo Carbon::now()->toString();
495
     * ```
496
     *
497
     * @return string
498
     */
499
    public function toString()
500
    {
501
        return $this->avoidMutation()->locale('en')->isoFormat('ddd MMM DD YYYY HH:mm:ss [GMT]ZZ');
502
    }
503
 
504
    /**
505
     * Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z, if $keepOffset truthy, offset will be kept:
506
     * 1977-04-22T01:00:00-05:00).
507
     *
508
     * @example
509
     * ```
510
     * echo Carbon::now('America/Toronto')->toISOString() . "\n";
511
     * echo Carbon::now('America/Toronto')->toISOString(true) . "\n";
512
     * ```
513
     *
514
     * @param bool $keepOffset Pass true to keep the date offset. Else forced to UTC.
515
     *
516
     * @return null|string
517
     */
518
    public function toISOString($keepOffset = false)
519
    {
520
        if (!$this->isValid()) {
521
            return null;
522
        }
523
 
524
        $yearFormat = $this->year < 0 || $this->year > 9999 ? 'YYYYYY' : 'YYYY';
525
        $tzFormat = $keepOffset ? 'Z' : '[Z]';
526
        $date = $keepOffset ? $this : $this->avoidMutation()->utc();
527
 
528
        return $date->isoFormat("$yearFormat-MM-DD[T]HH:mm:ss.SSSSSS$tzFormat");
529
    }
530
 
531
    /**
532
     * Return the ISO-8601 string (ex: 1977-04-22T06:00:00Z) with UTC timezone.
533
     *
534
     * @example
535
     * ```
536
     * echo Carbon::now('America/Toronto')->toJSON();
537
     * ```
538
     *
539
     * @return null|string
540
     */
541
    public function toJSON()
542
    {
543
        return $this->toISOString();
544
    }
545
 
546
    /**
547
     * Return native DateTime PHP object matching the current instance.
548
     *
549
     * @example
550
     * ```
551
     * var_dump(Carbon::now()->toDateTime());
552
     * ```
553
     *
554
     * @return DateTime
555
     */
556
    public function toDateTime()
557
    {
558
        return new DateTime($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone());
559
    }
560
 
561
    /**
562
     * Return native toDateTimeImmutable PHP object matching the current instance.
563
     *
564
     * @example
565
     * ```
566
     * var_dump(Carbon::now()->toDateTimeImmutable());
567
     * ```
568
     *
569
     * @return DateTimeImmutable
570
     */
571
    public function toDateTimeImmutable()
572
    {
573
        return new DateTimeImmutable($this->rawFormat('Y-m-d H:i:s.u'), $this->getTimezone());
574
    }
575
 
576
    /**
577
     * @alias toDateTime
578
     *
579
     * Return native DateTime PHP object matching the current instance.
580
     *
581
     * @example
582
     * ```
583
     * var_dump(Carbon::now()->toDate());
584
     * ```
585
     *
586
     * @return DateTime
587
     */
588
    public function toDate()
589
    {
590
        return $this->toDateTime();
591
    }
592
 
593
    /**
594
     * Create a iterable CarbonPeriod object from current date to a given end date (and optional interval).
595
     *
596
     * @param \DateTimeInterface|Carbon|CarbonImmutable|int|null $end      period end date or recurrences count if int
597
     * @param int|\DateInterval|string|null                      $interval period default interval or number of the given $unit
598
     * @param string|null                                        $unit     if specified, $interval must be an integer
599
     *
600
     * @return CarbonPeriod
601
     */
602
    public function toPeriod($end = null, $interval = null, $unit = null)
603
    {
604
        if ($unit) {
605
            $interval = CarbonInterval::make("$interval ".static::pluralUnit($unit));
606
        }
607
 
608
        $period = (new CarbonPeriod())->setDateClass(static::class)->setStartDate($this);
609
 
610
        if ($interval) {
611
            $period->setDateInterval($interval);
612
        }
613
 
614
        if (\is_int($end) || (\is_string($end) && ctype_digit($end))) {
615
            $period->setRecurrences($end);
616
        } elseif ($end) {
617
            $period->setEndDate($end);
618
        }
619
 
620
        return $period;
621
    }
622
 
623
    /**
624
     * Create a iterable CarbonPeriod object from current date to a given end date (and optional interval).
625
     *
626
     * @param \DateTimeInterface|Carbon|CarbonImmutable|null $end      period end date
627
     * @param int|\DateInterval|string|null                  $interval period default interval or number of the given $unit
628
     * @param string|null                                    $unit     if specified, $interval must be an integer
629
     *
630
     * @return CarbonPeriod
631
     */
632
    public function range($end = null, $interval = null, $unit = null)
633
    {
634
        return $this->toPeriod($end, $interval, $unit);
635
    }
636
}