TYPO3 CMS  TYPO3_6-2
GenericObjectValidator.php
Go to the documentation of this file.
1 <?php
3 
20 
24  protected $propertyValidators = array();
25 
34  public function validate($value) {
35  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
36  if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
37  if (!is_object($value)) {
38  $this->addError('Object expected, %1$s given.', 1241099149, array(gettype($value)));
39  } elseif ($this->isValidatedAlready($value) === FALSE) {
40  $this->isValid($value);
41  }
42  }
43 
44  return $this->result;
45  }
46 
56  protected function getPropertyValue($object, $propertyName) {
57  // TODO: add support for lazy loading proxies, if needed
58  if (\TYPO3\CMS\Extbase\Reflection\ObjectAccess::isPropertyGettable($object, $propertyName)) {
59  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName);
60  } else {
61  return \TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName, TRUE);
62  }
63  }
64 
74  protected function checkProperty($value, $validators, $propertyName) {
75  $result = NULL;
76  foreach ($validators as $validator) {
77  if ($validator instanceof ObjectValidatorInterface) {
78  $validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
79  }
80  $currentResult = $validator->validate($value);
81  if ($currentResult->hasMessages()) {
82  if ($result == NULL) {
83  $result = $currentResult;
84  } else {
85  $result->merge($currentResult);
86  }
87  }
88  }
89  if ($result != NULL) {
90  $this->result->forProperty($propertyName)->merge($result);
91  }
92  }
93 
102  public function isValid($object) {
103  if ($this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
104  foreach ($this->propertyValidators as $propertyName => $validators) {
105  $propertyValue = $this->getPropertyValue($object, $propertyName);
106  $this->checkProperty($propertyValue, $validators, $propertyName);
107  }
108 
109  return;
110  } else {
111  /* @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1 */
112  if (!is_object($object)) {
113  $this->addError(
114  $this->translateErrorMessage(
115  'validator.genericobject.noobject',
116  'extbase'
117  ), 1241099148);
118 
119  return FALSE;
120  }
121  $result = TRUE;
122  foreach (array_keys($this->propertyValidators) as $propertyName) {
123  if ($this->isPropertyValid($object, $propertyName) === FALSE) {
124  $result = FALSE;
125  }
126  }
127 
128  return $result;
129  }
130  }
131 
139  public function canValidate($object) {
140  return is_object($object);
141  }
142 
155  public function isPropertyValid($object, $propertyName) {
156  if (!is_object($object)) {
157  throw new \InvalidArgumentException('Object expected, ' . gettype($object) . ' given.', 1241099149);
158  }
159  if (!isset($this->propertyValidators[$propertyName])) {
160  return TRUE;
161  }
162  $result = TRUE;
163  foreach ($this->propertyValidators[$propertyName] as $validator) {
164  $validator->isValid(\TYPO3\CMS\Extbase\Reflection\ObjectAccess::getProperty($object, $propertyName));
165  if (count($validator->getErrors()) > 0) {
166  $this->addErrorsForProperty($validator->getErrors(), $propertyName);
167  $result = FALSE;
168  }
169  }
170  return $result;
171  }
172 
179  protected function addErrorsForProperty($errors, $propertyName) {
180  if (!isset($this->errors[$propertyName])) {
181  $this->errors[$propertyName] = new \TYPO3\CMS\Extbase\Validation\PropertyError($propertyName);
182  }
183  $this->errors[$propertyName]->addErrors($errors);
184  }
185 
194  public function addPropertyValidator($propertyName, \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator) {
195  if (!isset($this->propertyValidators[$propertyName])) {
196  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
197  }
198  $this->propertyValidators[$propertyName]->attach($validator);
199  }
200 
205  protected function isValidatedAlready($object) {
206  if ($this->validatedInstancesContainer === NULL) {
207  $this->validatedInstancesContainer = new \SplObjectStorage();
208  }
209  if ($this->validatedInstancesContainer->contains($object)) {
210  return TRUE;
211  } else {
212  $this->validatedInstancesContainer->attach($object);
213 
214  return FALSE;
215  }
216  }
217 
224  public function getPropertyValidators($propertyName = NULL) {
225  if ($propertyName !== NULL) {
226  return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : array();
227  } else {
229  }
230  }
231 
236 
241  public function injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager) {
242  // @todo: remove configuration manager once the old property mapper is removed
243  $this->configurationManager = $configurationManager;
244  }
245 
250 
259  $this->validatedInstancesContainer = $validatedInstancesContainer;
260  }
261 }
addError($message, $code, array $arguments=array(), $title='')
addPropertyValidator($propertyName, \TYPO3\CMS\Extbase\Validation\Validator\ValidatorInterface $validator)
translateErrorMessage($translateKey, $extensionName, $arguments=array())
injectConfigurationManager(\TYPO3\CMS\Extbase\Configuration\ConfigurationManagerInterface $configurationManager)
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)