-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChoiceBuilder.php
165 lines (133 loc) · 3.88 KB
/
ChoiceBuilder.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
/*
* This file is part of the Klipper package.
*
* (c) François Pluchino <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Klipper\Component\Metadata;
use Klipper\Component\Metadata\Util\BuilderUtil;
use Symfony\Component\Config\Resource\ResourceInterface;
/**
* @author François Pluchino <[email protected]>
*/
class ChoiceBuilder implements ChoiceBuilderInterface
{
protected string $name;
protected array $listIdentifiers;
protected ?array $values;
protected ?string $translationDomain;
protected ?string $placeholder;
/**
* @var ResourceInterface[]
*/
protected array $resources = [];
/**
* @param string $name The unique name
* @param null|string $translationDomain The translation domain
* @param array $identifiers The list identifiers
* @param array $values The values
* @param null|string $placeholder The placeholder
*/
public function __construct(
string $name,
?string $translationDomain,
array $identifiers,
?array $values = null,
?string $placeholder = null
) {
$this->name = $name;
$this->listIdentifiers = $identifiers;
$this->values = $values;
$this->translationDomain = $translationDomain;
$this->placeholder = $placeholder;
}
public function getName(): string
{
return $this->name;
}
public function getListIdentifiers(): ?array
{
return $this->listIdentifiers;
}
public function setListIdentifiers(array $identifiers)
{
$this->listIdentifiers = $identifiers;
return $this;
}
public function getValues(): ?array
{
return $this->values;
}
public function setValues(?array $values): self
{
$this->values = $values;
return $this;
}
public function getTranslationDomain(): ?string
{
return $this->translationDomain;
}
public function setTranslationDomain(?string $translationDomain): self
{
$this->translationDomain = $translationDomain;
return $this;
}
public function getPlaceholder(): ?string
{
return $this->placeholder;
}
public function setPlaceholder(?string $placeholder): self
{
$this->placeholder = $placeholder;
return $this;
}
public function getResources(): array
{
return array_values($this->resources);
}
public function addResource(ResourceInterface $resource): void
{
$key = (string) $resource;
if (!isset($this->resources[$key])) {
$this->resources[$key] = $resource;
}
}
public function merge(ChoiceBuilderInterface $builder): self
{
BuilderUtil::mergeValues($this, $builder);
return $this;
}
public function build(): ChoiceInterface
{
return new Choice(
$this->getName(),
$this->getListIdentifiers() ?? [],
$this->getValues() ?? self::getFlatValues($this->listIdentifiers),
$this->getTranslationDomain(),
$this->getPlaceholder(),
$this->getResources()
);
}
/**
* Get all values of the grouped choice identifiers keys.
*
* @param array $choiceIdentifiers The choice identifiers
*
* @return string[]
*/
private static function getFlatValues(array $choiceIdentifiers): array
{
$choices = [];
foreach ($choiceIdentifiers as $key => $value) {
if (\is_array($value)) {
$choices = array_merge($choices, self::getFlatValues($value));
} else {
$choices[] = $key;
}
}
return $choices;
}
}