TYPO3 CMS  TYPO3_8-7
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  if (is_object($value) && $this->isValidatedAlready($value)) {
40  return $this->result;
41  }
42 
43  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
44  if ($this->acceptsEmptyValues === false || $this->isEmpty($value) === false) {
45  if (!is_object($value)) {
46  $this->addError('Object expected, %1$s given.', 1241099149, [gettype($value)]);
47  } elseif ($this->isValidatedAlready($value) === false) {
48  $this->markInstanceAsValidated($value);
49  $this->isValid($value);
50  }
51  }
52 
53  return $this->result;
54  }
55 
65  protected function getPropertyValue($object, $propertyName)
66  {
67  // @todo add support for lazy loading proxies, if needed
68  if (ObjectAccess::isPropertyGettable($object, $propertyName)) {
69  return ObjectAccess::getProperty($object, $propertyName);
70  }
71  return ObjectAccess::getProperty($object, $propertyName, true);
72  }
73 
82  protected function checkProperty($value, $validators, $propertyName)
83  {
85  $result = null;
86  foreach ($validators as $validator) {
87  if ($validator instanceof ObjectValidatorInterface) {
88  $validator->setValidatedInstancesContainer($this->validatedInstancesContainer);
89  }
90  $currentResult = $validator->validate($value);
91  if ($currentResult->hasMessages()) {
92  if ($result == null) {
93  $result = $currentResult;
94  } else {
95  $result->merge($currentResult);
96  }
97  }
98  }
99  if ($result != null) {
100  $this->result->forProperty($propertyName)->merge($result);
101  }
102  }
103 
110  protected function isValid($object)
111  {
112  foreach ($this->propertyValidators as $propertyName => $validators) {
113  $propertyValue = $this->getPropertyValue($object, $propertyName);
114  $this->checkProperty($propertyValue, $validators, $propertyName);
115  }
116  }
117 
125  public function canValidate($object)
126  {
127  return is_object($object);
128  }
129 
137  public function addPropertyValidator($propertyName, ValidatorInterface $validator)
138  {
139  if (!isset($this->propertyValidators[$propertyName])) {
140  $this->propertyValidators[$propertyName] = new \SplObjectStorage();
141  }
142  $this->propertyValidators[$propertyName]->attach($validator);
143  }
144 
149  protected function isValidatedAlready($object)
150  {
151  if ($this->validatedInstancesContainer === null) {
152  $this->validatedInstancesContainer = new \SplObjectStorage();
153  }
154  if ($this->validatedInstancesContainer->contains($object)) {
155  return true;
156  }
157 
158  return false;
159  }
160 
164  protected function markInstanceAsValidated($object)
165  {
166  $this->validatedInstancesContainer->attach($object);
167  }
168 
175  public function getPropertyValidators($propertyName = null)
176  {
177  if ($propertyName !== null) {
178  return (isset($this->propertyValidators[$propertyName])) ? $this->propertyValidators[$propertyName] : [];
179  }
181  }
182 
187 
195  {
196  $this->validatedInstancesContainer = $validatedInstancesContainer;
197  }
198 }
addError($message, $code, array $arguments=[], $title='')
static isPropertyGettable($object, $propertyName)
static getProperty($subject, $propertyName, $forceDirectAccess=false)
setValidatedInstancesContainer(\SplObjectStorage $validatedInstancesContainer)
addPropertyValidator($propertyName, ValidatorInterface $validator)