‪TYPO3CMS  11.5
AbstractValidator.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
22 
27 {
37  protected ‪$acceptsEmptyValues = true;
38 
44  protected ‪$supportedOptions = [];
45 
49  protected ‪$options = [];
50 
54  protected ‪$result;
55 
63  public function ‪__construct(array ‪$options = [])
64  {
66  }
67 
75  public function ‪validate($value)
76  {
77  $this->result = new ‪Result();
78  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
79  $this->‪isValid($value);
80  }
81  return ‪$this->result;
82  }
83 
90  abstract protected function ‪isValid($value);
91 
100  protected function ‪addError($message, $code, array $arguments = [], $title = '')
101  {
102  $this->result->addError(new Error((string)$message, (int)$code, $arguments, (string)$title));
103  }
104 
114  protected function ‪addErrorForProperty($propertyPath, $message, $code, array $arguments = [], $title = '')
115  {
116  $propertyPath = is_array($propertyPath) ? implode('.', $propertyPath) : (string)$propertyPath;
117  $error = new Error((string)$message, (int)$code, $arguments, (string)$title);
118  $this->result->forProperty($propertyPath)->addError($error);
119  }
120 
126  public function ‪getOptions()
127  {
128  return ‪$this->options;
129  }
130 
135  final protected function ‪isEmpty($value)
136  {
137  return $value === null || $value === '';
138  }
139 
149  protected function ‪translateErrorMessage($translateKey, $extensionName, $arguments = []): string
150  {
152  $translateKey,
153  $extensionName,
154  $arguments
155  ) ?? '';
156  }
157 
162  protected function ‪initializeDefaultOptions(array ‪$options): void
163  {
164  // check for options given but not supported
165  if (($unsupportedOptions = array_diff_key(‪$options, $this->supportedOptions)) !== []) {
166  throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
167  }
168  // check for required options being set
169  array_walk(
170  $this->supportedOptions,
171  static function ($supportedOptionData, $supportedOptionName, ‪$options) {
172  if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, ‪$options)) {
173  throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
174  }
175  },
177  );
178  // merge with default values
179  $this->options = array_merge(
180  array_map(
181  static function ($value) {
182  return $value[0];
183  },
185  ),
187  );
188  }
189 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:27
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:50
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility
Definition: LocalizationUtility.php:33
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\initializeDefaultOptions
‪initializeDefaultOptions(array $options)
Definition: AbstractValidator.php:158
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:131
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isValid
‪isValid($value)
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\getOptions
‪array getOptions()
Definition: AbstractValidator.php:122
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$acceptsEmptyValues
‪bool $acceptsEmptyValues
Definition: AbstractValidator.php:36
‪TYPO3\CMS\Extbase\Error\Result
Definition: Result.php:24
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\__construct
‪__construct(array $options=[])
Definition: AbstractValidator.php:59
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$options
‪array $options
Definition: AbstractValidator.php:46
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$supportedOptions
‪array $supportedOptions
Definition: AbstractValidator.php:42
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:145
‪TYPO3\CMS\Extbase\Validation\Error
Definition: Error.php:22
‪TYPO3\CMS\Extbase\Utility\LocalizationUtility\translate
‪static string null translate(string $key, ?string $extensionName=null, array $arguments=null, string $languageKey=null, array $alternativeLanguageKeys=null)
Definition: LocalizationUtility.php:67
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: AbstractValidator.php:71
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addErrorForProperty
‪addErrorForProperty($propertyPath, $message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:110
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:16
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:22
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:96
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:25