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

Core/FieldType/Image/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\Image;
8
9 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
10 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
11 use eZ\Publish\Core\FieldType\FieldType;
12 use eZ\Publish\Core\FieldType\ValidationError;
13 use eZ\Publish\Core\FieldType\Value as BaseValue;
14 use eZ\Publish\SPI\FieldType\Value as SPIValue;
15 use eZ\Publish\SPI\Persistence\Content\FieldValue;
16
20 class Type extends FieldType
21 {
25 protected $validatorConfigurationSchema = [
26 'FileSizeValidator' => [
27 'maxFileSize' => [
28 'type' => 'int',
29 'default' => null,
30 ],
31 ],
32 'AlternativeTextValidator' => [
33 'required' => [
34 'type' => 'bool',
35 'default' => false,
36 ],
37 ],
38 ];
39
41 private $validators;
42
46 public function __construct(array $validators)
47 {
48 $this->validators = $validators;
49 }
50
56 public function getFieldTypeIdentifier()
57 {
58 return 'ezimage';
59 }
60
71 public function getName(SPIValue $value)
72 {
73 return !empty($value->alternativeText) ? $value->alternativeText : (string)$value->fileName;
74 }
75
82 public function getEmptyValue()
83 {
84 return new Value();
85 }
86
94 protected function createValueFromInput($inputValue)
95 {
96 if (is_string($inputValue)) {
97 $inputValue = Value::fromString($inputValue);
98 }
99
100 if (is_array($inputValue)) {
101 if (isset($inputValue['inputUri']) && file_exists($inputValue['inputUri'])) {
102 $inputValue['fileSize'] = filesize($inputValue['inputUri']);
103 if (!isset($inputValue['fileName'])) {
104 $inputValue['fileName'] = basename($inputValue['inputUri']);
105 }
106 }
107
108 $inputValue = new Value($inputValue);
109 }
110
111 return $inputValue;
112 }
113
121 protected function checkValueStructure(BaseValue $value)
122 {
123 if (isset($value->inputUri) && !is_string($value->inputUri)) {
124 throw new InvalidArgumentType('$value->inputUri', 'string', $value->inputUri);
125 }
126
127 if (isset($value->id) && !is_string($value->id)) {
128 throw new InvalidArgumentType('$value->id', 'string', $value->id);
129 }
130
131 // Required parameter $fileName
132 if (!isset($value->fileName) || !is_string($value->fileName)) {
133 throw new InvalidArgumentType('$value->fileName', 'string', $value->fileName);
134 }
135
136 // Optional parameter $alternativeText
137 if (isset($value->alternativeText) && !is_string($value->alternativeText)) {
138 throw new InvalidArgumentType(
139 '$value->alternativeText',
140 'string',
141 $value->alternativeText
142 );
143 }
144
145 if (isset($value->fileSize) && (!is_int($value->fileSize) || $value->fileSize < 0)) {
146 throw new InvalidArgumentType(
147 '$value->fileSize',
148 'int',
149 $value->alternativeText
150 );
151 }
152 }
153
164 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
165 {
166 $errors = [];
167
168 if ($this->isEmptyValue($fieldValue)) {
169 return $errors;
170 }
171
172 foreach ($this->validators as $externalValidator) {
173 if (!$externalValidator->validate($fieldValue)) {
174 $errors = array_merge($errors, $externalValidator->getMessage());
175 }
176 }
177
178 foreach ((array)$fieldDefinition->getValidatorConfiguration() as $validatorIdentifier => $parameters) {
179 switch ($validatorIdentifier) {
180 case 'FileSizeValidator':
181 if (empty($parameters['maxFileSize'])) {
182 // No file size limit
183 break;
184 }
185
186 // Database stores maxFileSize in MB
187 if (($parameters['maxFileSize'] * 1024 * 1024) < $fieldValue->fileSize) {
188 $errors[] = new ValidationError(
189 'The file size cannot exceed %size% megabyte.',
190 'The file size cannot exceed %size% megabytes.',
191 [
192 '%size%' => $parameters['maxFileSize'],
193 ],
194 'fileSize'
195 );
196 }
197 break;
198 case 'AlternativeTextValidator':
199 if ($parameters['required'] && $fieldValue->isAlternativeTextEmpty()) {
200 $errors[] = new ValidationError(
201 'Alternative text is required.',
202 null,
203 [],
204 'alternativeText'
205 );
206 }
207 break;
208 }
209 }
210
211 return $errors;
212 }
213
221 public function validateValidatorConfiguration($validatorConfiguration)
222 {
223 $validationErrors = [];
224
225 foreach ($validatorConfiguration as $validatorIdentifier => $parameters) {
226 switch ($validatorIdentifier) {
227 case 'FileSizeValidator':
228 if (!array_key_exists('maxFileSize', $parameters)) {
229 $validationErrors[] = new ValidationError(
230 'Validator %validator% expects parameter %parameter% to be set.',
231 null,
232 [
233 '%validator%' => $validatorIdentifier,
234 '%parameter%' => 'maxFileSize',
235 ],
236 "[$validatorIdentifier]"
237 );
238 break;
239 }
240 if (!is_int($parameters['maxFileSize']) && $parameters['maxFileSize'] !== null) {
241 $validationErrors[] = new ValidationError(
242 'Validator %validator% expects parameter %parameter% to be of %type%.',
243 null,
244 [
245 '%validator%' => $validatorIdentifier,
246 '%parameter%' => 'maxFileSize',
247 '%type%' => 'integer',
248 ],
249 "[$validatorIdentifier][maxFileSize]"
250 );
251 }
252 break;
253 case 'AlternativeTextValidator':
254 if (!array_key_exists('required', $parameters)) {
255 $validationErrors[] = new ValidationError(
256 'Validator %validator% expects parameter %parameter% to be set.',
257 null,
258 [
259 '%validator%' => $validatorIdentifier,
260 '%parameter%' => 'required',
261 ],
262 "[$validatorIdentifier]"
263 );
264 }
265 break;
266 default:
267 $validationErrors[] = new ValidationError(
268 "Validator '%validator%' is unknown",
269 null,
270 [
271 '%validator%' => $validatorIdentifier,
272 ],
273 "[$validatorIdentifier]"
274 );
275 }
276 }
277
278 return $validationErrors;
279 }
280
284 protected function getSortInfo(BaseValue $value)
285 {
286 return false;
287 }
288
296 public function fromHash($hash)
297 {
298 if ($hash === null) {
299 return $this->getEmptyValue();
300 }
301
302 return new Value($hash);
303 }
304
312 public function toHash(SPIValue $value)
313 {
314 if ($this->isEmptyValue($value)) {
315 return null;
316 }
317
318 return [
319 'id' => $value->id,
320 'path' => $value->inputUri ?: $value->id,
321 'alternativeText' => $value->alternativeText,
322 'fileName' => $value->fileName,
323 'fileSize' => $value->fileSize,
324 'imageId' => $value->imageId,
325 'uri' => $value->uri,
326 'inputUri' => $value->inputUri,
327 'width' => $value->width,
328 'height' => $value->height,
329 ];
330 }
331
339 public function toPersistenceValue(SPIValue $value)
340 {
341 // Store original data as external (to indicate they need to be stored)
342 return new FieldValue(
343 [
344 'data' => null,
345 'externalData' => $this->toHash($value),
346 'sortKey' => $this->getSortInfo($value),
347 ]
348 );
349 }
350
358 public function fromPersistenceValue(FieldValue $fieldValue)
359 {
360 if ($fieldValue->data === null) {
361 return $this->getEmptyValue();
362 }
363
364 // Restored data comes in $data, since it has already been processed
365 // there might be more data in the persistence value than needed here
366 $result = $this->fromHash(
367 [
368 'id' => (isset($fieldValue->data['id'])
369 ? $fieldValue->data['id']
370 : null),
371 'alternativeText' => (isset($fieldValue->data['alternativeText'])
372 ? $fieldValue->data['alternativeText']
373 : null),
374 'fileName' => (isset($fieldValue->data['fileName'])
375 ? $fieldValue->data['fileName']
376 : null),
377 'fileSize' => (isset($fieldValue->data['fileSize'])
378 ? $fieldValue->data['fileSize']
379 : null),
380 'uri' => (isset($fieldValue->data['uri'])
381 ? $fieldValue->data['uri']
382 : null),
383 'imageId' => (isset($fieldValue->data['imageId'])
384 ? $fieldValue->data['imageId']
385 : null),
386 'width' => (isset($fieldValue->data['width'])
387 ? $fieldValue->data['width']
388 : null),
389 'height' => (isset($fieldValue->data['height'])
390 ? $fieldValue->data['height']
391 : null),
392 ]
393 );
394
395 return $result;
396 }
397
398 public function valuesEqual(SPIValue $value1, SPIValue $value2): bool
399 {
400 $hashValue1 = $this->toHash($value1);
401 $hashValue2 = $this->toHash($value2);
402
403 unset($hashValue1['imageId'], $hashValue2['imageId']);
404
405 return $hashValue1 === $hashValue2;
406 }
407 }
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition
Definition: API/Repository/Values/ContentType/FieldDefinition.php:31
eZ\Publish\API\Repository\Values\ContentType\FieldDefinition\getValidatorConfiguration
getValidatorConfiguration()
This method returns the validator configuration of this 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\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\Image\Type
The Image field type.
Definition: Core/FieldType/Image/Type.php:21
eZ\Publish\Core\FieldType\Image\Type\valuesEqual
valuesEqual(SPIValue $value1, SPIValue $value2)
Definition: Core/FieldType/Image/Type.php:398
eZ\Publish\Core\FieldType\Image\Type\validateValidatorConfiguration
validateValidatorConfiguration($validatorConfiguration)
Validates the validatorConfiguration of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/Image/Type.php:221
eZ\Publish\Core\FieldType\Image\Type\$validatorConfigurationSchema
$validatorConfigurationSchema
Definition: Core/FieldType/Image/Type.php:25
eZ\Publish\Core\FieldType\Image\Type\toPersistenceValue
toPersistenceValue(SPIValue $value)
Converts a $value to a persistence value.
Definition: Core/FieldType/Image/Type.php:339
eZ\Publish\Core\FieldType\Image\Type\__construct
__construct(array $validators)
Definition: Core/FieldType/Image/Type.php:46
eZ\Publish\Core\FieldType\Image\Type\fromPersistenceValue
fromPersistenceValue(FieldValue $fieldValue)
Converts a persistence $fieldValue to a Value.
Definition: Core/FieldType/Image/Type.php:358
eZ\Publish\Core\FieldType\Image\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/Image/Type.php:312
eZ\Publish\Core\FieldType\Image\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/Image/Type.php:296
eZ\Publish\Core\FieldType\Image\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/Image/Type.php:164
eZ\Publish\Core\FieldType\Image\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/Image/Type.php:71
eZ\Publish\Core\FieldType\Image\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/Image/Type.php:56
eZ\Publish\Core\FieldType\Image\Type\getSortInfo
getSortInfo(BaseValue $value)
{}
Definition: Core/FieldType/Image/Type.php:284
eZ\Publish\Core\FieldType\Image\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/Image/Type.php:82
eZ\Publish\Core\FieldType\Image\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/Image/Type.php:121
eZ\Publish\Core\FieldType\Image\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/Image/Type.php:94
eZ\Publish\Core\FieldType\Image\Type\$validators
$validators
Definition: Core/FieldType/Image/Type.php:41
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\Persistence\Content\FieldValue
Definition: FieldValue.php:12
eZ\Publish\SPI\FieldType\Value
Interface for field value classes.
Definition: SPI/FieldType/Value.php:13
eZ\Publish\Core\FieldType\Image
Definition: AliasCleanerInterface.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.