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

Core/FieldType/FieldType.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType;
8
9 use eZ\Publish\SPI\FieldType\Comparable;
10 use eZ\Publish\SPI\FieldType\FieldType as FieldTypeInterface;
11 use eZ\Publish\Core\Persistence\TransformationProcessor;
12 use eZ\Publish\SPI\FieldType\Value as SPIValue;
13 use eZ\Publish\SPI\Persistence\Content\FieldValue as PersistenceValue;
14 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
15 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
16
34 abstract class FieldType implements FieldTypeInterface, Comparable
35 {
44 protected $settingsSchema = [];
45
57 protected $validatorConfigurationSchema = [];
58
64 protected $transformationProcessor;
65
69 public function setTransformationProcessor(TransformationProcessor $transformationProcessor)
70 {
71 $this->transformationProcessor = $transformationProcessor;
72 }
73
83 public function getSettingsSchema()
84 {
85 return $this->settingsSchema;
86 }
87
114 public function getValidatorConfigurationSchema()
115 {
116 return $this->validatorConfigurationSchema;
117 }
118
133 public function validate(FieldDefinition $fieldDefinition, SPIValue $value)
134 {
135 return [];
136 }
137
152 public function validateValidatorConfiguration($validatorConfiguration)
153 {
154 $validationErrors = [];
155
156 foreach ((array)$validatorConfiguration as $validatorIdentifier => $constraints) {
157 $validationErrors[] = new ValidationError(
158 "Validator '%validator%' is unknown",
159 null,
160 [
161 'validator' => $validatorIdentifier,
162 ],
163 "[$validatorIdentifier]"
164 );
165 }
166
167 return $validationErrors;
168 }
169
180 public function applyDefaultValidatorConfiguration(&$validatorConfiguration)
181 {
182 if ($validatorConfiguration !== null && !is_array($validatorConfiguration)) {
183 throw new InvalidArgumentType('$validatorConfiguration', 'array|null', $validatorConfiguration);
184 }
185
186 foreach ($this->getValidatorConfigurationSchema() as $validatorName => $configurationSchema) {
187 // Set configuration of specific validator to empty array if it is not already provided
188 if (!isset($validatorConfiguration[$validatorName])) {
189 $validatorConfiguration[$validatorName] = [];
190 }
191
192 foreach ($configurationSchema as $settingName => $settingConfiguration) {
193 // Check that a default entry exists in the configuration schema for the validator but that no value has been provided
194 if (!isset($validatorConfiguration[$validatorName][$settingName]) && array_key_exists('default', $settingConfiguration)) {
195 $validatorConfiguration[$validatorName][$settingName] = $settingConfiguration['default'];
196 }
197 }
198 }
199 }
200
211 public function validateFieldSettings($fieldSettings)
212 {
213 if (!empty($fieldSettings)) {
214 return [
215 new ValidationError(
216 "FieldType '%fieldType%' does not accept settings",
217 null,
218 [
219 'fieldType' => $this->getFieldTypeIdentifier(),
220 ],
221 'fieldType'
222 ),
223 ];
224 }
225
226 return [];
227 }
228
239 public function applyDefaultSettings(&$fieldSettings)
240 {
241 if ($fieldSettings !== null && !is_array($fieldSettings)) {
242 throw new InvalidArgumentType('$fieldSettings', 'array|null', $fieldSettings);
243 }
244
245 foreach ($this->getSettingsSchema() as $settingName => $settingConfiguration) {
246 // Checking that a default entry exists in the settingsSchema but that no value has been provided
247 if (!array_key_exists($settingName, (array)$fieldSettings) && array_key_exists('default', $settingConfiguration)) {
248 $fieldSettings[$settingName] = $settingConfiguration['default'];
249 }
250 }
251 }
252
271 protected function getSortInfo(Value $value)
272 {
273 throw new \RuntimeException('Not implemented, yet.');
274 }
275
283 public function toPersistenceValue(SPIValue $value)
284 {
285 // @todo Evaluate if creating the sortKey in every case is really needed
286 // Couldn't this be retrieved with a method, which would initialize
287 // that info on request only?
288 return new PersistenceValue(
289 [
290 'data' => $this->toHash($value),
291 'externalData' => null,
292 'sortKey' => $this->getSortInfo($value),
293 ]
294 );
295 }
296
304 public function fromPersistenceValue(PersistenceValue $fieldValue)
305 {
306 return $this->fromHash($fieldValue->data);
307 }
308
314 public function isSearchable()
315 {
316 return false;
317 }
318
324 public function isSingular()
325 {
326 return false;
327 }
328
334 public function onlyEmptyInstance()
335 {
336 return false;
337 }
338
350 public function isEmptyValue(SPIValue $value)
351 {
352 return $value === null || $value == $this->getEmptyValue();
353 }
354
376 final public function acceptValue($inputValue)
377 {
378 if ($inputValue === null) {
379 return $this->getEmptyValue();
380 }
381
382 $value = $this->createValueFromInput($inputValue);
383
384 static::checkValueType($value);
385
386 if ($this->isEmptyValue($value)) {
387 return $this->getEmptyValue();
388 }
389
390 $this->checkValueStructure($value);
391
392 return $value;
393 }
394
420 abstract protected function createValueFromInput($inputValue);
421
445 protected static function checkValueType($value)
446 {
447 $fieldTypeFQN = get_called_class();
448 $valueFQN = substr_replace($fieldTypeFQN, 'Value', strrpos($fieldTypeFQN, '\\') + 1);
449
450 if (!$value instanceof $valueFQN) {
451 throw new InvalidArgumentType('$value', $valueFQN, $value);
452 }
453 }
454
479 abstract protected function checkValueStructure(Value $value);
480
492 public function fieldSettingsToHash($fieldSettings)
493 {
494 return $fieldSettings;
495 }
496
511 public function fieldSettingsFromHash($fieldSettingsHash)
512 {
513 return $fieldSettingsHash;
514 }
515
527 public function validatorConfigurationToHash($validatorConfiguration)
528 {
529 return $validatorConfiguration;
530 }
531
545 public function validatorConfigurationFromHash($validatorConfigurationHash)
546 {
547 return $validatorConfigurationHash;
548 }
549
575 public function getRelations(SPIValue $fieldValue)
576 {
577 return [];
578 }
579
580 public function valuesEqual(SPIValue $value1, SPIValue $value2): bool
581 {
582 return $this->toHash($value1) === $this->toHash($value2);
583 }
584 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\Core\Base\Exceptions\InvalidArgumentType
Invalid Argument Type Exception implementation.
Definition: InvalidArgumentType.php:17
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\fieldSettingsToHash
fieldSettingsToHash($fieldSettings)
Converts the given $fieldSettings to a simple hash format.
Definition: Core/FieldType/FieldType.php:492
eZ\Publish\Core\FieldType\FieldType\applyDefaultValidatorConfiguration
applyDefaultValidatorConfiguration(&$validatorConfiguration)
Applies the default values to the given $validatorConfiguration of a FieldDefinitionCreateStruct.
Definition: Core/FieldType/FieldType.php:180
eZ\Publish\Core\FieldType\FieldType\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/FieldType.php:314
eZ\Publish\Core\FieldType\FieldType\$validatorConfigurationSchema
$validatorConfigurationSchema
Definition: Core/FieldType/FieldType.php:57
eZ\Publish\Core\FieldType\FieldType\checkValueStructure
checkValueStructure(Value $value)
Throws an exception if value structure is not of expected format.
eZ\Publish\Core\FieldType\FieldType\getValidatorConfigurationSchema
getValidatorConfigurationSchema()
Returns a schema for the validator configuration expected by the FieldType.
Definition: Core/FieldType/FieldType.php:114
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\FieldType\getSortInfo
getSortInfo(Value $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/FieldType.php:271
eZ\Publish\Core\FieldType\FieldType\isSingular
isSingular()
Indicates if the field definition of this type can appear only once in the same ContentType.
Definition: Core/FieldType/FieldType.php:324
eZ\Publish\Core\FieldType\FieldType\acceptValue
acceptValue($inputValue)
Potentially builds and checks the type and structure of the $inputValue.
Definition: Core/FieldType/FieldType.php:376
eZ\Publish\Core\FieldType\FieldType\getRelations
getRelations(SPIValue $fieldValue)
Returns relation data extracted from value.
Definition: Core/FieldType/FieldType.php:575
eZ\Publish\Core\FieldType\FieldType\validateValidatorConfiguration
validateValidatorConfiguration($validatorConfiguration)
Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/FieldType.php:152
eZ\Publish\Core\FieldType\FieldType\validatorConfigurationFromHash
validatorConfigurationFromHash($validatorConfigurationHash)
Converts the given $validatorConfigurationHash to a validator configuration of the type.
Definition: Core/FieldType/FieldType.php:545
eZ\Publish\Core\FieldType\FieldType\applyDefaultSettings
applyDefaultSettings(&$fieldSettings)
Applies the default values to the fieldSettings of a FieldDefinitionCreateStruct.
Definition: Core/FieldType/FieldType.php:239
eZ\Publish\Core\FieldType\FieldType\$transformationProcessor
$transformationProcessor
Definition: Core/FieldType/FieldType.php:64
eZ\Publish\Core\FieldType\FieldType\validatorConfigurationToHash
validatorConfigurationToHash($validatorConfiguration)
Converts the given $validatorConfiguration to a simple hash format.
Definition: Core/FieldType/FieldType.php:527
eZ\Publish\Core\FieldType\FieldType\validate
validate(FieldDefinition $fieldDefinition, SPIValue $value)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/FieldType.php:133
eZ\Publish\Core\FieldType\FieldType\onlyEmptyInstance
onlyEmptyInstance()
Indicates if the field definition of this type can be added to a ContentType with Content instances.
Definition: Core/FieldType/FieldType.php:334
eZ\Publish\Core\FieldType\FieldType\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/FieldType.php:211
eZ\Publish\Core\FieldType\FieldType\valuesEqual
valuesEqual(SPIValue $value1, SPIValue $value2)
Definition: Core/FieldType/FieldType.php:580
eZ\Publish\Core\FieldType\FieldType\toPersistenceValue
toPersistenceValue(SPIValue $value)
Converts a $value to a persistence value.
Definition: Core/FieldType/FieldType.php:283
eZ\Publish\Core\FieldType\FieldType\getSettingsSchema
getSettingsSchema()
Returns a schema for the settings expected by the FieldType.
Definition: Core/FieldType/FieldType.php:83
eZ\Publish\Core\FieldType\FieldType\setTransformationProcessor
setTransformationProcessor(TransformationProcessor $transformationProcessor)
Definition: Core/FieldType/FieldType.php:69
eZ\Publish\Core\FieldType\FieldType\fieldSettingsFromHash
fieldSettingsFromHash($fieldSettingsHash)
Converts the given $fieldSettingsHash to field settings of the type.
Definition: Core/FieldType/FieldType.php:511
eZ\Publish\Core\FieldType\FieldType\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
eZ\Publish\Core\FieldType\FieldType\fromPersistenceValue
fromPersistenceValue(PersistenceValue $fieldValue)
Converts a persistence $fieldValue to a Value.
Definition: Core/FieldType/FieldType.php:304
eZ\Publish\Core\FieldType\FieldType\$settingsSchema
$settingsSchema
Definition: Core/FieldType/FieldType.php:44
eZ\Publish\Core\FieldType\FieldType\checkValueType
static checkValueType($value)
Throws an exception if the given $value is not an instance of the supported value subtype.
Definition: Core/FieldType/FieldType.php:445
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\Core\Persistence\TransformationProcessor
Interface for processing a set of transformations on a string.
Definition: TransformationProcessor.php:15
eZ\Publish\SPI\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\FieldType\Comparable
Definition: Comparable.php:12
eZ\Publish\SPI\FieldType\FieldType
The field type interface which all field types have to implement.
Definition: SPI/FieldType/FieldType.php:30
eZ\Publish\SPI\FieldType\FieldType\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
eZ\Publish\SPI\FieldType\FieldType\getEmptyValue
getEmptyValue()
Returns the empty value for this field type.
eZ\Publish\SPI\FieldType\FieldType\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
eZ\Publish\SPI\FieldType\FieldType\toHash
toHash(Value $value)
Converts the given $value into a plain hash format.
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+.