‪TYPO3CMS  9.5
AbstractValidator.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
18 
23 {
33  protected ‪$acceptsEmptyValues = true;
34 
40  protected ‪$supportedOptions = [];
41 
45  protected ‪$options = [];
46 
50  protected ‪$result;
51 
58  public function ‪__construct(array ‪$options = [])
59  {
60  // check for options given but not supported
61  if (($unsupportedOptions = array_diff_key(‪$options, $this->supportedOptions)) !== []) {
62  throw new ‪InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
63  }
64 
65  // check for required options being set
66  array_walk(
67  $this->supportedOptions,
68  function ($supportedOptionData, $supportedOptionName, ‪$options) {
69  if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, ‪$options)) {
70  throw new ‪InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
71  }
72  },
74  );
75 
76  // merge with default values
77  $this->options = array_merge(
78  array_map(
79  function ($value) {
80  return $value[0];
81  },
83  ),
85  );
86  }
87 
95  public function ‪validate($value)
96  {
97  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
98  if ($this->acceptsEmptyValues === false || $this->‪isEmpty($value) === false) {
99  $this->‪isValid($value);
100  }
101  return ‪$this->result;
102  }
103 
110  abstract protected function ‪isValid($value);
111 
120  protected function ‪addError($message, $code, array $arguments = [], $title = '')
121  {
122  $this->result->addError(new \‪TYPO3\CMS\‪Extbase\Validation\Error($message, $code, $arguments, $title));
123  }
124 
130  public function ‪getOptions()
131  {
132  return ‪$this->options;
133  }
134 
139  final protected function ‪isEmpty($value)
140  {
141  return $value === null || $value === '';
142  }
143 
153  protected function ‪translateErrorMessage($translateKey, $extensionName, $arguments = [])
154  {
155  return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
156  $translateKey,
157  $extensionName,
158  $arguments
159  );
160  }
161 }
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator
Definition: AbstractValidator.php:23
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$result
‪TYPO3 CMS Extbase Error Result $result
Definition: AbstractValidator.php:46
‪TYPO3\CMS\Extbase\Annotation
Definition: IgnoreValidation.php:4
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isEmpty
‪bool isEmpty($value)
Definition: AbstractValidator.php:135
‪TYPO3
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\translateErrorMessage
‪string null translateErrorMessage($translateKey, $extensionName, $arguments=[])
Definition: AbstractValidator.php:149
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\isValid
‪isValid($value)
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\getOptions
‪array getOptions()
Definition: AbstractValidator.php:126
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$acceptsEmptyValues
‪bool $acceptsEmptyValues
Definition: AbstractValidator.php:32
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\__construct
‪__construct(array $options=[])
Definition: AbstractValidator.php:54
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$options
‪array $options
Definition: AbstractValidator.php:42
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\$supportedOptions
‪array $supportedOptions
Definition: AbstractValidator.php:38
‪TYPO3\CMS\Extbase\Validation\Error
Definition: Error.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\validate
‪TYPO3 CMS Extbase Error Result validate($value)
Definition: AbstractValidator.php:91
‪TYPO3\CMS\Extbase\Validation\Validator
Definition: AbstractCompositeValidator.php:2
‪TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface
Definition: ValidatorInterface.php:21
‪TYPO3\CMS\Extbase\Validation\Validator\AbstractValidator\addError
‪addError($message, $code, array $arguments=[], $title='')
Definition: AbstractValidator.php:116
‪TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException
Definition: InvalidValidationOptionsException.php:21