TYPO3 CMS  TYPO3_6-2
AbstractCompositeValidator.php
Go to the documentation of this file.
1 <?php
3 
22 abstract class AbstractCompositeValidator implements ObjectValidatorInterface, \Countable {
23 
29  protected $supportedOptions = array();
30 
34  protected $options = array();
35 
39  protected $validators;
40 
45 
49  protected $errors = array();
50 
57  public function __construct(array $options = array()) {
58  // check for options given but not supported
59  if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== array()) {
60  throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1339079804);
61  }
62 
63  // check for required options being set
64  array_walk(
65  $this->supportedOptions,
66  function($supportedOptionData, $supportedOptionName, $options) {
67  if (isset($supportedOptionData[3]) && !array_key_exists($supportedOptionName, $options)) {
68  throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1339163922);
69  }
70  },
71  $options
72  );
73 
74  // merge with default values
75  $this->options = array_merge(
76  array_map(
77  function ($value) {
78  return $value[0];
79  },
81  ),
82  $options
83  );
84  $this->validators = new \SplObjectStorage();
85  }
86 
94  public function setOptions(array $options) {
95  }
96 
103  public function getErrors() {
104  return $this->errors;
105  }
106 
114  public function addValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
115  if ($validator instanceof ObjectValidatorInterface) {
116  // @todo: provide bugfix as soon as it is fixed in TYPO3.Flow (http://forge.typo3.org/issues/48093)
117  $validator->setValidatedInstancesContainer = $this->validatedInstancesContainer;
118  }
119  $this->validators->attach($validator);
120  }
121 
129  public function removeValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
130  if (!$this->validators->contains($validator)) {
131  throw new \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException('Cannot remove validator because its not in the conjunction.', 1207020177);
132  }
133  $this->validators->detach($validator);
134  }
135 
142  public function count() {
143  return count($this->validators);
144  }
145 
151  public function getValidators() {
152  return $this->validators;
153  }
154 
160  public function getOptions() {
161  return $this->options;
162  }
163 
172  $this->validatedInstancesContainer = $validatedInstancesContainer;
173  }
174 
183  public function canValidate($object) {
184  // deliberately empty
185  }
186 
198  public function isPropertyValid($object, $propertyName) {
199  // deliberately empty
200  }
201 }
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
removeValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
addValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)