/**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     // Try get object annotation from class
     $objectAnnotation = null;
     $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class, true);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof ObjectAnnotation) {
             if ($objectAnnotation) {
                 throw new \RuntimeException(sprintf('Many @Transformer\\Object annotation in class "%s".', $class));
             }
             $objectAnnotation = $classAnnotation;
         }
     }
     if (!$objectAnnotation) {
         throw new TransformAnnotationNotFoundException(sprintf('Not found @Object annotations in class "%s".', $class));
     }
     // Try get properties annotations
     $properties = [];
     $classProperties = Reflection::getClassProperties($class, true);
     foreach ($classProperties as $classProperty) {
         $propertyAnnotations = $this->reader->getPropertyAnnotations($classProperty);
         foreach ($propertyAnnotations as $propertyAnnotation) {
             if ($propertyAnnotation instanceof PropertyAnnotation) {
                 $property = new PropertyMetadata($propertyAnnotation->propertyName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldTransform, $propertyAnnotation->expressionValue);
                 $properties[$classProperty->getName()] = $property;
             }
         }
     }
     return new ObjectMetadata($objectAnnotation->transformedClass, $properties);
 }
 /**
  * {@inheritDoc}
  */
 public function loadActions()
 {
     $actions = new ActionCollection();
     foreach ($this->classes as $id => $class) {
         $reflection = Reflection::loadClassReflection($class);
         // Get all methods from class
         $methods = $reflection->getMethods(\ReflectionMethod::IS_PUBLIC);
         foreach ($methods as $method) {
             $methodAnnotations = $this->reader->getMethodAnnotations($method);
             foreach ($methodAnnotations as $annotation) {
                 if ($annotation instanceof ActionAnnotation) {
                     if ($method->isStatic()) {
                         throw new \RuntimeException('The static method not supported (@todo).');
                     }
                     if ($annotation->response) {
                         $response = new ObjectResponse($annotation->response->class);
                     } else {
                         $response = null;
                     }
                     $action = new ServiceAction($annotation->name, $id, $method->getName(), $annotation->validationGroups, $annotation->securityGroups, $annotation->requestMappingGroup, $annotation->useStrictValidation, $annotation->checkEnabled, $response);
                     $actions->addAction($action);
                 }
             }
         }
     }
     return $actions;
 }
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     if (!is_array($data)) {
         throw DenormalizationFailedException::unexpected($data, 'array');
     }
     $collectionClass = $context->getAttribute('collection_class');
     $class = $context->getAttribute('class');
     if (!$class) {
         throw new DenormalizationFailedException('Undefined denormalizer class.');
     }
     try {
         $this->normalizerManager->getNormalizerForClass($class);
     } catch (UnsupportedClassException $e) {
         throw new DenormalizationFailedException(sprintf('Not found normalizer for denormalize collection. Collection class "%s". Denormalizer class "%s".', $collectionClass, $class), 0, $e);
     }
     if ($collectionClass) {
         $reflection = Reflection::loadClassReflection($collectionClass);
         $collection = $reflection->newInstanceWithoutConstructor();
     } else {
         $collection = [];
     }
     if (is_object($collection) && !$collection instanceof \ArrayAccess) {
         throw new DenormalizationFailedException(sprintf('The collection instance for denormalize data should implement "ArrayAccess", but "%s" given.', get_class($collection)));
     }
     foreach ($data as $key => $childData) {
         $denormalized = $this->normalizerManager->denormalize($class, $childData);
         $collection[$key] = $denormalized;
     }
     return $collection;
 }
 /**
  * {@inheritDoc}
  */
 public function load($object, $group)
 {
     $reflection = Reflection::loadClassReflection($object);
     $objectMappingAnnotation = $this->getObjectMappingAnnotation($reflection, $group);
     if ($objectMappingAnnotation && $objectMappingAnnotation->allProperties) {
         // Include all properties
         $propertiesMappingAnnotation = $this->getAllPropertiesMapping($reflection, $objectMappingAnnotation->group);
     } else {
         $propertiesMappingAnnotation = $this->getPropertiesMappingAnnotation($reflection, $group);
     }
     if (!$objectMappingAnnotation && !$propertiesMappingAnnotation) {
         return null;
     }
     if (!$objectMappingAnnotation) {
         $strategy = 'reflection';
     } else {
         $strategy = $objectMappingAnnotation->strategy;
     }
     $properties = array();
     foreach ($propertiesMappingAnnotation as $propertyName => $propertyAnnotation) {
         $fieldName = $propertyAnnotation->fieldName ?: $propertyName;
         if ($propertyAnnotation->collection) {
             $collection = new CollectionMetadata($propertyAnnotation->collection->class, $propertyAnnotation->collection->saveKeys);
         } else {
             $collection = null;
         }
         $property = new PropertyMetadata($propertyName, $fieldName, $propertyAnnotation->class, $collection);
         $properties[] = $property;
     }
     $objectMetadata = new ObjectMetadata($strategy, $properties);
     return $objectMetadata;
 }
 /**
  * {@inheritDoc}
  */
 public function loadForMethod($class, $method, $group)
 {
     $methodReflection = Reflection::loadMethodReflection($class, $method);
     $methodAnnotations = $this->reader->getMethodAnnotations($methodReflection);
     $securityMethodAnnotation = null;
     $rules = array();
     foreach ($methodAnnotations as $methodAnnotation) {
         if ($methodAnnotation instanceof MethodSecurityAnnotation && $group == $methodAnnotation->group) {
             if ($securityMethodAnnotation) {
                 throw new \RuntimeException(sprintf('The @MethodSecurity annotation already defined in method "%s::%s".', $class, $method));
             }
             $securityMethodAnnotation = $methodAnnotation;
         }
         if ($rule = $this->transformAnnotationToRule($methodAnnotation, $group, $class)) {
             $rules[] = $rule;
         }
     }
     if (!$securityMethodAnnotation && !count($rules)) {
         return null;
     }
     if ($securityMethodAnnotation) {
         $strategy = $securityMethodAnnotation->strategy;
     } else {
         $strategy = Security::STRATEGY_AFFIRMATIVE;
     }
     $securityMethod = new MethodSecurity($class, $method, $strategy, $rules, $group);
     return $securityMethod;
 }
 /**
  * {@inheritDoc}
  */
 public function convertParameter(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $method, $value, $group, array $attributes = [])
 {
     $converter = $this->getConverter($group);
     if (!$converter->isSupported($parameter, $method)) {
         throw new ConverterNotSupportedException(sprintf('The parameter converter "%s" not supported in function "%s".', $parameter->getName(), Reflection::getCalledMethod($method)));
     }
     $value = $converter->convert($parameter, $method, $value, $attributes);
     return $value;
 }
 /**
  * {@inheritDoc}
  */
 public function convert(\ReflectionParameter $parameter, \ReflectionFunctionAbstract $method, $value, array $attributes = [])
 {
     foreach ($this->converters as $converter) {
         if (true === $converter->isSupported($parameter, $method)) {
             return $converter->convert($parameter, $method, $value, $attributes);
         }
     }
     throw new ConverterNotFoundException(sprintf('Not found parameter converter for argument "%s" in function "%s".', $parameter->getName(), Reflection::getCalledMethod($method)));
 }
 /**
  * {@inheritDoc}
  */
 public function resolve(ActionInterface $action)
 {
     if (!$action instanceof ServiceAction) {
         throw UnexpectedTypeException::create($action, 'FivePercent\\Bundle\\ApiBundle\\SMD\\Action\\ServiceAction');
     }
     $serviceId = $action->getServiceId();
     $method = $action->getMethod();
     $service = $this->container->get($serviceId);
     $reflectionService = Reflection::loadClassReflection($service);
     $reflectionMethod = $reflectionService->getMethod($method);
     return new BaseCallable($reflectionMethod, $service);
 }
 /**
  * {@inheritDoc}
  */
 protected function getPropertiesForClass(ActionInterface $action, CallableInterface $callable, $class)
 {
     if (!$this->metadataFactory->supportsClass($class)) {
         return [];
     }
     $metadata = $this->metadataFactory->loadMetadata($class);
     $reflectionClass = Reflection::loadClassReflection($class);
     $properties = [];
     foreach ($metadata->getProperties() as $propertyName => $property) {
         $properties[] = $reflectionClass->getProperty($propertyName);
     }
     return $properties;
 }
 /**
  * {@inheritDoc}
  */
 public function convertProperties($object, $group)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     $converter = $this->getConverter($group);
     $classReflection = Reflection::loadClassReflection($object);
     $properties = $classReflection->getProperties();
     foreach ($properties as $property) {
         if ($converter->isSupported($property)) {
             $this->convertPropertyReflection($object, $property, $group);
         }
     }
 }
 /**
  * {@inheritDoc}
  */
 public function map(PropertyMetadata $property, $object, $value)
 {
     if (!is_object($object)) {
         throw UnexpectedTypeException::create($object, 'object');
     }
     if (!$property->reflection) {
         $objectReflection = Reflection::loadObjectReflection($object);
         $propertyName = $property->getPropertyName();
         $propertyReflection = $objectReflection->getProperty($propertyName);
         if (!$propertyReflection->isPublic()) {
             $propertyReflection->setAccessible(true);
         }
         $property->reflection = $propertyReflection;
     }
     $property->reflection->setValue($object, $value);
 }
 /**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     // Try get @Object annotation
     $objectAnnotation = null;
     $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class);
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof ObjectAnnotation) {
             if ($objectAnnotation) {
                 throw new \RuntimeException(sprintf('Many @Normalize\\Object annotations in class "%s".', $class));
             }
             $objectAnnotation = $classAnnotation;
         }
     }
     // Try get @Property annotation from properties
     $properties = [];
     $classProperties = Reflection::getClassProperties($class, true);
     if ($objectAnnotation && $objectAnnotation->allProperties) {
         foreach ($classProperties as $classProperty) {
             /** @var PropertyAnnotation $propertyAnnotation */
             $propertyAnnotation = $this->reader->getPropertyAnnotation($classProperty, 'FivePercent\\Component\\ModelNormalizer\\Annotation\\Property');
             if ($propertyAnnotation) {
                 $properties[$classProperty->getName()] = new PropertyMetadata($propertyAnnotation->fieldName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldNormalize, $propertyAnnotation->expressionValue, $propertyAnnotation->denormalizerClass);
             } else {
                 $properties[$classProperty->getName()] = new PropertyMetadata($classProperty->getName(), [], false, null);
             }
         }
     } else {
         foreach ($classProperties as $classProperty) {
             $propertyAnnotations = $this->reader->getPropertyAnnotations($classProperty);
             foreach ($propertyAnnotations as $propertyAnnotation) {
                 if ($propertyAnnotation instanceof PropertyAnnotation) {
                     $properties[$classProperty->getName()] = new PropertyMetadata($propertyAnnotation->fieldName ?: $classProperty->getName(), $propertyAnnotation->groups, $propertyAnnotation->shouldNormalize, $propertyAnnotation->expressionValue, $propertyAnnotation->denormalizerClass);
                 }
             }
         }
     }
     if (!count($properties) && !$objectAnnotation) {
         throw new NormalizeAnnotationNotFoundException(sprintf('Not found normalize annotations in class "%s".', $class));
     }
     return new ObjectMetadata($properties);
 }
 /**
  * {@inheritDoc}
  */
 protected function execute(InputInterface $input, OutputInterface $output)
 {
     $handlerRegistry = $this->getContainer()->get('api.handler_registry');
     $callableResolver = $this->getContainer()->get('api.callable_resolver');
     $projectDirectory = realpath($this->getContainer()->getParameter('kernel.root_dir') . '/..');
     $handlerKeys = $handlerRegistry->getHandlerKeys();
     if ($input->getArgument('handler')) {
         $handlerKeys = array_diff($handlerKeys, array_diff($handlerKeys, $input->getArgument('handler')));
     }
     if (!count($handlerKeys)) {
         $output->writeln('Not found handlers.');
         return 0;
     }
     foreach ($handlerKeys as $handlerKey) {
         $handler = $handlerRegistry->getHandler($handlerKey);
         $actions = $handler->getActions();
         /** @var \Symfony\Component\Console\Helper\Table $table */
         $table = $this->getHelper('table');
         $table->setHeaders(['Name', 'Callable', 'File / Line']);
         $rows = [];
         foreach ($actions as $action) {
             try {
                 $callable = $callableResolver->resolve($action);
             } catch (\Exception $e) {
                 $rows[] = [$action->getName(), '<error>Not supported</error>', ''];
                 continue;
             }
             $reflection = $callable->getReflection();
             $file = str_replace($projectDirectory, '', $reflection->getFileName());
             $callableName = Reflection::getCalledMethod($callable->getReflection(), false);
             $fileAndLine = sprintf('%s on lines %d:%d', ltrim($file, '/'), $reflection->getStartLine(), $reflection->getEndLine());
             $rows[] = [$action->getName(), $callableName, $fileAndLine];
         }
         $output->writeln(sprintf('Action list for handler <info>%s</info>:', $handlerKey));
         $table->setRows($rows);
         $table->render($output);
         $output->writeln([null, null]);
     }
     return 0;
 }
 /**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     $notifications = [];
     $classAnnotations = Reflection::loadClassAnnotations($this->reader, $class);
     $withoutOnEventParameter = false;
     foreach ($classAnnotations as $classAnnotation) {
         if ($classAnnotation instanceof NotificationAnnotation) {
             if (!$classAnnotation->onEvent) {
                 $withoutOnEventParameter = true;
             }
             $notification = new NotificationMetadata($classAnnotation->name, $classAnnotation->strategy, $classAnnotation->onEvent);
             $notifications[] = $notification;
         }
     }
     if (count($notifications) > 1 && $withoutOnEventParameter) {
         throw new \RuntimeException(sprintf('The parameter "onEvent" is required, if you use many @Notification annotation in class "%s".', $class));
     }
     if (count($notifications) == 0) {
         throw new NotificationAnnotationNotFoundException(sprintf('Not found @Notification annotation in class "%s".', $class));
     }
     return new ClassMetadata($notifications);
 }
 /**
  * {@inheritDoc}
  */
 public function loadMetadata($class)
 {
     $properties = Reflection::getClassProperties($class, true);
     $metadataProperties = [];
     foreach ($properties as $property) {
         $docBlock = new DocBlock($property);
         $docVarTags = $docBlock->getTagsByName('var');
         if (count($docVarTags)) {
             /** @var \phpDocumentor\Reflection\DocBlock\Tag\VarTag $docVarTag */
             $docVarTag = array_pop($docVarTags);
             $types = $docVarTag->getTypes();
             $types = array_map(function ($type) {
                 return ltrim($type, '\\');
             }, $types);
             if (count($types)) {
                 $propertyMetadata = new PropertyMetadata($types);
                 $metadataProperties[$property->getName()] = $propertyMetadata;
             }
         }
     }
     $classMetadata = new ClassMetadata($metadataProperties);
     return $classMetadata;
 }
 /**
  * {@inheritDoc}
  */
 public function transform($object, ContextInterface $context)
 {
     $metadata = $this->metadataFactory->loadMetadata(get_class($object));
     // Get properties for transformation
     if (!$context->getGroups()) {
         $transformProperties = $metadata->getProperties();
     } else {
         $transformProperties = $metadata->getPropertiesForGroups($context->getGroups());
     }
     // Try create transformed
     $transformedClass = $metadata->getTransformedClass();
     $transformedReflection = Reflection::loadClassReflection($transformedClass);
     $transformed = $transformedReflection->newInstanceWithoutConstructor();
     $objectReflection = Reflection::loadClassReflection($object);
     foreach ($transformProperties as $transformPropertyName => $propertyMetadata) {
         try {
             $objectPropertyReflection = $objectReflection->getProperty($transformPropertyName);
         } catch (\ReflectionException $e) {
             throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $transformPropertyName, $objectReflection->getName()), 0, $e);
         }
         try {
             $transformedPropertyReflection = $transformedReflection->getProperty($propertyMetadata->getPropertyName());
         } catch (\ReflectionException $e) {
             throw new \RuntimeException(sprintf('Error transform property: Not found property "%s" in class "%s".', $propertyMetadata->getPropertyName(), $transformedReflection->getName()));
         }
         if (!$transformedPropertyReflection->isPublic()) {
             $transformedPropertyReflection->setAccessible(true);
         }
         if (!$objectPropertyReflection->isPublic()) {
             $objectPropertyReflection->setAccessible(true);
         }
         $objectPropertyValue = $objectPropertyReflection->getValue($object);
         $transformedValue = $this->transformValue($object, $objectPropertyValue, $propertyMetadata, $transformedPropertyReflection);
         $transformedPropertyReflection->setValue($transformed, $transformedValue);
     }
     return $transformed;
 }
 /**
  * Get converter annotation
  *
  * @param \ReflectionParameter $parameter
  * @param \ReflectionMethod    $method
  *
  * @return ORM
  *
  * @throws ORMAnnotationNotFoundException
  */
 private function getConverterAnnotation(\ReflectionParameter $parameter, \ReflectionMethod $method)
 {
     $annotations = $this->reader->getMethodAnnotations($method);
     foreach ($annotations as $annotation) {
         if ($annotation instanceof ORM) {
             // Check parameter name
             if ($annotation->name === $parameter->name) {
                 return $annotation;
             }
             // Check classes
             if ($class = $parameter->getClass()) {
                 if (is_a($class->getName(), $annotation->entityClass, true)) {
                     return $annotation;
                 }
             }
             if (!$annotation->name && !$annotation->entityClass) {
                 throw new \RuntimeException(sprintf('Invalid @ORM annotation. The name or entity class must be specified in method "%s".', Reflection::getCalledMethod($method)));
             }
         }
     }
     throw new ORMAnnotationNotFoundException(sprintf('Not found ORM annotation for parameter "%s" in method "%s".', $parameter->getName(), Reflection::getCalledMethod($method)));
 }
 /**
  * Create a new object by class
  *
  * @param string $class
  *
  * @return object
  */
 protected function createObjectFromClass($class)
 {
     $reflection = Reflection::loadClassReflection($class);
     if (!$reflection->isUserDefined()) {
         // PHP System class
         return $reflection->newInstance();
     }
     $constructor = $reflection->getConstructor();
     if (!$constructor) {
         // Constructor not found
         return $reflection->newInstanceWithoutConstructor();
     }
     $constructorParameters = $constructor->getParameters();
     // Is constructor has required parameter
     $constructorHasRequiredParameter = false;
     foreach ($constructorParameters as $constructorParameter) {
         if (!$constructorParameter->isOptional()) {
             $constructorHasRequiredParameter = true;
             break;
         }
     }
     if ($constructorHasRequiredParameter) {
         return $reflection->newInstanceWithoutConstructor();
     } else {
         return $reflection->newInstance();
     }
 }
Example #19
0
 /**
  * {@inheritDoc}
  */
 public function handle($method, array $parameters)
 {
     try {
         // Get action and callable
         $action = $this->actionManager->getAction($method);
         $callable = $this->callableResolver->resolve($action);
         // Resolve parameters
         $parameters = $this->parameterResolver->resolve($action, $callable, $parameters);
         // Dispatch "pre dispatch" event
         $event = new ActionDispatchEvent($action, $callable, $parameters);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_PRE_DISPATCH, $event);
         // Call to API method
         $response = $callable->apply($parameters);
         if (null === $response) {
             throw new \RuntimeException(sprintf('The callable "%s" should be return Response or any values. Can not be empty.', Reflection::getCalledMethod($callable->getReflection())));
         }
         if (!$response instanceof ResponseInterface) {
             // Try transform in listeners.
             $event = new ActionViewEvent($action, $callable, $parameters, $response);
             $this->eventDispatcher->dispatch(ApiEvents::ACTION_VIEW, $event);
             $response = $event->getResponse();
             if (!$response) {
                 throw new \RuntimeException(sprintf('Not found response after dispatch view event in API method "%s". ' . 'You must return response or transform response in view event. ' . 'Maybe not found transformer for object?', Reflection::getCalledMethod($callable->getReflection())));
             }
             if (!$response instanceof ResponseInterface) {
                 throw new \RuntimeException(sprintf('The response after dispatch view event must be a ResponseInterface instance, but "%s" given ' . 'for method "%s".', is_object($response) ? get_class($response) : gettype($response)));
             }
         }
         $event = new ActionDispatchEvent($action, $callable, $parameters, $response);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_POST_DISPATCH, $event);
         return $response;
     } catch (\Exception $e) {
         $event = new ActionExceptionEvent(isset($action) ? $action : null, $e);
         $this->eventDispatcher->dispatch(ApiEvents::ACTION_EXCEPTION, $event);
         if ($event->hasResponse()) {
             return $event->getResponse();
         }
         throw $e;
     }
 }
 /**
  * Test with child collection
  */
 public function testWithChildCollection()
 {
     $object = new WithCollectionObject();
     $this->objectMapper->map($object, ['name' => 'Foo', 'simpleObject' => ['name' => 'Simple object'], 'simpleObjects' => [['name' => '#1'], ['name' => '#2']]]);
     $this->assertEquals('Foo', Reflection::getPropertyValue($object, 'name'));
     $simpleObject = Reflection::getPropertyValue($object, 'simpleObject');
     $this->assertInstanceOf('FivePercent\\Component\\ObjectMapper\\Tests\\Mapping\\SimpleObject', $simpleObject);
     $this->assertEquals('Simple object', Reflection::getPropertyValue($simpleObject, 'name'));
     $simpleObjects = Reflection::getPropertyValue($object, 'simpleObjects');
     $this->assertInstanceOf('ArrayAccess', $simpleObjects);
     $this->assertFalse(empty($simpleObjects[0]));
     $this->assertFalse(empty($simpleObjects[1]));
     $object1 = $simpleObjects[0];
     $this->assertInstanceOf('FivePercent\\Component\\ObjectMapper\\Tests\\Mapping\\SimpleObject', $object1);
     $this->assertEquals('#1', Reflection::getPropertyValue($object1, 'name'));
     $object2 = $simpleObjects[1];
     $this->assertInstanceOf('FivePercent\\Component\\ObjectMapper\\Tests\\Mapping\\SimpleObject', $object2);
     $this->assertEquals('#2', Reflection::getPropertyValue($object2, 'name'));
 }
 /**
  * Resolve request parameter
  *
  * @param ActionInterface      $action
  * @param CallableInterface    $callable
  * @param array                $inputArguments
  * @param \ReflectionParameter $parameter
  *
  * @return object
  *
  * @throws \Exception
  */
 private function resolveRequest(ActionInterface $action, CallableInterface $callable, array $inputArguments, \ReflectionParameter $parameter)
 {
     $class = $parameter->getClass();
     if ($class->isInterface()) {
         throw new \RuntimeException(sprintf('Could not create instance via interface for parameter "%s" in method "%s". ' . 'You must set the class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection())));
     }
     if ($class->isAbstract()) {
         throw new \RuntimeException(sprintf('Could not create instance via abstract class for parameter "%s" in method "%s". ' . 'You must set the real class for type hinting.', $parameter->getName(), Reflection::getCalledMethod($callable->getReflection())));
     }
     /** @var RequestInterface $request */
     $request = $class->newInstance();
     // Map arguments
     $this->objectMapper->map($request, $inputArguments, $action->getRequestMappingGroup());
     // First step validation: Strict mode
     if ($this->validator && $action->isStrictValidation()) {
         $this->strictRequestValidate($request);
     }
     // Second step validation: Base validation
     if ($this->validator && $action->getValidationGroups()) {
         $violationList = $this->validator->validate($request, null, $action->getValidationGroups());
         if (count($violationList)) {
             throw ViolationListException::create($violationList);
         }
     }
     // Convert request properties
     if ($this->propertyConverter) {
         try {
             $this->propertyConverter->convertProperties($request, PropertyConverterManagerInterface::GROUP_DEFAULT);
         } catch (InvalidArgumentException $e) {
             $constraintViolation = new ConstraintViolation($e->getMessage(), null, [], null, $e->getName(), $e->getInvalidValue());
             $constraintViolationList = new ConstraintViolationList([$constraintViolation]);
             throw ViolationListException::create($constraintViolationList);
         } catch (ConverterNotFoundException $e) {
             if ($this->logger) {
                 $this->logger->warning(sprintf('Could not convert properties with message: %s.', rtrim($e->getMessage(), '.')));
             }
             // Nothing action
         }
     }
     return $request;
 }
 /**
  * Validate property value by types
  *
  * @param object $object
  * @param string $propertyName
  * @param array  $types
  *
  * @return null|\Symfony\Component\Validator\ConstraintViolationInterface[]
  */
 private function validatePropertyValueByTypes($object, $propertyName, array $types)
 {
     $classReflection = Reflection::loadClassReflection($object);
     $property = $classReflection->getProperty($propertyName);
     if (!$property->isPublic()) {
         $property->setAccessible(true);
     }
     $value = $property->getValue($object);
     if (!$value) {
         return null;
     }
     $firstViolationList = null;
     foreach ($types as $type) {
         try {
             $varTagConstraint = $this->constraintFactoryRegistry->getConstraintFactory($type)->getVarTagConstraint();
         } catch (ConstraintFactoryNotFoundException $e) {
             continue;
         }
         $constraints = $varTagConstraint->getConstraints();
         $groupSequence = $varTagConstraint->getGroupSequence();
         if (count($constraints)) {
             $violationList = $this->validator->validate($value, $constraints, $groupSequence);
             if (count($violationList)) {
                 if (!$firstViolationList) {
                     $firstViolationList = $violationList;
                 }
             } else {
                 return null;
             }
         }
     }
     return $firstViolationList;
 }
 /**
  * {@inheritDoc}
  */
 public function denormalize($data, ContextInterface $context)
 {
     $class = $context->getAttribute('_class');
     if (!$class) {
         throw new DenormalizationFailedException('Undefined class for denormalization');
     }
     $metadata = $this->metadataFactory->loadMetadata($class);
     if ($context->getGroups()) {
         $denormalizeProperties = $metadata->getPropertiesForGroups($context->getGroups());
     } else {
         $denormalizeProperties = $metadata->getProperties();
     }
     $classReflection = Reflection::loadClassReflection($class);
     if ($object = $context->getAttribute('_object')) {
         if (!is_object($object)) {
             throw UnexpectedTypeException::create($object, 'object');
         }
         if (get_class($object) != $classReflection && !is_a($object, $class)) {
             throw UnexpectedTypeException::create($object, $class);
         }
     } else {
         $object = $classReflection->newInstanceWithoutConstructor();
     }
     foreach ($denormalizeProperties as $denormalizePropertyName => $propertyMetadata) {
         $fieldName = $propertyMetadata->getFieldName();
         if (!isset($data[$fieldName])) {
             continue;
         }
         $objectPropertyReflection = $classReflection->getProperty($denormalizePropertyName);
         if (!$objectPropertyReflection->isPublic()) {
             $objectPropertyReflection->setAccessible(true);
         }
         $denormalizedValue = $this->denormalizeValue($data[$fieldName], $propertyMetadata, $objectPropertyReflection);
         $objectPropertyReflection->setValue($object, $denormalizedValue);
     }
     return $object;
 }
 /**
  * Get properties for class
  *
  * @param ActionInterface   $action
  * @param CallableInterface $callable
  * @param string            $class
  *
  * @return \ReflectionProperty[]
  */
 protected function getPropertiesForClass(ActionInterface $action, CallableInterface $callable, $class)
 {
     return Reflection::getClassProperties($class);
 }
 /**
  * On pre dispatch event
  *
  * @param ActionDispatchEvent $event
  */
 public function onPreDispatch(ActionDispatchEvent $event)
 {
     $message = sprintf('Match callable "%s" for action "%s".', Reflection::getCalledMethod($event->getCallable()->getReflection()), $event->getAction()->getName());
     $this->logger->debug($message);
 }