Neos\Flow\Validation\ValidatorResolver::buildMethodArgumentsValidatorConjunctions PHP Метод

buildMethodArgumentsValidatorConjunctions() публичный Метод

Detects and registers any validators for arguments: - by the data type specified in the param annotations - additional validators specified in the validate annotations of a method
public buildMethodArgumentsValidatorConjunctions ( string $className, string $methodName, array $methodParameters = null, array $methodValidateAnnotations = null ) : array
$className string
$methodName string
$methodParameters array Optional pre-compiled array of method parameters
$methodValidateAnnotations array Optional pre-compiled array of validate annotations (as array)
Результат array An Array of ValidatorConjunctions for each method parameters.
    public function buildMethodArgumentsValidatorConjunctions($className, $methodName, array $methodParameters = null, array $methodValidateAnnotations = null)
    {
        $validatorConjunctions = [];
        if ($methodParameters === null) {
            $methodParameters = $this->reflectionService->getMethodParameters($className, $methodName);
        }
        if (count($methodParameters) === 0) {
            return $validatorConjunctions;
        }
        foreach ($methodParameters as $parameterName => $methodParameter) {
            $validatorConjunction = $this->createValidator(ConjunctionValidator::class);
            if (!array_key_exists('type', $methodParameter)) {
                throw new Exception\InvalidTypeHintException('Missing type information, probably no @param annotation for parameter "$' . $parameterName . '" in ' . $className . '->' . $methodName . '()', 1281962564);
            }
            if (strpos($methodParameter['type'], '\\') === false) {
                $typeValidator = $this->createValidator($methodParameter['type']);
            } else {
                $typeValidator = null;
            }
            if ($typeValidator !== null) {
                $validatorConjunction->addValidator($typeValidator);
            }
            $validatorConjunctions[$parameterName] = $validatorConjunction;
        }
        if ($methodValidateAnnotations === null) {
            $validateAnnotations = $this->reflectionService->getMethodAnnotations($className, $methodName, Flow\Validate::class);
            $methodValidateAnnotations = array_map(function ($validateAnnotation) {
                return ['type' => $validateAnnotation->type, 'options' => $validateAnnotation->options, 'argumentName' => $validateAnnotation->argumentName];
            }, $validateAnnotations);
        }
        foreach ($methodValidateAnnotations as $annotationParameters) {
            $newValidator = $this->createValidator($annotationParameters['type'], $annotationParameters['options']);
            if ($newValidator === null) {
                throw new Exception\NoSuchValidatorException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Could not resolve class name for  validator "' . $annotationParameters['type'] . '".', 1239853109);
            }
            if (isset($validatorConjunctions[$annotationParameters['argumentName']])) {
                $validatorConjunctions[$annotationParameters['argumentName']]->addValidator($newValidator);
            } elseif (strpos($annotationParameters['argumentName'], '.') !== false) {
                $objectPath = explode('.', $annotationParameters['argumentName']);
                $argumentName = array_shift($objectPath);
                $validatorConjunctions[$argumentName]->addValidator($this->buildSubObjectValidator($objectPath, $newValidator));
            } else {
                throw new Exception\InvalidValidationConfigurationException('Invalid validate annotation in ' . $className . '->' . $methodName . '(): Validator specified for argument name "' . $annotationParameters['argumentName'] . '", but this argument does not exist.', 1253172726);
            }
        }
        return $validatorConjunctions;
    }

Usage Example

 /**
  * @test
  */
 public function buildMethodArgumentsValidatorConjunctionsReturnsEmptyArrayIfMethodHasNoArguments()
 {
     $mockController = $this->getAccessibleMock(ActionController::class, ['fooAction'], [], '', false);
     $mockReflectionService = $this->getMockBuilder(ReflectionService::class)->disableOriginalConstructor()->getMock();
     $mockReflectionService->expects($this->once())->method('getMethodParameters')->with(get_class($mockController), 'fooAction')->will($this->returnValue([]));
     $this->validatorResolver = $this->getAccessibleMock(ValidatorResolver::class, ['createValidator'], [], '', false);
     $this->validatorResolver->_set('reflectionService', $mockReflectionService);
     $result = $this->validatorResolver->buildMethodArgumentsValidatorConjunctions(get_class($mockController), 'fooAction');
     $this->assertSame([], $result);
 }