Subversion-Projekte lars-tiefland.laravel_shop

Revision

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