TYPO3 CMS  TYPO3_7-6
AbstractCompositeValidator.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 
22 abstract class AbstractCompositeValidator implements ObjectValidatorInterface, \Countable
23 {
29  protected $supportedOptions = [];
30 
34  protected $options = [];
35 
39  protected $validators;
40 
45 
53  public function __construct(array $options = [])
54  {
55  // check for options given but not supported
56  if (($unsupportedOptions = array_diff_key($options, $this->supportedOptions)) !== []) {
57  throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Unsupported validation option(s) found: ' . implode(', ', array_keys($unsupportedOptions)), 1339079804);
58  }
59 
60  // check for required options being set
61  array_walk(
62  $this->supportedOptions,
63  function ($supportedOptionData, $supportedOptionName, $options) {
64  if (isset($supportedOptionData[3]) && !array_key_exists($supportedOptionName, $options)) {
65  throw new \TYPO3\CMS\Extbase\Validation\Exception\InvalidValidationOptionsException('Required validation option not set: ' . $supportedOptionName, 1339163922);
66  }
67  },
68  $options
69  );
70 
71  // merge with default values
72  $this->options = array_merge(
73  array_map(
74  function ($value) {
75  return $value[0];
76  },
78  ),
79  $options
80  );
81  $this->validators = new \SplObjectStorage();
82  }
83 
91  public function addValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
92  {
93  if ($validator instanceof ObjectValidatorInterface) {
94  // @todo: provide bugfix as soon as it is fixed in TYPO3.Flow (http://forge.typo3.org/issues/48093)
95  $validator->setValidatedInstancesContainer = $this->validatedInstancesContainer;
96  }
97  $this->validators->attach($validator);
98  }
99 
107  public function removeValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
108  {
109  if (!$this->validators->contains($validator)) {
110  throw new \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException('Cannot remove validator because its not in the conjunction.', 1207020177);
111  }
112  $this->validators->detach($validator);
113  }
114 
121  public function count()
122  {
123  return count($this->validators);
124  }
125 
131  public function getValidators()
132  {
133  return $this->validators;
134  }
135 
141  public function getOptions()
142  {
143  return $this->options;
144  }
145 
154  {
155  $this->validatedInstancesContainer = $validatedInstancesContainer;
156  }
157 }
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
removeValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
addValidator(\TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)