TYPO3 CMS  TYPO3_6-2
CollectionValidator.php
Go to the documentation of this file.
1 <?php
3 
23 
27  protected $supportedOptions = array(
28  'elementValidator' => array(NULL, 'The validator type to use for the collection elements', 'string'),
29  'elementType' => array(NULL, 'The type of the elements in the collection', 'string'),
30  'validationGroups' => array(NULL, 'The validation groups to link to', 'string'),
31  );
32 
37  protected $validatorResolver;
38 
47  public function validate($value) {
48  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
49 
50  if ($this->acceptsEmptyValues === FALSE || $this->isEmpty($value) === FALSE) {
51  if ((is_object($value) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType(get_class($value))) && !is_array($value)) {
52  $this->addError('The given subject was not a collection.', 1317204797);
53  return $this->result;
54  } elseif ($value instanceof \TYPO3\CMS\Extbase\Persistence\Generic\LazyObjectStorage && !$value->isInitialized()) {
55  return $this->result;
56  } elseif (is_object($value) && $this->isValidatedAlready($value)) {
57  return $this->result;
58  } else {
59  $this->isValid($value);
60  }
61  }
62  return $this->result;
63  }
64 
77  public function isValid($value) {
78  if (!$this->configurationManager->isFeatureEnabled('rewrittenPropertyMapper')) {
79  // @deprecated since Extbase 1.4.0, will be removed two versions after Extbase 6.1
80  if ($this->validatedInstancesContainer == NULL) {
81  $this->validatedInstancesContainer = new \SplObjectStorage();
82  }
83 
84  if ($this->result == NULL) {
85  $this->result = new \TYPO3\CMS\Extbase\Error\Result();
86  }
87  }
88 
89  foreach ($value as $index => $collectionElement) {
90  if (isset($this->options['elementValidator'])) {
91  $collectionElementValidator = $this->validatorResolver->createValidator($this->options['elementValidator']);
92  } elseif (isset($this->options['elementType'])) {
93  if (isset($this->options['validationGroups'])) {
94  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType'], $this->options['validationGroups']);
95  } else {
96  $collectionElementValidator = $this->validatorResolver->getBaseValidatorConjunction($this->options['elementType']);
97  }
98  } else {
99  return;
100  }
101  if ($collectionElementValidator instanceof ObjectValidatorInterface) {
102  $collectionElementValidator->setValidatedInstancesContainer($this->validatedInstancesContainer);
103  }
104  $this->result->forProperty($index)->merge($collectionElementValidator->validate($collectionElement));
105  }
106  }
107 }
addError($message, $code, array $arguments=array(), $title='')