/** * Stores the object in the request. * * @param Request $request The request * @param ParamConverter $configuration Contains the name, class and options of the object * * @throws \InvalidArgumentException * * @return bool True if the object has been successfully set, else false */ public function apply(Request $request, ParamConverter $configuration) { $name = $configuration->getName(); $class = $configuration->getClass(); $options = $configuration->getOptions(); if (isset($options['query'])) { $content = new \stdClass(); $metadata = $this->serializer->getMetadataFactory()->getMetadataForClass($class); foreach ($metadata->propertyMetadata as $propertyMetadata) { if (!$propertyMetadata->readOnly) { $property = $propertyMetadata->name; $value = $request->query->get($propertyMetadata->name); if (!is_null($value)) { $content->{$property} = $request->query->get($propertyMetadata->name); } } } $content = json_encode($content); } else { $content = $request->getContent(); } if (!class_exists($class)) { throw new \InvalidArgumentException($class . ' class does not exist.'); } $success = false; try { $model = $this->serializer->deserialize($content, $class, 'json'); $success = true; } catch (\Exception $e) { $model = new $class(); } /** * Validate if possible */ if ($model instanceof ValidatableInterface) { $violations = $this->validator->validate($model); $valid = $success && !(bool) $violations->count(); $model->setViolations($violations); $model->setValid($valid); } /** * Adding transformed collection * to request attribute. */ $request->attributes->set($name, $model); /** * Alias to access current collection * Used by exception listener */ $request->attributes->set(Alias::DATA, $name); return true; }
public function __construct(Serializer $serializer, array $bestReferencedFieldChoices) { $this->metadataFactory = $serializer->getMetadataFactory(); $this->bestReferencedFieldChoices = $bestReferencedFieldChoices; }
public function __construct(Serializer $serializer, PropertyNamingStrategyInterface $namingStrategy) { $this->metadataFactory = $serializer->getMetadataFactory(); $this->namingStrategy = $namingStrategy; }