‪TYPO3CMS  10.4
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 
62  public function ‪__construct(array ‪$options = [])
63  {
64  // check for options given but not supported
65  if (($unsupportedOptions = array_diff_key(‪$options, $this->supportedOptions)) !== []) {
66  throw new ‪InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
67  }
68 
69  // check for required options being set
70  array_walk(
71  $this->supportedOptions,
72  function ($supportedOptionData, $supportedOptionName, ‪$options) {
73  if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, ‪$options)) {
74  throw new ‪InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
75  }
76  },
78  );
79 
80  // merge with default values
81  $this->options = array_merge(
82  array_map(
83  function ($value) {
84  return $value[0];
85  },
87  ),
89  );
90  }
91 
99  public function ‪validate($value)
100  {
101  $this->result = new ‪Result();
102  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
103  $this->‪isValid($value);
104  }
105  return ‪$this->result;
106  }
107 
114  abstract protected function ‪isValid($value);
115 
124  protected function ‪addError($message, $code, array $arguments = [], $title = '')
125  {
126  $this->result->addError(new Error((string)$message, (int)$code, $arguments, (string)$title));
127  }
128 
134  public function ‪getOptions()
135  {
136  return ‪$this->options;
137  }
138 
143  final protected function ‪isEmpty($value)
144  {
145  return $value === null || $value === '';
146  }
147 
157  protected function ‪translateErrorMessage($translateKey, $extensionName, $arguments = [])
158  {
160  $translateKey,
161  $extensionName,
162  $arguments
163  );
164  }
165 }
‪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\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:139
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string null translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:153
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isValid
‪isValid($value)
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\getOptions
‪array getOptions()
Definition: AbstractValidator.php:130
‪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:58
‪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\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:95
‪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:120
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:26