Passed
Push — 6.0 ( e6360e...a5ddb0 )
by Olivier
01:38
created

LocalizedDateTime::format_as_medium()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace ICanBoogie\CLDR;
4
5
use DateTime;
6
use DateTimeInterface;
7
use ICanBoogie\PropertyNotDefined;
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(object $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
        if (str_starts_with($property, 'as_')) {
50
            return $this->{'format_' . $property}();
51
        }
52
53
        return $this->target->$property;
54
    }
55
56
    /**
57
     * @param string $property
58
     * @param mixed $value
59
     */
60
    public function __set($property, $value): void
61
    {
62
        $this->target->$property = $value;
63
    }
64
65
    /**
66
     * @param string $method
67
     * @param array<string, mixed> $arguments
68
     *
69
     * @return mixed
70
     *
71
     * @throws \Exception
72
     */
73
    public function __call($method, $arguments)
74
    {
75
        return $this->target->$method(...$arguments);
76
    }
77
78
    public function __toString(): string
79
    {
80
        $target = $this->target;
81
82
        if (method_exists($target, __FUNCTION__)) {
83
            return (string)$target;
84
        }
85
86
        // `ATOM` is used instead of `ISO8601` because of a bug in the pattern
87
        // @see http://php.net/manual/en/class.datetime.php#datetime.constants.iso8601
88
89
        return $this->target->format(DateTime::ATOM);
90
    }
91
92
    /**
93
     * @inheritDoc
94
     *
95
     * @throws \Exception
96
     */
97
    public function format(string|DateTimeFormatLength|DateTimeFormatId $pattern): string
98
    {
99
        return $this->formatter->format($this->target, $pattern);
100
    }
101
102
    /**
103
     * Formats the instance according to the {@link DateTimeFormatLength::FULL} length.
104
     */
105
    public function format_as_full(): string
106
    {
107
        return $this->format(DateTimeFormatLength::FULL);
108
    }
109
110
    /**
111
     * Formats the instance according to the {@link DateTimeFormatLength::LONG} length.
112
     */
113
    public function format_as_long(): string
114
    {
115
        return $this->format(DateTimeFormatLength::LONG);
116
    }
117
118
    /**
119
     * Formats the instance according to the {@link DateTimeFormatLength::MEDIUM} length.
120
     */
121
    public function format_as_medium(): string
122
    {
123
        return $this->format(DateTimeFormatLength::MEDIUM);
124
    }
125
126
    /**
127
     * Formats the instance according to the {@link DateTimeFormatLength::SHORT} length.
128
     */
129
    public function format_as_short(): string
130
    {
131
        return $this->format(DateTimeFormatLength::SHORT);
132
    }
133
}
134