TYPO3 CMS  TYPO3_6-2
AbstractValidator.php
Go to the documentation of this file.
1 <?php
3 
17 
21 abstract class AbstractValidator implements ValidatorInterface {
22 
32  protected $acceptsEmptyValues = TRUE;
33 
39  protected $supportedOptions = array();
40 
44  protected $options = array();
45 
50  protected $errors = array();
51 
55  protected $result;
56 
64  public function __construct(array $options = array()) {
65  // check for options given but not supported
66  if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== array()) {
67  throw new InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1379981890);
68  }
69 
70  // check for required options being set
71  array_walk(
72  $this->supportedOptions,
73  function($supportedOptionData, $supportedOptionName, $options) {
74  if (isset($supportedOptionData[3]) && $supportedOptionData[3] === TRUE && !array_key_exists($supportedOptionName, $options)) {
75  throw new InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1379981891);
76  }
77  },
78  $options
79  );
80 
81  // merge with default values
82  $this->options = array_merge(
83  array_map(
84  function ($value) {
85  return $value[0];
86  },
88  ),
89  $options
90  );
91  }
92 
101  public function validate($value) {
102  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
103  if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
104  $this->isValid($value);
105  }
106  return $this->result;
107  }
108 
116  abstract protected function isValid($value);
117 
125  public function setOptions(array $options) {
126  $this->options = $options;
127  }
128 
135  public function getErrors() {
136  return $this->errors;
137  }
138 
148  protected function addError($message, $code, array $arguments = array(), $title = '') {
149  if ($this->result !== NULL) {
150  // backwards compatibility before Extbase 1.4.0: we cannot expect the "result" object to be there.
151  $this->result->addError(new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title));
152  }
153  // the following is @deprecated since Extbase 1.4.0:
154  $this->errors[] = new \TYPO3\CMS\Extbase\Validation\Error($message, $code, $arguments, $title);
155  }
156 
162  public function getOptions() {
163  return $this->options;
164  }
165 
170  final protected function isEmpty($value) {
171  return $value === NULL || $value === '';
172  }
173 
183  protected function translateErrorMessage($translateKey, $extensionName, $arguments = array()) {
184  return \TYPO3\CMS\Extbase\Utility\LocalizationUtility::translate(
185  $translateKey,
186  $extensionName,
187  $arguments
188  );
189  }
190 }
addError($message, $code, array $arguments=array(), $title='')
translateErrorMessage($translateKey, $extensionName, $arguments=array())