/** * Reorder the children of the parent form data at $this->name. * * For whatever reason we have to go through the parent object, just * getting the collection from the form event and reordering it does * not update the stored order. * * @param FormEvent $event */ public function onSubmit(FormEvent $event) { $form = $event->getForm()->getParent(); $data = $form->getData(); if (!is_object($data)) { return; } $accessor = PropertyAccess::getPropertyAccessor(); // use deprecated BC method to support symfony 2.2 $newCollection = $accessor->getValue($data, $this->name); if (!$newCollection instanceof Collection) { return; } /* @var $newCollection Collection */ $newCollection->clear(); /** @var $item FormBuilder */ foreach ($form->get($this->name) as $key => $item) { if ($item->get('_delete')->getData()) { // do not re-add a deleted child continue; } if ($item->getName() && !is_numeric($item->getName())) { // keep key in collection $newCollection[$item->getName()] = $item->getData(); } else { $newCollection[] = $item->getData(); } } }
/** * Renders a pagerfanta. * * @param PagerfantaInterface $pagerfanta The pagerfanta. * @param string $viewName The view name. * @param array $options An array of options (optional). * * @return string The pagerfanta rendered. */ public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = null, array $options = array()) { $options = array_replace(array('routeName' => null, 'routeParams' => array(), 'pageParameter' => '[page]', 'queryString' => null), $options); if (null === $viewName) { $viewName = $this->container->getParameter('white_october_pagerfanta.default_view'); } $router = $this->container->get('router'); if (null === $options['routeName']) { $request = $this->container->get('request'); $options['routeName'] = $request->attributes->get('_route'); if ('_internal' === $options['routeName']) { throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest'); } $options['routeParams'] = array_merge($request->query->all(), $request->attributes->get('_route_params')); } $routeName = $options['routeName']; $routeParams = $options['routeParams']; $pagePropertyPath = new PropertyPath($options['pageParameter']); $routeGenerator = function ($page) use($router, $routeName, $routeParams, $pagePropertyPath, $options) { $propertyAccessor = PropertyAccess::getPropertyAccessor(); $propertyAccessor->setValue($routeParams, $pagePropertyPath, $page); $url = $router->generate($routeName, $routeParams); if ($options['queryString']) { $url .= '?' . $options['queryString']; } return $url; }; return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options); }
public function __construct(Container $container) { $this->basePath = app_upload(); $this->baseSource = app_upload() . '/uploads'; $this->propertyAccessor = PropertyAccess::getPropertyAccessor(); $this->basedirs = array(); }
/** * Renders a pagerfanta. * * @param PagerfantaInterface $pagerfanta The pagerfanta. * @param string $viewName The view name. * @param array $options An array of options (optional). * * @return string The pagerfanta rendered. */ public function renderPagerfanta(PagerfantaInterface $pagerfanta, $viewName = null, array $options = array()) { $options = array_replace($this->app['pagerfanta.view.options'], $options); if (null === $viewName) { $viewName = $options['default_view']; } $router = $this->app['url_generator']; //Custom router and router params if (isset($this->app['pagerfanta.view.router.name'])) { $options['routeName'] = $this->app['pagerfanta.view.router.name']; } if (isset($this->app['pagerfanta.view.router.params'])) { $options['routeParams'] = $this->app['pagerfanta.view.router.params']; } if (null === $options['routeName']) { $request = $this->app['request']; $options['routeName'] = $request->attributes->get('_route'); $options['routeParams'] = array_merge($request->query->all(), $request->attributes->get('_route_params')); } $routeName = $options['routeName']; $routeParams = $options['routeParams']; $pageParameter = $options['pageParameter']; $propertyAccessor = PropertyAccess::getPropertyAccessor(); $routeGenerator = function ($page) use($router, $routeName, $routeParams, $pageParameter, $propertyAccessor) { $propertyAccessor->setValue($routeParams, $pageParameter, $page); return $router->generate($routeName, $routeParams); }; return $this->app['pagerfanta.view_factory']->get($viewName)->render($pagerfanta, $routeGenerator, $options); }
protected function getText($object) { if (!$this->property || !class_exists('Symfony\\Component\\PropertyAccess\\PropertyAccess')) { return (string) $object; } $accessor = PropertyAccess::getPropertyAccessor(); return $accessor->getValue($object, $this->property); }
/** * Creates a new object choice list. * * @param array|\Traversable $choices The array of choices. Choices may also be given * as hierarchy of unlimited depth by creating nested * arrays. The title of the sub-hierarchy can be * stored in the array key pointing to the nested * array. The topmost level of the hierarchy may also * be a \Traversable. * @param string $labelPath A property path pointing to the property used * for the choice labels. The value is obtained * by calling the getter on the object. If the * path is NULL, the object's __toString() method * is used instead. * @param array $preferredChoices A flat array of choices that should be * presented to the user with priority. * @param string $groupPath A property path pointing to the property used * to group the choices. Only allowed if * the choices are given as flat array. * @param string $valuePath A property path pointing to the property used * for the choice values. If not given, integers * are generated instead. * @param PropertyAccessorInterface $propertyAccessor The reflection graph for reading property paths. */ public function __construct($choices, $labelPath = null, array $preferredChoices = array(), $groupPath = null, $valuePath = null, PropertyAccessorInterface $propertyAccessor = null) { $this->propertyAccessor = $propertyAccessor ?: PropertyAccess::getPropertyAccessor(); $this->labelPath = null !== $labelPath ? new PropertyPath($labelPath) : null; $this->groupPath = null !== $groupPath ? new PropertyPath($groupPath) : null; $this->valuePath = null !== $valuePath ? new PropertyPath($valuePath) : null; parent::__construct($choices, array(), $preferredChoices); }
/** * @param IItem $root * @param int $maxDepth * @param array $filter */ public function __construct(IItem $root, $maxDepth = -1, $filter = array()) { parent::__construct($root, parent::SELF_FIRST); parent::setMaxDepth($maxDepth); $this->filter = $filter; $this->accessor = PropertyAccess::getPropertyAccessor(); $this->iterated = new \SplObjectStorage(); }
/** * {@inheritDoc} */ public function getValue($source) { try { return PropertyAccess::getPropertyAccessor()->getValue($source, $this->sourcePropertyPath); } catch (NoSuchPropertyException $ex) { // ignore properties not found } }
/** @dataProvider provideResolveEntityUrlTestData */ public function testResolveEntityUrl($pattern, $context, $expectation) { $actions = Actions::enable(); $entity = new Entity($context, PropertyAccess::getPropertyAccessor()); $method = new \ReflectionMethod($actions, 'resolveEntityUrl'); $method->setAccessible(true); $url = $method->invoke($actions, $pattern, $entity); $this->assertEquals($expectation, $url); }
/** Re-map, initially the purpose was to have a dual of map($target, $data), it ends up being * more general : Maps $source stuff over $target based of an list of (from ; to) pairs defined * as $ppaths (key->value). * * Fixme: map() shall disappear in favor of this one func. * * @param mixed $source Anything that the PropertyPath component can work with, typically an object or array. * @param array $ppaths A mapping of property paths over $source, to property paths over $target. * @param array $target Likewise $source, defaults to array(). * @return mixed $target. * * Fixme: //public static function remap($source, \Traversable $ppaths, $target = array()) */ public static function remap($source, array $ppaths, $target = array()) { $accessor = PropertyAccess::getPropertyAccessor(); foreach ($ppaths as $from => $to) { $to = $to ?: "[{$from}]"; $value = $accessor->getValue($source, $from); $accessor->setValue($target, $to, $value); } return $target; }
public function transform($entity) { if (null === $entity) { return null; } //if (!$this->unitOfWork->isInIdentityMap($entity) and !$this->unitOfWork->isScheduledForInsert($entity)) { // throw new TransformationFailedException("Entities must be managed"); //} return !empty($this->property) ? PropertyAccess::getPropertyAccessor()->getValue($entity, $this->property) : current($this->unitOfWork->getEntityIdentifier($entity)); }
/** * Transforms an object into an sphinx object having the required keys * * @param object $object the object to convert * @param array $fields the keys we want to have in the returned array * * @return array **/ public function transform($object, array $fields) { $accessor = PropertyAccess::getPropertyAccessor(); $identifier = $accessor->getValue($object, $this->options['identifier']); $document = array('id' => $identifier); foreach ($fields as $key) { $property = $accessor->getValue($object, $key); $document[$key] = $this->normalizeValue($property); } return $document; }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $city = $accessor->getValue($data, $this->propertyPathToCity); $country = $city ? $city->getProvince()->getCountry() : null; $this->addCountryForm($form, $country); }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $centro = $accessor->getValue($data, $this->propertyPathToCentro); $tipo = $centro ? $centro->getTipo() : null; $this->addTipoCentroForm($form, $tipo); }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $model = $accessor->getValue($data, $this->propertyPathToModel); $country = $model ? $model->getBrand() : null; $this->addBrandForm($form, $country); }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $claseRecurso = $accessor->getValue($data, $this->propertyPathToResource); $clase = $claseRecurso ? $claseRecurso->getClase() : 'P'; $this->addTipoGrupoRecursoForm($form, $clase); }
/** * @test * @dataProvider statusProvider */ public function shouldHaveConvenienceMethods($status, $method) { $methods = array('stopped', 'checking', 'downloading', 'seeding'); $accessor = PropertyAccess::getPropertyAccessor(); $this->getTorrent()->setStatus($status); $methods = array_filter($methods, function ($value) use($method) { return $method !== $value; }); $this->assertTrue($accessor->getValue($this->getTorrent(), $method)); foreach ($methods as $m) { $this->assertFalse($accessor->getValue($this->getTorrent(), $m), $m); } }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { return; } $accessor = PropertyAccess::getPropertyAccessor(); $comune = $accessor->getValue($data, $this->propertyPathToComune); $provincia = $comune ? $comune->getProvince() : null; $id_regione = $provincia ? $provincia->getCountry()->getId() : null; $this->addProvinceForm($form, $id_regione, $provincia); }
public function transform($entity) { if (null === $entity) { return null; } if (!$this->unitOfWork->isInIdentityMap($entity)) { throw new FormException('Entities passed to the choice field must be managed'); } if ($this->property) { $propertyAccessor = PropertyAccess::getPropertyAccessor(); return $propertyAccessor->getValue($entity, $this->property); } return current($this->unitOfWork->getEntityIdentifier($entity)); }
/** * @param EntityManager $em * @param string $className * @param string|null $property * @param callable $queryBuilderCallback * @throws UnexpectedTypeException When $queryBuilderCallback is set and not callable */ public function __construct(EntityManager $em, $className, $property = null, $queryBuilderCallback = null) { $this->em = $em; $this->className = $className; if (!$property) { $property = $this->getIdPropertyPathFromEntityManager($em, $className); } $this->property = $property; $this->propertyAccessor = PropertyAccess::getPropertyAccessor(); $this->propertyPath = new PropertyPath($this->property); if (null !== $queryBuilderCallback && !is_callable($queryBuilderCallback)) { throw new UnexpectedTypeException($queryBuilderCallback, 'callable'); } $this->queryBuilderCallback = $queryBuilderCallback; }
/** * @param string $key key * @param Response $response response * @param string $format format * * @return mixed */ protected function extractKeyFromResponse($key, Response $response, $format = 'json') { if ($format != 'json') { throw new \LogicException(__METHOD__ . ' accepts only json format.'); } $body = json_decode($response->getBody(), true); if (null === $body) { throw new Exception\UnexpectedResponseException(sprintf('JSON body expected, "%s" returned', $response->getBody())); } $accessor = PropertyAccess::getPropertyAccessor(); $value = $accessor->getValue($body, $key); if (null === $value) { throw new Exception\UnexpectedResponseException(sprintf('Key "%s" not found in payload "%s"', $key, $response->getBody())); } return $value; }
public function preSetData(FormEvent $event) { $data = $event->getData(); $form = $event->getForm(); if (null === $data) { $this->addCityForm($form, null, null); return; } $accessor = PropertyAccess::getPropertyAccessor(); $city = $accessor->getValue($data, 'city'); //$province = ($city) ? $city->getProvince() : null ; //$this->addCityForm($form, $city, $province); //$country = ($data->getCity()) ? $data->getCity()->getCountry() : null ; $country = $city ? $city->getCountry() : null; $this->addCityForm($form, $city, $country); }
protected function buildValues($entity) { $result = []; $accessor = PropertyAccess::getPropertyAccessor(); foreach ($this->headers as $header) { try { $value = $accessor->getValue($entity, $header); if (is_scalar($value)) { $result[$header] = $value; } } catch (\Exception $ex) { unset($ex); } } return $result; }
/** * @param \PropelCollection $query The Doctrine Query * @param array $fields Fields to export * @param string $dateTimeFormat */ public function __construct(PropelCollection $collection, array $fields, $dateTimeFormat = 'r') { $this->collection = clone $collection; // Note : will be deprecated in Symfony 3.0, conserved for 2.2 compatibility // Use createPropertyAccessor() for 3.0 // @see Symfony\Component\PropertyAccess\PropertyAccess $this->propertyAccessor = PropertyAccess::getPropertyAccessor(); $this->propertyPaths = array(); foreach ($fields as $name => $field) { if (is_string($name) && is_string($field)) { $this->propertyPaths[$name] = new PropertyPath($field); } else { $this->propertyPaths[$field] = new PropertyPath($field); } } $this->dateTimeFormat = $dateTimeFormat; }
/** * {@inheritdoc} */ public function buildView(FormView $view, FormInterface $form, array $options) { $view->vars['image_url'] = null; $view->vars['aspect_ratio'] = $options['aspect_ratio']; if (array_key_exists('image_path', $options)) { $parentData = $form->getParent()->getData(); if (!$parentData) { return false; } $propertyAccessor = PropertyAccess::getPropertyAccessor(); $propertyPath = new PropertyPath($options['image_path']); $imageUrl = $propertyAccessor->getValue($parentData, $propertyPath); // définit une variable "image_url" qui sera disponible à l'affichage du champ // $view->set('image_url', $imageUrl); $view->vars['image_url'] = $imageUrl; } }
/** * {@inheritdoc} */ public function transform($data) { if (null === $data) { return null; } $propertyAccessor = PropertyAccess::getPropertyAccessor(); $propertyPath = new PropertyPath($this->property); $value = $propertyAccessor->getValue($data, $propertyPath); $return = array('id' => $value); if ($this->propertyDisplay == null) { $return['value'] = (string) $data; } else { $propertyDisplay = new PropertyPath($this->propertyDisplay); $value = $propertyAccessor->getValue($data, $propertyDisplay); $return['value'] = $value; } return $return; }
public function completeFields(ObjectManager $em, $entity) { $accessor = PropertyAccess::getPropertyAccessor(); $metadata = $this->resolver->getMetadataFromObject($em, $entity); foreach ($metadata->getColumnNames() as $columnName) { $property = $metadata->getFieldName($columnName); if (false === $metadata->isNullable($property)) { try { if (null === $accessor->getValue($entity, $property)) { $accessor->setValue($entity, $property, $this->complete($metadata->getFieldMapping($property), $metadata->getName())); } } catch (\Exception $ex) { unset($ex); } } } return $this; }
/** * {@inheritdoc} */ public function setDefaultOptions(OptionsResolverInterface $resolver) { $type = $this->type; $registry = $this->registry; $resolver->setDefaults(array('configs' => array(), 'suggestions' => array(), 'route_name' => null, 'class' => null, 'property' => null, 'em' => null, 'document_manager' => null)); $resolver->setNormalizers(array('em' => function (Options $options, $manager) use($registry, $type) { if (!in_array($type, array('entity', 'document'))) { return null; } if (null !== $options['document_manager'] && $manager) { throw new \InvalidArgumentException('You cannot set both an "em" and "document_manager" option.'); } $manager = $options['document_manager'] ?: $manager; if (null === $manager) { return $registry->getManagerForClass($options['class']); } return $registry->getManager($manager); }, 'suggestions' => function (Options $options, $suggestions) use($type, $registry) { if (null !== $options['route_name']) { return array(); } if (empty($suggestions)) { switch ($type) { case 'entity': case 'document': $propertyPath = $options['property'] ? new PropertyPath($options['property']) : null; $suggestions = array(); $objects = $options['em']->getRepository($options['class'])->findAll(); foreach ($objects as $object) { if ($propertyPath) { $suggestions[] = PropertyAccess::getPropertyAccessor()->getValue($object, $propertyPath); } elseif (method_exists($object, '__toString')) { $suggestions[] = (string) $object; } else { throw new \RuntimeException('Cannot cast object of type "' . get_class($object) . '" to string, please implement a __toString method or set the "property" option to the desired value.'); } } break; } } return $suggestions; })); }
public function buildView(FormView $view, FormInterface $form, array $options) { parent::buildView($view, $form, $options); $view->vars = array_replace($view->vars, array('type' => 'file', 'value' => '')); if (array_key_exists('file_path', $options)) { $parentData = $form->getParent()->getData(); if (null !== $parentData) { $accessor = PropertyAccess::getPropertyAccessor(); $imageUrl = $accessor->getValue($parentData, $options['file_path']); $value = $accessor->getValue($parentData, 'filePath'); } else { $imageUrl = null; $value = null; } // set an "image_url" variable that will be available when rendering this field $view->vars['file_url'] = $imageUrl; $view->vars['value'] = $value; } }
/** * Transforms an array of sphinx objects into an array of * model objects fetched from the doctrine repository * * @param array of sphinx objects * @return array **/ public function transform(array $sphinxObjects) { $ids = array(); foreach ($sphinxObjects as $sphinxObject) { $ids[] = $sphinxObject['id']; } $objects = $this->findByIdentifiers($ids, $this->options['hydrate']); if (count($objects) < count($sphinxObjects)) { throw new \RuntimeException('Cannot find corresponding Doctrine objects for all Sphinx results.'); } $accessor = PropertyAccess::getPropertyAccessor(); $identifier_key = $this->options['identifier']; // sort objects in the order of ids $idPos = array_flip($ids); usort($objects, function ($a, $b) use($idPos, $accessor, $identifier_key) { return $idPos[$accessor->getValue($a, $identifier_key)] > $idPos[$accessor->getValue($b, $identifier_key)]; }); return $objects; }