TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
AbstractValidator.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Extbase\Validation\Validator;
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 
22 abstract class AbstractValidator implements ValidatorInterface
23 {
33  protected $acceptsEmptyValues = true;
34 
40  protected $supportedOptions = [];
41 
45  protected $options = [];
46 
50  protected $result;
51 
59  public function __construct(array $options = [])
60  {
61  // check for options given but not supported
62  if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== []) {
63  throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
64  }
65 
66  // check for required options being set
67  array_walk(
68  $this->supportedOptions,
69  function ($supportedOptionData, $supportedOptionName, $options) {
70  if (isset($supportedOptionData[3]) && $supportedOptionData[3] === true && !array_key_exists($supportedOptionName, $options)) {
71  throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
72  }
73  },
74  $options
75  );
76 
77  // merge with default values
78  $this->options = array_merge(
79  array_map(
80  function ($value) {
81  return $value[0];
82  },
84  ),
85  $options
86  );
87  }
88 
97  public function validate($value)
98  {
99  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
100  if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
101  $this->isValid($value);
102  }
103  return $this->result;
104  }
105 
113  abstract protected function isValid($value);
114 
124  protected function addError($message, $code, array $arguments = [], $title = '')
125  {
126  $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $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  {
159  return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
160  $translateKey,
161  $extensionName,
162  $arguments
163  );
164  }
165 }
addError($message, $code, array $arguments=[], $title= '')
translateErrorMessage($translateKey, $extensionName, $arguments=[])