Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

Core/FieldType/Country/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Country;
8
9 use eZ\Publish\Core\FieldType\FieldType;
10 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
11 use eZ\Publish\Core\FieldType\Country\Exception\InvalidValue;
12 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
13 use eZ\Publish\Core\FieldType\ValidationError;
14 use eZ\Publish\SPI\FieldType\Value as SPIValue;
15 use eZ\Publish\Core\FieldType\Value as BaseValue;
16
22 class Type extends FieldType
23 {
24 protected $settingsSchema = [
25 'isMultiple' => [
26 'type' => 'boolean',
27 'default' => false,
28 ],
29 ];
30
32 protected $countriesInfo;
33
37 public function __construct(array $countriesInfo)
38 {
39 $this->countriesInfo = $countriesInfo;
40 }
41
47 public function getFieldTypeIdentifier()
48 {
49 return 'ezcountry';
50 }
51
62 public function getName(SPIValue $value)
63 {
64 return (string)$value;
65 }
66
73 public function getEmptyValue()
74 {
75 return new Value();
76 }
77
85 protected function createValueFromInput($inputValue)
86 {
87 if (is_array($inputValue)) {
88 $inputValue = $this->fromHash($inputValue);
89 }
90
91 return $inputValue;
92 }
93
101 protected function checkValueStructure(BaseValue $value)
102 {
103 if (!is_array($value->countries)) {
104 throw new InvalidArgumentType(
105 '$value->countries',
106 'array',
107 $value->countries
108 );
109 }
110 }
111
124 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
125 {
126 $validationErrors = [];
127
128 if ($this->isEmptyValue($fieldValue)) {
129 return $validationErrors;
130 }
131
132 $fieldSettings = $fieldDefinition->getFieldSettings();
133
134 if ((!isset($fieldSettings['isMultiple']) || $fieldSettings['isMultiple'] === false)
135 && count($fieldValue->countries) > 1) {
136 $validationErrors[] = new ValidationError(
137 'Field definition does not allow multiple countries to be selected.',
138 null,
139 [],
140 'countries'
141 );
142 }
143
144 foreach ($fieldValue->countries as $alpha2 => $countryInfo) {
145 if (!isset($this->countriesInfo[$alpha2])) {
146 $validationErrors[] = new ValidationError(
147 "Country with Alpha2 code '%alpha2%' is not defined in FieldType settings.",
148 null,
149 [
150 '%alpha2%' => $alpha2,
151 ],
152 'countries'
153 );
154 }
155 }
156
157 return $validationErrors;
158 }
159
163 protected function getSortInfo(BaseValue $value)
164 {
165 $countries = [];
166 foreach ($value->countries as $countryInfo) {
167 $countries[] = $this->transformationProcessor->transformByGroup($countryInfo['Name'], 'lowercase');
168 }
169
170 sort($countries);
171
172 return implode(',', $countries);
173 }
174
182 public function fromHash($hash)
183 {
184 if ($hash === null) {
185 return $this->getEmptyValue();
186 }
187
188 $countries = [];
189 foreach ($hash as $country) {
190 foreach ($this->countriesInfo as $countryInfo) {
191 switch ($country) {
192 case $countryInfo['Name']:
193 case $countryInfo['Alpha2']:
194 case $countryInfo['Alpha3']:
195 $countries[$countryInfo['Alpha2']] = $countryInfo;
196 continue 3;
197 }
198 }
199
200 throw new InvalidValue($country);
201 }
202
203 return new Value($countries);
204 }
205
213 public function toHash(SPIValue $value)
214 {
215 if ($this->isEmptyValue($value)) {
216 return null;
217 }
218
219 return array_keys($value->countries);
220 }
221
227 public function isSearchable()
228 {
229 return true;
230 }
231
239 public function validateFieldSettings($fieldSettings)
240 {
241 $validationErrors = [];
242
243 foreach ($fieldSettings as $name => $value) {
244 if (!isset($this->settingsSchema[$name])) {
245 $validationErrors[] = new ValidationError(
246 "Setting '%setting%' is unknown",
247 null,
248 [
249 '%setting%' => $name,
250 ],
251 "[$name]"
252 );
253 continue;
254 }
255
256 switch ($name) {
257 case 'isMultiple':
258 if (!is_bool($value)) {
259 $validationErrors[] = new ValidationError(
260 "Setting '%setting%' value must be of boolean type",
261 null,
262 [
263 '%setting%' => $name,
264 ],
265 "[$name]"
266 );
267 }
268 break;
269 }
270 }
271
272 return $validationErrors;
273 }
274 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition\getFieldSettings
getFieldSettings()
This method returns settings for the field definition supported by the field type.
eZ\Publish\API\Repository\Values\Content\Query\Criterion\Value
Struct that stores extra value information for a Criterion object.
Definition: API/Repository/Values/Content/Query/Criterion/Value.php:13
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
eZ\Publish\Core\FieldType\Country\Exception\InvalidValue
Exception thrown if an invalid identifier is used for a country.
Definition: InvalidValue.php:15
eZ\Publish\Core\FieldType\Country\Type
The Country field type.
Definition: Core/FieldType/Country/Type.php:23
eZ\Publish\Core\FieldType\Country\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates field value against 'isMultiple' setting.
Definition: Core/FieldType/Country/Type.php:124
eZ\Publish\Core\FieldType\Country\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Country/Type.php:101
eZ\Publish\Core\FieldType\Country\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Country/Type.php:62
eZ\Publish\Core\FieldType\Country\Type\$countriesInfo
$countriesInfo
Definition: Core/FieldType/Country/Type.php:32
eZ\Publish\Core\FieldType\Country\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/Country/Type.php:24
eZ\Publish\Core\FieldType\Country\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Country/Type.php:47
eZ\Publish\Core\FieldType\Country\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Country/Type.php:73
eZ\Publish\Core\FieldType\Country\Type\getSortInfo
getSortInfo(BaseValue $value)
{}
Definition: Core/FieldType/Country/Type.php:163
eZ\Publish\Core\FieldType\Country\Type\__construct
__construct(array $countriesInfo)
Definition: Core/FieldType/Country/Type.php:37
eZ\Publish\Core\FieldType\Country\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Country/Type.php:213
eZ\Publish\Core\FieldType\Country\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Country/Type.php:182
eZ\Publish\Core\FieldType\Country\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/Country/Type.php:227
eZ\Publish\Core\FieldType\Country\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Country/Type.php:85
eZ\Publish\Core\FieldType\Country\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/Country/Type.php:239
eZ\Publish\Core\FieldType\FieldType
Base class for field types, the most basic storage unit of data inside eZ Publish.
Definition: Core/FieldType/FieldType.php:35
eZ\Publish\Core\FieldType\FieldType\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/FieldType.php:350
eZ\Publish\Core\FieldType\ValidationError
Class for validation errors.
Definition: Core/FieldType/ValidationError.php:17
eZ\Publish\Core\FieldType\Value
Abstract class for all field value classes.
Definition: Core/FieldType/Value.php:17
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.