TYPO3 CMS  TYPO3_7-6
GenericObjectValidator.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 
18 
23 {
27  protected $propertyValidators = [];
28 
37  public function validate($value)
38  {
39  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
40  if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
41  if (!is_object($value)) {
42  $this->addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
43  } elseif ($this->isValidatedAlready($value) === false) {
44  $this->isValid($value);
45  }
46  }
47 
48  return $this->result;
49  }
50 
60  protected function getPropertyValue($object, $propertyName)
61  {
62  // @todo add support for lazy loading proxies, if needed
63  if (ObjectAccess::isPropertyGettable($object, $propertyName)) {
64  return ObjectAccess::getProperty($object, $propertyName);
65  } else {
66  return ObjectAccess::getProperty($object, $propertyName, true);
67  }
68  }
69 
79  protected function checkProperty($value, $validators, $propertyName)
80  {
82  $result = null;
83  foreach ($validators as $validator) {
84  if ($validator instanceof ObjectValidatorInterface) {
85  $validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
86  }
87  $currentResult = $validator->validate($value);
88  if ($currentResult->hasMessages()) {
89  if ($result == null) {
90  $result = $currentResult;
91  } else {
92  $result->merge($currentResult);
93  }
94  }
95  }
96  if ($result != null) {
97  $this->result->forProperty($propertyName)->merge($result);
98  }
99  }
100 
108  protected function isValid($object)
109  {
110  foreach ($this->propertyValidators as $propertyName => $validators) {
111  $propertyValue = $this->getPropertyValue($object, $propertyName);
112  $this->checkProperty($propertyValue, $validators, $propertyName);
113  }
114  }
115 
123  public function canValidate($object)
124  {
125  return is_object($object);
126  }
127 
136  public function addPropertyValidator($propertyName, ValidatorInterface $validator)
137  {
138  if (!isset($this->propertyValidators[$propertyName])) {
139  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
140  }
141  $this->propertyValidators[$propertyName]->attach($validator);
142  }
143 
148  protected function isValidatedAlready($object)
149  {
150  if ($this->validatedInstancesContainer === null) {
151  $this->validatedInstancesContainer = new \SplObjectStorage();
152  }
153  if ($this->validatedInstancesContainer->contains($object)) {
154  return true;
155  } else {
156  $this->validatedInstancesContainer->attach($object);
157 
158  return false;
159  }
160  }
161 
168  public function getPropertyValidators($propertyName = null)
169  {
170  if ($propertyName !== null) {
171  return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : [];
172  } else {
174  }
175  }
176 
181 
190  {
191  $this->validatedInstancesContainer = $validatedInstancesContainer;
192  }
193 }
addError($message, $code, array $arguments=[], $title='')
static isPropertyGettable($object, $propertyName)
static getProperty($subject, $propertyName, $forceDirectAccess=false)
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
addPropertyValidator($propertyName, ValidatorInterface $validator)