/
Type.php
311 lines (283 loc) · 10.3 KB
/
Type.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
<?php
/**
* @copyright Copyright (C) eZ Systems AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
namespace eZ\Publish\Core\FieldType\Selection;
use eZ\Publish\Core\FieldType\FieldType;
use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
use eZ\Publish\Core\FieldType\ValidationError;
use eZ\Publish\SPI\FieldType\Value as SPIValue;
use eZ\Publish\Core\FieldType\Value as BaseValue;
/**
* The Selection field type.
*
* This field type represents a simple string.
*/
class Type extends FieldType
{
/**
* The setting keys which are available on this field type.
*
* The key is the setting name, and the value is the default value for given
* setting, set to null if no particular default should be set.
*
* @var mixed
*/
protected $settingsSchema = [
'isMultiple' => [
'type' => 'bool',
'default' => false,
],
'options' => [
'type' => 'hash',
'default' => [],
],
'multilingualOptions' => [
'type' => 'hash',
'default' => [],
],
];
/**
* Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
*
* @param mixed $fieldSettings
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validateFieldSettings($fieldSettings)
{
$validationErrors = [];
foreach ($fieldSettings as $settingKey => $settingValue) {
switch ($settingKey) {
case 'isMultiple':
if (!is_bool($settingValue)) {
$validationErrors[] = new ValidationError(
"FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
null,
[
'%fieldType%' => $this->getFieldTypeIdentifier(),
'%setting%' => $settingKey,
'%type%' => 'bool',
],
"[$settingKey]"
);
}
break;
case 'options':
if (!is_array($settingValue)) {
$validationErrors[] = new ValidationError(
"FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
null,
[
'%fieldType%' => $this->getFieldTypeIdentifier(),
'%setting%' => $settingKey,
'%type%' => 'hash',
],
"[$settingKey]"
);
}
break;
case 'multilingualOptions':
if (!is_array($settingValue) && !is_array(reset($settingValue))) {
$validationErrors[] = new ValidationError(
"FieldType '%fieldType%' expects setting '%setting%' to be of type '%type%'",
null,
[
'%fieldType%' => $this->getFieldTypeIdentifier(),
'%setting%' => $settingKey,
'%type%' => 'hash',
],
"[$settingKey]"
);
}
break;
default:
$validationErrors[] = new ValidationError(
"Setting '%setting%' is unknown",
null,
[
'%setting%' => $settingKey,
],
"[$settingKey]"
);
}
}
return $validationErrors;
}
/**
* Returns the field type identifier for this field type.
*
* @return string
*/
public function getFieldTypeIdentifier()
{
return 'ezselection';
}
/**
* @param \eZ\Publish\Core\FieldType\Selection\Value|\eZ\Publish\SPI\FieldType\Value $value
*/
public function getName(SPIValue $value, FieldDefinition $fieldDefinition, string $languageCode): string
{
if (empty($value->selection)) {
return '';
}
$names = [];
$fieldSettings = $fieldDefinition->getFieldSettings();
foreach ($value->selection as $optionIndex) {
if (isset($fieldSettings['multilingualOptions'][$languageCode][$optionIndex])) {
$names[] = $fieldSettings['multilingualOptions'][$languageCode][$optionIndex];
} elseif (isset($fieldSettings['multilingualOptions'][$fieldDefinition->mainLanguageCode][$optionIndex])) {
$names[] = $fieldSettings['multilingualOptions'][$fieldDefinition->mainLanguageCode][$optionIndex];
} elseif (isset($fieldSettings['options'][$optionIndex])) {
$names[] = $fieldSettings['options'][$optionIndex];
}
}
return implode(' ', $names);
}
/**
* Returns the fallback default value of field type when no such default
* value is provided in the field definition in content types.
*
* @return \eZ\Publish\Core\FieldType\Selection\Value
*/
public function getEmptyValue()
{
return new Value();
}
/**
* Inspects given $inputValue and potentially converts it into a dedicated value object.
*
* @param array|\eZ\Publish\Core\FieldType\Selection\Value $inputValue
*
* @return \eZ\Publish\Core\FieldType\Selection\Value The potentially converted and structurally plausible value.
*/
protected function createValueFromInput($inputValue)
{
if (is_array($inputValue)) {
$inputValue = new Value($inputValue);
}
return $inputValue;
}
/**
* Throws an exception if value structure is not of expected format.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException If the value does not match the expected structure.
*
* @param \eZ\Publish\Core\FieldType\Selection\Value $value
*/
protected function checkValueStructure(BaseValue $value)
{
if (!is_array($value->selection)) {
throw new InvalidArgumentType(
'$value->selection',
'array',
$value->selection
);
}
}
/**
* Validates field value against 'isMultiple' and 'options' settings.
*
* Does not use validators.
*
* @throws \eZ\Publish\API\Repository\Exceptions\InvalidArgumentException
*
* @param \eZ\Publish\API\Repository\Values\ContentType\FieldDefinition $fieldDefinition The field definition of the field
* @param \eZ\Publish\Core\FieldType\Selection\Value $fieldValue The field value for which an action is performed
*
* @return \eZ\Publish\SPI\FieldType\ValidationError[]
*/
public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
{
$validationErrors = [];
if ($this->isEmptyValue($fieldValue)) {
return $validationErrors;
}
$fieldSettings = $fieldDefinition->getFieldSettings();
if ((!isset($fieldSettings['isMultiple']) || $fieldSettings['isMultiple'] === false)
&& count($fieldValue->selection) > 1) {
$validationErrors[] = new ValidationError(
'Field definition does not allow multiple options to be selected.',
null,
[],
'selection'
);
}
foreach ($fieldValue->selection as $optionIndex) {
if (!isset($fieldSettings['options'][$optionIndex]) && empty($fieldSettings['multilingualOptions'])) {
$validationErrors[] = new ValidationError(
'Option with index %index% does not exist in the field definition.',
null,
[
'%index%' => $optionIndex,
],
'selection'
);
}
}
//@todo: find a way to include selection language
if (isset($fieldSettings['multilingualOptions'])) {
$possibleOptionIndexesByLanguage = array_map(function ($languageOptionIndexes) {
return array_keys($languageOptionIndexes);
}, $fieldSettings['multilingualOptions']);
$possibleOptionIndexes = call_user_func_array('array_merge', $possibleOptionIndexesByLanguage);
foreach ($fieldValue->selection as $optionIndex) {
if (!in_array($optionIndex, $possibleOptionIndexes)) {
$validationErrors[] = new ValidationError(
'Option with index %index% does not exist in the field definition.',
null,
[
'%index%' => $optionIndex,
],
'selection'
);
}
}
}
return $validationErrors;
}
/**
* Returns information for FieldValue->$sortKey relevant to the field type.
*
* @param \eZ\Publish\Core\FieldType\Selection\Value $value
*
* @return string
*/
protected function getSortInfo(BaseValue $value)
{
return implode('-', $value->selection);
}
/**
* Converts an $hash to the Value defined by the field type.
*
* @param mixed $hash
*
* @return \eZ\Publish\Core\FieldType\Selection\Value $value
*/
public function fromHash($hash)
{
return new Value($hash);
}
/**
* Converts a $Value to a hash.
*
* @param \eZ\Publish\Core\FieldType\Selection\Value $value
*
* @return mixed
*/
public function toHash(SPIValue $value)
{
return $value->selection;
}
/**
* Returns whether the field type is searchable.
*
* @return bool
*/
public function isSearchable()
{
return true;
}
}