/**
  * Adds the internal validator object to the incoming argument validator
  *
  * @param ConjunctionValidator $argumentValidator The validator to extend
  *
  * @return void
  */
 public function addTo(ConjunctionValidator $argumentValidator)
 {
     $this->addPropertiesValidator();
     $this->addPersonValidator();
     $this->addMessageValidator();
     $argumentValidator->addValidator($this->validator);
 }
 /**
  * Only returns the TypoScriptValidator if overwriteDefaultValidation was TRUE other it returns child validators
  *
  * @return \SplObjectStorage
  */
 public function getValidators()
 {
     $result = parent::getValidators();
     foreach ($this->validators as $validator) {
         if ($validator instanceof TypoScriptValidator && $validator->overwriteDefaultValidation()) {
             $result = new \SplObjectStorage();
             $result->attach($validator);
         }
     }
     return $result;
 }
Example #3
0
 /**
  * This adds custom validators to the passed $conjunctionValidator.
  *
  * A custom validator is found if it follows the naming convention "Replace '\Model\' by '\Validator\' and
  * append 'Validator'". If found, it will be added to the $conjunctionValidator.
  *
  * In addition canValidate() will be called on all implementations of the ObjectValidatorInterface to find
  * all validators that could validate the target. The one with the highest priority will be added as well.
  * If multiple validators have the same priority, which one will be added is not deterministic.
  *
  * @param string $targetClassName
  * @param ConjunctionValidator $conjunctionValidator
  * @return NULL|Validator\ObjectValidatorInterface
  */
 protected function addCustomValidators($targetClassName, ConjunctionValidator &$conjunctionValidator)
 {
     $addedValidatorClassName = NULL;
     // @todo: get rid of ClassNamingUtility usage once we dropped underscored class name support
     $possibleValidatorClassName = ClassNamingUtility::translateModelNameToValidatorName($targetClassName);
     $customValidator = $this->createValidator($possibleValidatorClassName);
     if ($customValidator !== NULL) {
         $conjunctionValidator->addValidator($customValidator);
         $addedValidatorClassName = get_class($customValidator);
     }
     // @todo: find polytype validator for class
 }
 /**
  * Builds a base validator conjunction for the given data type.
  *
  * The base validation rules are those which were declared directly in a class (typically
  * a model) through some validate annotations on properties.
  *
  * If a property holds a class for which a base validator exists, that property will be
  * checked as well, regardless of a validate annotation
  *
  * Additionally, if a custom validator was defined for the class in question, it will be added
  * to the end of the conjunction. A custom validator is found if it follows the naming convention
  * "Replace '\Model\' by '\Validator\' and append 'Validator'".
  *
  * Example: $targetClassName is TYPO3\Foo\Domain\Model\Quux, then the validator will be found if it has the
  * name TYPO3\Foo\Domain\Validator\QuuxValidator
  *
  * @param string $indexKey The key to use as index in $this->baseValidatorConjunctions; calculated from target class name and validation groups
  * @param string $targetClassName The data type to build the validation conjunction for. Needs to be the fully qualified class name.
  * @param array $validationGroups The validation groups to build the validator for
  * @return void
  * @throws \TYPO3\CMS\Extbase\Validation\Exception\NoSuchValidatorException
  * @throws \InvalidArgumentException
  */
 protected function buildBaseValidatorConjunction($indexKey, $targetClassName, array $validationGroups = array())
 {
     $conjunctionValidator = new \TYPO3\CMS\Extbase\Validation\Validator\ConjunctionValidator();
     $this->baseValidatorConjunctions[$indexKey] = $conjunctionValidator;
     if (class_exists($targetClassName)) {
         // Model based validator
         /** @var \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator $objectValidator */
         $objectValidator = $this->objectManager->get('TYPO3\\CMS\\Extbase\\Validation\\Validator\\GenericObjectValidator', array());
         foreach ($this->reflectionService->getClassPropertyNames($targetClassName) as $classPropertyName) {
             $classPropertyTagsValues = $this->reflectionService->getPropertyTagsValues($targetClassName, $classPropertyName);
             if (!isset($classPropertyTagsValues['var'])) {
                 throw new \InvalidArgumentException(sprintf('There is no @var annotation for property "%s" in class "%s".', $classPropertyName, $targetClassName), 1363778104);
             }
             try {
                 $parsedType = \TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::parseType(trim(implode('', $classPropertyTagsValues['var']), ' \\'));
             } catch (\TYPO3\CMS\Extbase\Utility\Exception\InvalidTypeException $exception) {
                 throw new \InvalidArgumentException(sprintf(' @var annotation of ' . $exception->getMessage(), 'class "' . $targetClassName . '", property "' . $classPropertyName . '"'), 1315564744, $exception);
             }
             $propertyTargetClassName = $parsedType['type'];
             if (\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCollectionType($propertyTargetClassName) === TRUE) {
                 $collectionValidator = $this->createValidator('TYPO3\\CMS\\Extbase\\Validation\\Validator\\CollectionValidator', array('elementType' => $parsedType['elementType'], 'validationGroups' => $validationGroups));
                 $objectValidator->addPropertyValidator($classPropertyName, $collectionValidator);
             } elseif (class_exists($propertyTargetClassName) && !\TYPO3\CMS\Extbase\Utility\TypeHandlingUtility::isCoreType($propertyTargetClassName) && $this->objectManager->isRegistered($propertyTargetClassName) && $this->objectManager->getScope($propertyTargetClassName) === \TYPO3\CMS\Extbase\Object\Container\Container::SCOPE_PROTOTYPE) {
                 $validatorForProperty = $this->getBaseValidatorConjunction($propertyTargetClassName, $validationGroups);
                 if (count($validatorForProperty) > 0) {
                     $objectValidator->addPropertyValidator($classPropertyName, $validatorForProperty);
                 }
             }
             $validateAnnotations = array();
             // @todo: Resolve annotations via reflectionService once its available
             if (isset($classPropertyTagsValues['validate']) && is_array($classPropertyTagsValues['validate'])) {
                 foreach ($classPropertyTagsValues['validate'] as $validateValue) {
                     $parsedAnnotations = $this->parseValidatorAnnotation($validateValue);
                     foreach ($parsedAnnotations['validators'] as $validator) {
                         array_push($validateAnnotations, array('argumentName' => $parsedAnnotations['argumentName'], 'validatorName' => $validator['validatorName'], 'validatorOptions' => $validator['validatorOptions']));
                     }
                 }
             }
             foreach ($validateAnnotations as $validateAnnotation) {
                 // @todo: Respect validationGroups
                 $newValidator = $this->createValidator($validateAnnotation['validatorName'], $validateAnnotation['validatorOptions']);
                 if ($newValidator === NULL) {
                     throw new Exception\NoSuchValidatorException('Invalid validate annotation in ' . $targetClassName . '::' . $classPropertyName . ': Could not resolve class name for  validator "' . $validateAnnotation->type . '".', 1241098027);
                 }
                 $objectValidator->addPropertyValidator($classPropertyName, $newValidator);
             }
         }
         if (count($objectValidator->getPropertyValidators()) > 0) {
             $conjunctionValidator->addValidator($objectValidator);
         }
     }
     $this->addCustomValidators($targetClassName, $conjunctionValidator);
 }