/** * @param ParamConverter $configuration * @return bool */ protected function checkFailureOnValidationError(ParamConverter $configuration) { if (isset($configuration->getOptions()['fail_on_validation_error'])) { return $configuration->getOptions()['fail_on_validation_error']; } return $this->failOnValidationError; }
/** * @param Request $request * @param ParamConverter $configuration * @return string */ protected function getRequestAttributeName(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (array_key_exists('id', $configuration->getOptions())) { $param = $configuration->getOptions()['id']; } return $param; }
public function apply(Request $request, ParamConverter $configuration) { if ($request->isMethod('POST') && !empty($configuration->getOptions()['request_path'])) { $requestPath = $configuration->getOptions()['request_path']; $request->attributes->set($requestPath, $request->request->get($requestPath)); } elseif ($request->isMethod('GET') && !empty($configuration->getOptions()['query_path'])) { $queryPath = $configuration->getOptions()['query_path']; $request->attributes->set($queryPath, $request->query->get($queryPath)); } return parent::apply($request, $configuration); }
public function it_supports_translatable_classes(ParamConverter $paramConverter, TranslatableMetadata $translatableMetadata) { $paramConverter->getOptions()->willReturn(array()); $paramConverter->getClass()->willReturn('TranslatableEntity'); $translatableMetadata->hasTranslatableProperties()->willReturn(true); $this->supports($paramConverter)->shouldReturn(true); }
/** * execute * * @param Request $request * @param SensioParamConverter $configuration * * @return bool|mixed */ public function execute(Request $request, SensioParamConverter $configuration) { $id = $request->attributes->get('id'); $locale = $request->attributes->get('locale'); $url = $request->attributes->get('url'); $name = $configuration->getName(); $options = $configuration->getOptions(); $resolvedClass = $configuration->getClass(); $method = $request->getMethod(); $rawPayload = $request->getContent(); switch (true) { case 'GET' === $method: $convertedValue = $this->loadEntity($resolvedClass, $id, $locale, $url); break; case 'DELETE' === $method: $convertedValue = $this->loadEntity($resolvedClass, $id, $locale, $url); break; case 'PUT' === $method: $payload = array_merge(array('id' => $id), json_decode($rawPayload, true)); $convertedValue = $this->updateEntity($resolvedClass, json_encode($payload)); break; case 'POST' === $method: $convertedValue = $this->updateEntity($resolvedClass, $rawPayload); break; } return $convertedValue; }
/** * Stores the object in the request. * * @param Request $request The request * @param ParamConverter $configuration Contains the name, class and options of the object * * @return boolean True if the object has been successfully set, else false * * @throws UnsupportedMediaTypeHttpException * @throws BadRequestHttpException */ protected function execute(Request $request, ParamConverter $configuration) { $options = (array) $configuration->getOptions(); if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { $context = array_merge($this->context, $options['deserializationContext']); } else { $context = $this->context; } if ($this->serializer instanceof SerializerInterface) { $context = $this->configureDeserializationContext($this->getDeserializationContext(), $context); } try { $object = $this->serializer->deserialize($request->getContent(), $configuration->getClass(), $request->getContentType(), $context); } catch (UnsupportedFormatException $e) { throw new UnsupportedMediaTypeHttpException($e->getMessage()); } catch (JMSSerializerException $e) { throw new BadRequestHttpException($e->getMessage()); } catch (SymfonySerializerException $e) { throw new BadRequestHttpException($e->getMessage()); } $request->attributes->set($configuration->getName(), $object); if (null !== $this->validator) { $validatorOptions = $this->getValidatorOptions($options); $request->attributes->set($this->validationErrorsArgument, $this->validator->validate($object, $validatorOptions['groups'], $validatorOptions['traverse'], $validatorOptions['deep'])); } return true; }
/** * {@inheritdoc} */ public function apply(Request $request, ParamConverter $configuration) { $options = (array) $configuration->getOptions(); if (isset($options['deserializationContext']) && is_array($options['deserializationContext'])) { $arrayContext = array_merge($this->context, $options['deserializationContext']); } else { $arrayContext = $this->context; } $this->configureContext($context = new Context(), $arrayContext); try { $object = $this->serializer->deserialize($request->getContent(), $configuration->getClass(), $request->getContentType(), $context); } catch (UnsupportedFormatException $e) { throw new UnsupportedMediaTypeHttpException($e->getMessage(), $e); } catch (JMSSerializerException $e) { throw new BadRequestHttpException($e->getMessage(), $e); } catch (SymfonySerializerException $e) { throw new BadRequestHttpException($e->getMessage(), $e); } $request->attributes->set($configuration->getName(), $object); if (null !== $this->validator) { $validatorOptions = $this->getValidatorOptions($options); $errors = $this->validator->validate($object, null, $validatorOptions['groups']); $request->attributes->set($this->validationErrorsArgument, $errors); } return true; }
/** * {@inheritdoc} * * @throws NotFoundHttpException When invalid date given */ public function apply(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param)) { return false; } $options = $configuration->getOptions(); $value = $request->attributes->get($param); if (!$value && $configuration->isOptional()) { return false; } if (isset($options['format'])) { $date = DateTime::createFromFormat($options['format'], $value); if (!$date) { throw new NotFoundHttpException('Invalid date given.'); } } else { if (false === strtotime($value)) { throw new NotFoundHttpException('Invalid date given.'); } $date = new DateTime($value); } $request->attributes->set($param, $date); return true; }
/** * {@inheritdoc} */ public function supports(ParamConverter $configuration) { $options = $configuration->getOptions(); if (isset($options['allowAnonymous'])) { return is_bool($options['allowAnonymous']) ? true : false; } return true; }
/** * {@inheritdoc} */ public function supports(ParamConverter $configuration) { $options = $configuration->getOptions(); if (isset($options['multipleIds']) && $options['multipleIds'] === true) { return true; } return false; }
/** * {@inheritdoc} */ public function supports(ParamConverter $configuration) { $options = $configuration->getOptions(); if (isset($options['authenticatedUser']) && is_bool($options['authenticatedUser'])) { return true; } return false; }
/** * @param Request $request * @param ParamConverter $configuration * * @return bool * * @throws \LogicException * @throws NotFoundHttpException * @throws \Exception */ public function apply(Request $request, ParamConverter $configuration) { $classQuery = $configuration->getClass() . 'Query'; $classPeer = $configuration->getClass() . 'Peer'; $this->filters = array(); $this->exclude = array(); if (!class_exists($classQuery)) { throw new \Exception(sprintf('The %s Query class does not exist', $classQuery)); } $tableMap = $classPeer::getTableMap(); $pkColumns = $tableMap->getPrimaryKeyColumns(); if (count($pkColumns) == 1) { $this->pk = strtolower($pkColumns[0]->getName()); } $options = $configuration->getOptions(); // Check route options for converter options, if there are non provided. if (empty($options) && $request->attributes->has('_route') && $this->router && $configuration instanceof ParamConverter) { $converterOption = $this->router->getRouteCollection()->get($request->attributes->get('_route'))->getOption('propel_converter'); if (!empty($converterOption[$configuration->getName()])) { $options = $converterOption[$configuration->getName()]; } } if (isset($options['mapping'])) { // We use the mapping for calling findPk or filterBy foreach ($options['mapping'] as $routeParam => $column) { if ($request->attributes->has($routeParam)) { if ($this->pk === $column) { $this->pk = $routeParam; } else { $this->filters[$column] = $request->attributes->get($routeParam); } } } } else { $this->exclude = isset($options['exclude']) ? $options['exclude'] : array(); $this->filters = $request->attributes->all(); } $this->withs = isset($options['with']) ? is_array($options['with']) ? $options['with'] : array($options['with']) : array(); // find by Pk if (false === ($object = $this->findPk($classQuery, $request))) { // find by criteria if (false === ($object = $this->findOneBy($classQuery, $request))) { if ($configuration->isOptional()) { //we find nothing but the object is optional $object = null; } else { throw new \LogicException('Unable to guess how to get a Propel object from the request information.'); } } } if (null === $object && false === $configuration->isOptional()) { throw new NotFoundHttpException(sprintf('%s object not found.', $configuration->getClass())); } $request->attributes->set($configuration->getName(), $object); return true; }
/** * Stores the object in the request. * * @param Request $request The request * @param ParamConverter $configuration Contains the name, class and options of the object * * @return bool True if the object has been successfully set, else false */ public function apply(Request $request, ParamConverter $configuration) { $options = $configuration->getOptions(); if (!empty($options['parameters'])) { foreach ($options['parameters'] as $parameter) { $request->attributes->set($parameter, $request->get($parameter)); } } return true; }
private function getOptions(ParamConverter $configuration) { $options = array_replace(['model' => $configuration->getClass() . 'Model'], $configuration->getOptions()); if (isset($options['connection'])) { $options['session'] = $this->pomm[$options['session']]; } else { $options['session'] = $this->pomm->getDefaultSession(); } return $options; }
/** * @see \Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface::apply() */ public function apply(Request $request, ParamConverter $configuration) { $options = $configuration->getOptions(); $service_id = isset($options['service_id']) ? $options['service_id'] : $this->getServiceIdForClassName($configuration); $handler = $this->container->get($service_id); $class = $this->handlers[$service_id]; if (!$handler instanceof FormHandlerInterface || get_class($handler) !== $class) { return; } $request->attributes->set($configuration->getName(), $handler); }
/** * @{inheritDoc} */ public function supports(ParamConverter $configuration) { if (!$configuration instanceof ParamConverter) { return false; } $options = $configuration->getOptions(); if (isset($options['orderable']) && $options['orderable'] === true) { return true; } return false; }
/** * 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; }
/** * {@inheritdoc} * * @throws InvalidConfigurationException if the parameter name is missing * @throws AccessDeniedHttpException if the current user is not authenticated */ public function apply(Request $request, ParamConverter $configuration) { $slug = $request->attributes->get('slug'); $options = $configuration->getOptions(); if (isset($options['check_deleted']) && !$options['check_deleted']) { $this->entityManager->getFilters()->disable('softdeleteable'); } $badge = $this->badgeRepository->findBySlug($slug); if (null === $badge) { throw new NotFoundHttpException(); } $parameterName = $configuration->getName(); $request->attributes->set($parameterName, $badge); return true; }
/** * @inheritdoc */ public function apply(Request $request, ParamConverter $configuration) { $isQuery = isset($configuration->getOptions()["query"]); $source = $isQuery ? $request->query->all() : $request->request->all(); $nameMangling = $isQuery ? [self::class, "tableize"] : function ($a) { return $a; }; $class = new \ReflectionClass($configuration->getClass()); $result = $this->converter->convert($source, $class, $nameMangling); if (!$result->getErrors()) { $violations = $this->validator->validate($result->getValue()); if (!$violations->count()) { $request->attributes->set($configuration->getName(), $result->getValue()); } else { throw new RequestValidationException($violations); } } else { throw new RequestConversionException($result->getErrors()); } }
/** * {@inheritdoc} * * @throws NotFoundHttpException When invalid date given */ public function apply(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param)) { return false; } $options = $configuration->getOptions(); $value = $request->attributes->get($param); if (!$value && $configuration->isOptional()) { return false; } $invalidDateMessage = 'Invalid date given.'; try { $date = isset($options['format']) ? Date::createFromFormat($options['format'], $value) : new Date($value); } catch (Exception $e) { throw new NotFoundHttpException($invalidDateMessage); } if (!$date) { throw new NotFoundHttpException($invalidDateMessage); } $request->attributes->set($param, $date); return true; }
/** * {@inheritdoc} * * @throws InvalidArgumentException Thrown, when parameter has been already parsed to array or object. */ public function apply(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param)) { return false; } $options = $configuration->getOptions(); $value = $request->attributes->get($param); if (!$value && $configuration->isOptional()) { return false; } if (is_object($value) || is_array($value)) { throw new InvalidArgumentException('Parameter already parsed to object or array.'); } if (!empty($options['delimiter'])) { $delimiter = $options['delimiter']; } else { $delimiter = ','; } $array = $this->getArrayObject($delimiter, $value); $request->attributes->set($param, $array); return true; }
/** * Stores the object in the request. * * @param Request $request The request * @param ParamConverter $configuration Contains the name, class and options of the object * * @return bool True if the object has been successfully set, else false */ public function apply(Request $request, ParamConverter $configuration) { $name = $configuration->getName(); $value = json_decode($request->getContent(), true); $options = $configuration->getOptions(); if (!empty($options['parameters'])) { foreach ($options['parameters'] as $parameter) { if (isset($value[$parameter])) { $request->attributes->set($parameter, $value[$parameter]); } } } /** * Adding transformed collection * to request attribute. */ $request->attributes->set($name, $value); /** * Alias to access current collection * Used by exception listener */ $request->attributes->set(Alias::DATA, $name); return true; }
protected function getOptions(ParamConverter $configuration) { return array_replace(array('entity_manager' => null, 'exclude' => array(), 'mapping' => array(), 'strip_null' => false), $configuration->getOptions()); }
/** * @param ParamConverter $configuration */ protected function checkConfiguration(ParamConverter $configuration) { if (!isset($configuration->getOptions()['relations'])) { throw new InvalidConfigurationException('Option relations has to be specified for ParamConverter "link".'); } }
protected function getOptions(ParamConverter $configuration) { return array_replace(array('fields' => array()), $configuration->getOptions()); }
/** * @param Request $request * @param ParamConverter $configuration * @param string $mapToField */ private function setMappingOptions(Request $request, ParamConverter $configuration, $mapToField) { $configuration->setOptions(array_merge($configuration->getOptions(), $this->getMappingOptions($request, $configuration, $mapToField))); }
private function getOptions(ParamConverter $configuration) { return array_replace(['limit' => 10, 'min_limit' => 0, 'max_limit' => 500], $configuration->getOptions()); }
/** * Try and find a command for the requested class. * * @param ParamConverter $configuration * * @return array|null Array containing the client and command if found, null if not. * * @throws LogicException */ protected function find(ParamConverter $configuration) { $options = $configuration->getOptions(); // determine the client, if possible if (true === isset($options['client'])) { if (false === isset($this->clients[$options['client']])) { throw new LogicException(sprintf('Unknown client \'%s\'', $options['client'])); } $client = $this->clients[$options['client']]; } elseif (1 === count($this->clients)) { $ids = array_keys($this->clients); $options['client'] = reset($ids); $client = reset($this->clients); } else { $client = null; } // determine the command, if possible if (true === isset($options['command'])) { if (null === $client) { throw new LogicException('Command defined without a client'); } $operations = $client->getDescription()->getOperations(); if (false === isset($operations[$options['command']])) { throw new LogicException(sprintf('Unknown command \'%s\' for client \'%s\'', $options['command'], $options['client'])); } $command = $options['command']; if (false === ($configuration->getClass() === $operations[$options['command']]->getResponseClass())) { throw new LogicException(sprintf('Command \'%s\' return \'%s\' rather than \'%s\'', $options['command'], $operations[$options['command']]->getResponseClass(), $configuration->getClass())); } } else { $command = null; } // if we don't know the command yet, try and find it if (null === $command) { if (null !== $client) { $searchClients = array($options['client'] => $client); } else { $searchClients = $this->clients; } foreach ($searchClients as $thisClient) { if (null === $thisClient->getDescription()) { continue; } foreach ($thisClient->getDescription()->getOperations() as $operation) { if ('GET' === $operation->getHttpMethod() && $configuration->getClass() === $operation->getResponseClass()) { $client = $thisClient; $command = $operation->getName(); break 2; } } } } if (null !== $client && null !== $command) { return array('client' => $client, 'command' => $command); } else { return null; } }
/** * @param Request $request * @param ParamConfiguration $configuration * @return BookReadModel * @throws NotFoundHttpException */ protected function convertBook(Request $request, ParamConfiguration $configuration) { $uuid = $request->attributes->get($configuration->getOptions()['id']); return $this->getBook($uuid); }
private function getOptions(ParamConverter $configuration) { return array_replace(['sort_order' => 'desc'], $configuration->getOptions()); }