|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Eclipxe\Enum\Internal; |
|
6
|
|
|
|
|
7
|
|
|
use ArrayObject; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* This is a collection of Entry elements |
|
11
|
|
|
* |
|
12
|
|
|
* This is an internal class, do not use it by your own. Changes on this class are not library breaking changes. |
|
13
|
|
|
* @internal |
|
14
|
|
|
*/ |
|
15
|
|
|
class Entries |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var ArrayObject<Entry> */ |
|
18
|
|
|
private $entries; |
|
19
|
|
|
|
|
20
|
5 |
|
public function __construct() |
|
21
|
|
|
{ |
|
22
|
5 |
|
$this->entries = new ArrayObject(); |
|
23
|
5 |
|
} |
|
24
|
|
|
|
|
25
|
4 |
|
public function toIndexValueArray(): array |
|
26
|
|
|
{ |
|
27
|
4 |
|
$mixed = []; |
|
28
|
4 |
|
foreach ($this->entries as $entry) { |
|
29
|
4 |
|
$mixed[$entry->index()] = $entry->value(); |
|
30
|
|
|
} |
|
31
|
4 |
|
return $mixed; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
5 |
|
public function hasName(string $name): bool |
|
35
|
|
|
{ |
|
36
|
5 |
|
return isset($this->entries[$this->normalizeName($name)]); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
5 |
|
public function put(string $name, Entry $entry): void |
|
40
|
|
|
{ |
|
41
|
5 |
|
$this->entries[$this->normalizeName($name)] = $entry; |
|
42
|
5 |
|
} |
|
43
|
|
|
|
|
44
|
5 |
|
public function append(self $entries): void |
|
45
|
|
|
{ |
|
46
|
|
|
// access to private property since it has no sense to expose it to the outside |
|
47
|
|
|
/** |
|
48
|
|
|
* @var string $name |
|
49
|
|
|
* @var Entry $entry |
|
50
|
|
|
*/ |
|
51
|
5 |
|
foreach ($entries->entries as $name => $entry) { |
|
52
|
1 |
|
$this->entries[$name] = $entry; |
|
53
|
|
|
} |
|
54
|
5 |
|
} |
|
55
|
|
|
|
|
56
|
15 |
|
public function findEntryByName(string $name): ?Entry |
|
57
|
|
|
{ |
|
58
|
15 |
|
return $this->entries[$this->normalizeName($name)] ?? null; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
8 |
|
public function findEntryByValue(string $value): ?Entry |
|
62
|
|
|
{ |
|
63
|
8 |
|
foreach ($this->entries as $entry) { |
|
64
|
8 |
|
if ($entry->equalValue($value)) { |
|
65
|
4 |
|
return $entry; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
6 |
|
return null; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
18 |
|
public function findEntryByIndex(int $index): ?Entry |
|
72
|
|
|
{ |
|
73
|
18 |
|
foreach ($this->entries as $entry) { |
|
74
|
17 |
|
if ($entry->equalIndex($index)) { |
|
75
|
15 |
|
return $entry; |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
6 |
|
return null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
5 |
|
public function indices(): array |
|
82
|
|
|
{ |
|
83
|
5 |
|
$indices = []; |
|
84
|
5 |
|
foreach ($this->entries as $entry) { |
|
85
|
4 |
|
$indices[] = $entry->index(); |
|
86
|
|
|
} |
|
87
|
5 |
|
return $indices; |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
5 |
|
public function nextIndex(): int |
|
91
|
|
|
{ |
|
92
|
5 |
|
$indices = $this->indices(); |
|
93
|
5 |
|
if ([] === $indices) { |
|
94
|
4 |
|
return 0; |
|
95
|
|
|
} |
|
96
|
4 |
|
return max($indices) + 1; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
18 |
|
protected function normalizeName(string $name): string |
|
100
|
|
|
{ |
|
101
|
18 |
|
return strtolower($name); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|