コード例 #1
0
 /**
  * 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 ConjunctionValidator();
     $this->baseValidatorConjunctions[$indexKey] = $conjunctionValidator;
     // note: the simpleType check reduces lookups to the class loader
     if (!TypeHandlingUtility::isSimpleType($targetClassName) && class_exists($targetClassName)) {
         // Model based validator
         /** @var \TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator $objectValidator */
         $objectValidator = $this->objectManager->get(\TYPO3\CMS\Extbase\Validation\Validator\GenericObjectValidator::class, 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 = 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'];
             // note: the outer simpleType check reduces lookups to the class loader
             if (!TypeHandlingUtility::isSimpleType($propertyTargetClassName)) {
                 if (TypeHandlingUtility::isCollectionType($propertyTargetClassName)) {
                     $collectionValidator = $this->createValidator(\TYPO3\CMS\Extbase\Validation\Validator\CollectionValidator::class, array('elementType' => $parsedType['elementType'], 'validationGroups' => $validationGroups));
                     $objectValidator->addPropertyValidator($classPropertyName, $collectionValidator);
                 } elseif (class_exists($propertyTargetClassName) && !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 (!empty($validatorForProperty)) {
                         $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 (!empty($objectValidator->getPropertyValidators())) {
             $conjunctionValidator->addValidator($objectValidator);
         }
     }
     $this->addCustomValidators($targetClassName, $conjunctionValidator);
 }
コード例 #2
0
 /**
  * Determine the type converter to be used. If no converter has been found, an exception is raised.
  *
  * @param mixed $source
  * @param string $targetType
  * @param PropertyMappingConfigurationInterface $configuration
  * @throws Exception\TypeConverterException
  * @throws Exception\InvalidTargetException
  * @return \TYPO3\CMS\Extbase\Property\TypeConverterInterface Type Converter which should be used to convert between $source and $targetType.
  */
 protected function findTypeConverter($source, $targetType, PropertyMappingConfigurationInterface $configuration)
 {
     if ($configuration->getTypeConverter() !== null) {
         return $configuration->getTypeConverter();
     }
     $sourceType = $this->determineSourceType($source);
     if (!is_string($targetType)) {
         throw new Exception\InvalidTargetException('The target type was no string, but of type "' . gettype($targetType) . '"', 1297941727);
     }
     $targetType = $this->parseCompositeType($targetType);
     // This is needed to correctly convert old class names to new ones
     // This compatibility layer will be removed with 7.0
     $targetType = \TYPO3\CMS\Core\Core\ClassLoadingInformation::getClassNameForAlias($targetType);
     $targetType = TypeHandlingUtility::normalizeType($targetType);
     $converter = null;
     if (TypeHandlingUtility::isSimpleType($targetType)) {
         if (isset($this->typeConverters[$sourceType][$targetType])) {
             $converter = $this->findEligibleConverterWithHighestPriority($this->typeConverters[$sourceType][$targetType], $source, $targetType);
         }
     } else {
         $converter = $this->findFirstEligibleTypeConverterInObjectHierarchy($source, $sourceType, $targetType);
     }
     if ($converter === null) {
         throw new Exception\TypeConverterException('No converter found which can be used to convert from "' . $sourceType . '" to "' . $targetType . '".');
     }
     return $converter;
 }
コード例 #3
0
 /**
  * Converts the given parameter reflection into an information array
  *
  * @param ParameterReflection $parameter The parameter to reflect
  * @param int $parameterPosition
  * @param MethodReflection|NULL $method
  * @return array Parameter information array
  */
 protected function convertParameterReflectionToArray(ParameterReflection $parameter, $parameterPosition, MethodReflection $method = null)
 {
     $parameterInformation = ['position' => $parameterPosition, 'byReference' => $parameter->isPassedByReference(), 'array' => $parameter->isArray(), 'optional' => $parameter->isOptional(), 'allowsNull' => $parameter->allowsNull()];
     $parameterClass = $parameter->getClass();
     $parameterInformation['class'] = $parameterClass !== null ? $parameterClass->getName() : null;
     if ($parameter->isDefaultValueAvailable()) {
         $parameterInformation['defaultValue'] = $parameter->getDefaultValue();
     }
     if ($parameterClass !== null) {
         $parameterInformation['type'] = $parameterClass->getName();
     } elseif ($method !== null) {
         $methodTagsAndValues = $this->getMethodTagsValues($method->getDeclaringClass()->getName(), $method->getName());
         if (isset($methodTagsAndValues['param']) && isset($methodTagsAndValues['param'][$parameterPosition])) {
             $explodedParameters = explode(' ', $methodTagsAndValues['param'][$parameterPosition]);
             if (count($explodedParameters) >= 2) {
                 if (TypeHandlingUtility::isSimpleType($explodedParameters[0])) {
                     // ensure that short names of simple types are resolved correctly to the long form
                     // this is important for all kinds of type checks later on
                     $typeInfo = TypeHandlingUtility::parseType($explodedParameters[0]);
                     $parameterInformation['type'] = $typeInfo['type'];
                 } else {
                     $parameterInformation['type'] = $explodedParameters[0];
                 }
             }
         }
     }
     if (isset($parameterInformation['type']) && $parameterInformation['type'][0] === '\\') {
         $parameterInformation['type'] = substr($parameterInformation['type'], 1);
     }
     return $parameterInformation;
 }
コード例 #4
0
 /**
  * Returns TRUE if the $type is a simple type.
  *
  * @param string $type
  * @return boolean
  */
 public function isSimpleType($type)
 {
     return TypeHandlingUtility::isSimpleType($type);
 }