本文整理汇总了PHP中Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter::isOptional方法的典型用法代码示例。如果您正苦于以下问题:PHP ParamConverter::isOptional方法的具体用法?PHP ParamConverter::isOptional怎么用?PHP ParamConverter::isOptional使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
的用法示例。
在下文中一共展示了ParamConverter::isOptional方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: apply
/**
* @{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;
}
示例2: apply
/**
* {@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;
}
示例3: apply
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;
}
示例4: getOptions
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;
}
示例5: apply
/**
* @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;
}
示例6: apply
/**
* 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;
}
示例7: apply
/**
* @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);
}
}
示例8: apply
/**
* {@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');
}
}
示例9: apply
/**
* {@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;
}
示例10: apply
/**
* {@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;
}
示例11: apply
/**
* {@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;
}
示例12: apply
/**
* @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;
}