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

Core/FieldType/ISBN/Type.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\FieldType\ISBN;
8
9 use eZ\Publish\Core\FieldType\FieldType;
10 use eZ\Publish\Core\FieldType\ValidationError;
11 use eZ\Publish\API\Repository\Values\ContentType\FieldDefinition;
12 use eZ\Publish\Core\Base\Exceptions\InvalidArgumentType;
13 use eZ\Publish\SPI\FieldType\Value as SPIValue;
14 use eZ\Publish\Core\FieldType\Value as BaseValue;
15
21 class Type extends FieldType
22 {
23 const ISBN13_PREFIX_LENGTH = 3;
24 const ISBN13_CHECK_LENGTH = 1;
25 const ISBN13_LENGTH = 13;
26 const ISBN13_PREFIX_978 = '978';
27 const ISBN13_PREFIX_979 = '979';
28
29 protected $settingsSchema = [
30 'isISBN13' => [
31 'type' => 'boolean',
32 'default' => true,
33 ],
34 ];
35
41 public function getFieldTypeIdentifier()
42 {
43 return 'ezisbn';
44 }
45
56 public function getName(SPIValue $value)
57 {
58 return (string)$value->isbn;
59 }
60
67 public function getEmptyValue()
68 {
69 return new Value();
70 }
71
79 public function isEmptyValue(SPIValue $value)
80 {
81 return $value->isbn === null || trim($value->isbn) === '';
82 }
83
91 protected function createValueFromInput($inputValue)
92 {
93 if (is_string($inputValue)) {
94 $inputValue = $this->fromHash($inputValue);
95 }
96
97 return $inputValue;
98 }
99
107 protected function checkValueStructure(BaseValue $value)
108 {
109 if (!is_string($value->isbn)) {
110 throw new InvalidArgumentType(
111 '$value->isbn',
112 'string',
113 $value->isbn
114 );
115 }
116 }
117
130 public function validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
131 {
132 $validationErrors = [];
133 if ($this->isEmptyValue($fieldValue)) {
134 return $validationErrors;
135 }
136
137 $fieldSettings = $fieldDefinition->getFieldSettings();
138 $isbnTestNumber = preg_replace("/[\s|\-]/", '', trim($fieldValue->isbn));
139
140 // Check if value and settings are inline
141 if ((!isset($fieldSettings['isISBN13']) || $fieldSettings['isISBN13'] === false)
142 && strlen($isbnTestNumber) !== 10) {
143 $validationErrors[] = new ValidationError(
144 'ISBN-10 must be 10 character length',
145 null,
146 [],
147 'isbn'
148 );
149 } elseif (strlen($isbnTestNumber) === 10) {
150 // ISBN-10 check
151 if (!$this->validateISBNChecksum($isbnTestNumber)) {
152 $validationErrors[] = new ValidationError(
153 'ISBN value must be in a valid ISBN-10 format',
154 null,
155 [],
156 'isbn'
157 );
158 }
159 } else {
160 // ISBN-13 check
161 if (!$this->validateISBN13Checksum($isbnTestNumber, $error)) {
162 $validationErrors[] = new ValidationError(
163 $error,
164 null,
165 [],
166 'isbn'
167 );
168 }
169 }
170
171 return $validationErrors;
172 }
173
181 protected function getSortInfo(BaseValue $value)
182 {
183 return $this->transformationProcessor->transformByGroup((string)$value, 'lowercase');
184 }
185
193 public function fromHash($hash)
194 {
195 if ($hash === null || $hash === '') {
196 return $this->getEmptyValue();
197 }
198
199 return new Value($hash);
200 }
201
209 public function toHash(SPIValue $value)
210 {
211 if ($this->isEmptyValue($value)) {
212 return null;
213 }
214
215 return $value->isbn;
216 }
217
223 public function isSearchable()
224 {
225 return true;
226 }
227
235 public function validateFieldSettings($fieldSettings)
236 {
237 $validationErrors = [];
238
239 foreach ($fieldSettings as $name => $value) {
240 if (!isset($this->settingsSchema[$name])) {
241 $validationErrors[] = new ValidationError(
242 "Setting '%setting%' is unknown",
243 null,
244 [
245 '%setting%' => $name,
246 ],
247 "[$name]"
248 );
249 continue;
250 }
251
252 switch ($name) {
253 case 'isISBN13':
254 if (!is_bool($value)) {
255 $validationErrors[] = new ValidationError(
256 "Setting '%setting%' value must be of boolean type",
257 null,
258 [
259 '%setting%' => $name,
260 ],
261 "[$name]"
262 );
263 }
264 break;
265 }
266 }
267
268 return $validationErrors;
269 }
270
280 private function validateISBNChecksum($isbnNr)
281 {
282 $result = 0;
283 $isbnNr = strtoupper($isbnNr);
284 for ($i = 10; $i > 0; --$i) {
285 if (is_numeric($isbnNr[$i - 1]) || ($i === 10 && $isbnNr[$i - 1] === 'X')) {
286 if (($i === 1) && ($isbnNr[9] === 'X')) {
287 $result += 10 * $i;
288 } else {
289 $result += $isbnNr[10 - $i] * $i;
290 }
291 } else {
292 return false;
293 }
294 }
295
296 return $result % 11 === 0;
297 }
298
308 private function validateISBN13Checksum($isbnNr, &$error)
309 {
310 if (!$isbnNr) {
311 return false;
312 }
313
314 if (strlen($isbnNr) !== self::ISBN13_LENGTH) {
315 $error = 'ISBN-13 must be 13 digit, digit count is: ' . strlen($isbnNr);
316
317 return false;
318 }
319
320 if (substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH) !== self::ISBN13_PREFIX_978 &&
321 substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH) !== self::ISBN13_PREFIX_979) {
322 $error = 'ISBN-13 value must start with 978 or 979, got: ' . substr($isbnNr, 0, self::ISBN13_PREFIX_LENGTH);
323
324 return false;
325 }
326
327 $checksum13 = 0;
328 $weight13 = 1;
329 //compute checksum
330 $val = 0;
331 for ($i = 0; $i < self::ISBN13_LENGTH; ++$i) {
332 $val = $isbnNr[$i];
333 if (!is_numeric($isbnNr[$i])) {
334 $error = 'All ISBN-13 characters need to be numeric';
335
336 return false;
337 }
338 $checksum13 = $checksum13 + $weight13 * $val;
339 $weight13 = ($weight13 + 2) % 4;
340 }
341 if (($checksum13 % 10) !== 0) {
342 // Calculate the last digit from the 12 first numbers.
343 $checkDigit = (10 - (($checksum13 - (($weight13 + 2) % 4) * $val) % 10)) % 10;
344 //bad checksum
345 $error = 'Bad checksum, last digit of ISBN-13 should be ' . $checkDigit;
346
347 return false;
348 }
349
350 return true;
351 }
352 }
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\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\ISBN\Type
The ISBN field type.
Definition: Core/FieldType/ISBN/Type.php:22
eZ\Publish\Core\FieldType\ISBN\Type\getName
getName(SPIValue $value)
Returns the name of the given field value.
Definition: Core/FieldType/ISBN/Type.php:56
eZ\Publish\Core\FieldType\ISBN\Type\createValueFromInput
createValueFromInput($inputValue)
Inspects given $inputValue and potentially converts it into a dedicated value object.
Definition: Core/FieldType/ISBN/Type.php:91
eZ\Publish\Core\FieldType\ISBN\Type\validate
validate(FieldDefinition $fieldDefinition, SPIValue $fieldValue)
Validates a field based on the validators in the field definition.
Definition: Core/FieldType/ISBN/Type.php:130
eZ\Publish\Core\FieldType\ISBN\Type\isEmptyValue
isEmptyValue(SPIValue $value)
Returns if the given $value is considered empty by the field type.
Definition: Core/FieldType/ISBN/Type.php:79
eZ\Publish\Core\FieldType\ISBN\Type\isSearchable
isSearchable()
Returns whether the field type is searchable.
Definition: Core/FieldType/ISBN/Type.php:223
eZ\Publish\Core\FieldType\ISBN\Type\validateFieldSettings
validateFieldSettings($fieldSettings)
Validates the fieldSettings of a FieldDefinitionCreateStruct or FieldDefinitionUpdateStruct.
Definition: Core/FieldType/ISBN/Type.php:235
eZ\Publish\Core\FieldType\ISBN\Type\ISBN13_PREFIX_978
const ISBN13_PREFIX_978
Definition: Core/FieldType/ISBN/Type.php:26
eZ\Publish\Core\FieldType\ISBN\Type\checkValueStructure
checkValueStructure(BaseValue $value)
Throws an exception if value structure is not of expected format.
Definition: Core/FieldType/ISBN/Type.php:107
eZ\Publish\Core\FieldType\ISBN\Type\validateISBNChecksum
validateISBNChecksum($isbnNr)
Validates the ISBN number.
Definition: Core/FieldType/ISBN/Type.php:280
eZ\Publish\Core\FieldType\ISBN\Type\$settingsSchema
$settingsSchema
Definition: Core/FieldType/ISBN/Type.php:29
eZ\Publish\Core\FieldType\ISBN\Type\fromHash
fromHash($hash)
Converts an $hash to the Value defined by the field type.
Definition: Core/FieldType/ISBN/Type.php:193
eZ\Publish\Core\FieldType\ISBN\Type\ISBN13_PREFIX_LENGTH
const ISBN13_PREFIX_LENGTH
Definition: Core/FieldType/ISBN/Type.php:23
eZ\Publish\Core\FieldType\ISBN\Type\ISBN13_LENGTH
const ISBN13_LENGTH
Definition: Core/FieldType/ISBN/Type.php:25
eZ\Publish\Core\FieldType\ISBN\Type\validateISBN13Checksum
validateISBN13Checksum($isbnNr, &$error)
Validates the ISBN-13 number.
Definition: Core/FieldType/ISBN/Type.php:308
eZ\Publish\Core\FieldType\ISBN\Type\getEmptyValue
getEmptyValue()
Returns the fallback default value of field type when no such default value is provided in the field ...
Definition: Core/FieldType/ISBN/Type.php:67
eZ\Publish\Core\FieldType\ISBN\Type\ISBN13_CHECK_LENGTH
const ISBN13_CHECK_LENGTH
Definition: Core/FieldType/ISBN/Type.php:24
eZ\Publish\Core\FieldType\ISBN\Type\getSortInfo
getSortInfo(BaseValue $value)
Returns information for FieldValue->$sortKey relevant to the field type.
Definition: Core/FieldType/ISBN/Type.php:181
eZ\Publish\Core\FieldType\ISBN\Type\toHash
toHash(SPIValue $value)
Converts a $Value to a hash.
Definition: Core/FieldType/ISBN/Type.php:209
eZ\Publish\Core\FieldType\ISBN\Type\getFieldTypeIdentifier
getFieldTypeIdentifier()
Returns the field type identifier for this field type.
Definition: Core/FieldType/ISBN/Type.php:41
eZ\Publish\Core\FieldType\ISBN\Type\ISBN13_PREFIX_979
const ISBN13_PREFIX_979
Definition: Core/FieldType/ISBN/Type.php:27
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
eZ\Publish\Core\FieldType\ISBN
Definition: ISBN/SearchField.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.