LocalizedDateTime   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 14
dl 0
loc 71
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A format_as_full() 0 3 1
A format_as_short() 0 3 1
A __toString() 0 6 1
A format_as_medium() 0 3 1
A __get() 0 8 1
A format_as_long() 0 3 1
A format() 0 3 1
A __construct() 0 3 1
1
<?php
2
3
namespace ICanBoogie\CLDR\Dates;
4
5
use DateTimeInterface;
6
use ICanBoogie\CLDR\Core\Locale;
7
use ICanBoogie\CLDR\Core\LocalizedObjectWithFormatter;
8
9
/**
10
 * A localized date time.
11
 *
12
 * <pre>
13
 * <?php
14
 *
15
 * namespace ICanBoogie\CLDR;
16
 *
17
 * $ldt = new LocalizedDateTime(new \DateTime('2013-11-04 20:21:22 UTC'), $repository->locales['fr']);
18
 *
19
 * echo $ldt->as_full;          // lundi 4 novembre 2013 20:21:22 UTC
20
 * # or
21
 * echo $ldt->format_as_full(); // lundi 4 novembre 2013 20:21:22 UTC
22
 *
23
 * echo $ldt->as_long;          // 4 novembre 2013 20:21:22 UTC
24
 * echo $ldt->as_medium;        // 4 nov. 2013 20:21:22
25
 * echo $ldt->as_short;         // 04/11/2013 20:21
26
 * </pre>
27
 *
28
 * @extends LocalizedObjectWithFormatter<DateTimeInterface, DateTimeFormatter>
29
 *
30
 * @property-read string $as_full
31
 * @property-read string $as_long
32
 * @property-read string $as_medium
33
 * @property-read string $as_short
34
 */
35
final class LocalizedDateTime extends LocalizedObjectWithFormatter
36
{
37
    public function __construct(DateTimeInterface $target, Locale $locale)
38
    {
39
        parent::__construct($target, $locale, $locale->calendar->datetime_formatter);
40
    }
41
42
    /**
43
     * @param string $property
44
     *
45
     * @return mixed
46
     */
47
    public function __get($property)
48
    {
49
        return match ($property) {
50
            'as_full' => $this->format_as_full(),
51
            'as_long' => $this->format_as_long(),
52
            'as_medium' => $this->format_as_medium(),
53
            'as_short' => $this->format_as_short(),
54
            default => $this->target->$property,
55
        };
56
    }
57
58
    public function __toString(): string
59
    {
60
        // `ATOM` is used instead of `ISO8601` because of a bug in the pattern
61
        // @link https://php.net/manual/en/class.datetime.php#datetime.constants.iso8601
62
63
        return $this->target->format(DateTimeInterface::ATOM);
64
    }
65
66
    /**
67
     * @see DateTimeFormatter::format()
68
     *
69
     * @throws \Exception
70
     */
71
    public function format(string|DateTimeFormatLength|DateTimeFormatId $pattern_or_length_or_id): string
72
    {
73
        return $this->formatter->format($this->target, $pattern_or_length_or_id);
0 ignored issues
show
Bug introduced by
The method format() does not exist on ICanBoogie\CLDR\Core\Formatter. Since it exists in all sub-types, consider adding an abstract or default implementation to ICanBoogie\CLDR\Core\Formatter. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

73
        return $this->formatter->/** @scrutinizer ignore-call */ format($this->target, $pattern_or_length_or_id);
Loading history...
74
    }
75
76
    /**
77
     * Formats the instance according to the {@see DateTimeFormatLength::FULL} length.
78
     */
79
    public function format_as_full(): string
80
    {
81
        return $this->format(DateTimeFormatLength::FULL);
82
    }
83
84
    /**
85
     * Formats the instance according to the {@see DateTimeFormatLength::LONG} length.
86
     */
87
    public function format_as_long(): string
88
    {
89
        return $this->format(DateTimeFormatLength::LONG);
90
    }
91
92
    /**
93
     * Formats the instance according to the {@see DateTimeFormatLength::MEDIUM} length.
94
     */
95
    public function format_as_medium(): string
96
    {
97
        return $this->format(DateTimeFormatLength::MEDIUM);
98
    }
99
100
    /**
101
     * Formats the instance according to the {@see DateTimeFormatLength::SHORT} length.
102
     */
103
    public function format_as_short(): string
104
    {
105
        return $this->format(DateTimeFormatLength::SHORT);
106
    }
107
}
108