/** * @{inheritDoc} * * @throws InvalidConfigurationException if the parameter name, class or id option are missing * @throws NotFoundHttpException if the id doesn't matche an existing entity */ public function apply(Request $request, ParamConverter $configuration) { if (null === ($parameter = $configuration->getName())) { throw new InvalidConfigurationException(InvalidConfigurationException::MISSING_NAME); } if (null === ($entityClass = $configuration->getClass())) { throw new InvalidConfigurationException(InvalidConfigurationException::MISSING_CLASS); } $options = $configuration->getOptions(); if (!isset($options['id'])) { throw new InvalidConfigurationException(InvalidConfigurationException::MISSING_ID); } if ($request->attributes->has($options['id'])) { if (null !== ($id = $request->attributes->get($options['id']))) { if (null !== ($entity = $this->em->getRepository($entityClass)->find($id))) { $request->attributes->set($parameter, $entity); return true; } } if (!$configuration->isOptional()) { throw new NotFoundHttpException(); } } return false; }
/** * {@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; }
public function apply(Request $request, ParamConverter $configuration) { $entity = $this->findByRequestParameter($request); if (!$configuration->isOptional() && null === $entity) { throw new NotFoundHttpException(sprintf('Entity of type "%s" was not found', $configuration->getClass())); } $param = $configuration->getName(); $request->attributes->set($param, $entity); 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(); } $options["optional"] = $configuration->isOptional(); return $options; }
/** * @param Request $request * @param ParamConverter $configuration * * @return bool */ public function apply(Request $request, ParamConverter $configuration) { if (!$request->attributes->has($this->getPropertyName())) { return false; } $valueObjectId = $request->attributes->get($this->getPropertyName()); if (!$valueObjectId && $configuration->isOptional()) { return false; } $request->attributes->set($configuration->getName(), $this->loadValueObject($valueObjectId)); return true; }
/** * Reads the 'q' attribute of the request and returns a menu item array.. * * @param Request $request The request * @param ParamConverter $configuration Contains the name, class and options of the object * * @throws NotFoundHttpException * * @return bool True if the object has been successfully set, else false */ public function apply(Request $request, ParamConverter $configuration) { if (!$request->attributes->has('q')) { return false; } $value = $request->attributes->get('q'); if (!$value && $configuration->isOptional()) { return false; } $router_item = menu_get_item($value); if (!$router_item) { throw new NotFoundHttpException('Entity not found.'); } $param = $configuration->getName(); $request->attributes->set($param, $router_item); return true; }
/** * @param Request $request * @param ParamConverter $configuration * * @throws NotFoundHttpException if value object was not found * @throws AccessDeniedHttpException if user is not allowed to load the value object * * @return bool */ public function apply(Request $request, ParamConverter $configuration) { if (!$request->attributes->has($this->getPropertyName())) { return false; } $valueObjectId = $request->attributes->get($this->getPropertyName()); if (!$valueObjectId && $configuration->isOptional()) { return false; } try { $request->attributes->set($configuration->getName(), $this->loadValueObject($valueObjectId)); return true; } catch (NotFoundException $e) { throw new NotFoundHttpException('Requested values not found', $e); } catch (UnauthorizedException $e) { throw new AccessDeniedHttpException('Access to values denied', $e); } }
/** * {@inheritdoc} * * @throws NotFoundHttpException When invalid uuid given */ public function apply(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param)) { return false; } $value = $request->attributes->get($param); if (!$value && $configuration->isOptional()) { return false; } try { $uuid = Uuid::fromString($value); $request->attributes->set($param, $uuid); return true; } catch (\InvalidArgumentException $e) { throw new NotFoundHttpException('Invalid uuid given'); } }
/** * {@inheritDoc} */ public function apply(Request $request, ParamConverter $configuration) { $param = $configuration->getName(); if (!$request->attributes->has($param) || !$request->attributes->has('entity_type')) { return false; } $entityType = $request->attributes->get('entity_type'); $value = $request->attributes->get($param); if (!$value && $configuration->isOptional()) { return false; } $entities = entity_load($entityType, array($value)); $entity = reset($entities); if (empty($entity)) { throw new NotFoundHttpException('Entity not found.'); } $request->attributes->set($param, $entity); 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; }
/** * {@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; }
/** * @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; }