-
Notifications
You must be signed in to change notification settings - Fork 1
/
EnumTrait.php
145 lines (117 loc) · 3.05 KB
/
EnumTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace HillValley\Fluxcap\Base;
use HillValley\Fluxcap\Exception\InvalidPartException;
use function count;
/**
* @internal
* @psalm-immutable
*/
trait EnumTrait
{
private readonly int $index;
private function __construct(int $index)
{
if ($index < 1 || $index > count(self::NAMES)) {
throw InvalidPartException::create(sprintf('Index must be between 1 (%s) and %d (%s), got "%d".', self::NAMES[1], $count = count(self::NAMES), self::NAMES[$count], $index));
}
$this->index = $index;
}
/** @psalm-pure */
public static function get(int $index): self
{
/**
* @var array<int, self> $instances
* @psalm-suppress ImpureStaticVariable
*/
static $instances = [];
if (!isset($instances[$index])) {
$instances[$index] = new self($index);
}
return $instances[$index];
}
/**
* @return array<int, self>
* @psalm-pure
*/
public static function all(): array
{
$all = [];
foreach (self::NAMES as $index => $_) {
$all[$index] = self::get($index);
}
return $all;
}
public function __toString(): string
{
return $this->getName();
}
public function getIndex(): int
{
return $this->index;
}
public function getName(): string
{
return self::NAMES[$this->index];
}
public function getAbbreviation(): string
{
return self::ABBREVIATIONS[$this->index];
}
public function equals(self $other): bool
{
return $this->index === $other->index;
}
public function diffToPrev(self $prev): int
{
if ($prev->index < $this->index) {
return $this->index - $prev->index;
}
return count(self::NAMES) - $prev->index + $this->index;
}
public function diffToNext(self $next): int
{
if ($next->index > $this->index) {
return $next->index - $this->index;
}
return count(self::NAMES) - $this->index + $next->index;
}
/**
* @param array{index: int} $data
*/
public static function __set_state(array $data): self
{
return self::get($data['index']);
}
public function jsonSerialize(): int
{
return $this->index;
}
public function __serialize(): array
{
return ['index' => $this->index];
}
/**
* @param array{index: int} $data
*/
public function __unserialize(array $data): void
{
$this->index = $data['index'];
}
public function __debugInfo(): array
{
return [
'index' => $this->index,
'name' => $this->getName(),
'abbreviation' => $this->getAbbreviation(),
];
}
private function formatIntl(string $format): string
{
return IntlFormatter::formatTimestamp($this->toTimestamp(), $format);
}
private function toTimestamp(): int
{
return strtotime(self::NAMES[$this->index]);
}
}