Symfony\Component\Form\Util\PropertyPath::setValue PHP Method

setValue() public method

Example: $path = new PropertyPath('child.name'); echo $path->setValue($object, 'Fabien'); equals echo $object->getChild()->setName('Fabien'); This method first tries to find a public setter for each property in the path. The name of the setter must be the camel-cased property name prefixed with "set". If the setter does not exist, this method tries to find a public property. The value of the property is then changed. If neither is found, an exception is thrown.
public setValue ( object | array &$objectOrArray, mixed $value )
$objectOrArray object | array The object or array to traverse
$value mixed The value at the end of the property path
    public function setValue(&$objectOrArray, $value)
    {
        for ($i = 0, $l = $this->length - 1; $i < $l; ++$i) {

            if (is_object($objectOrArray)) {
                $nestedObject = $this->readProperty($objectOrArray, $i);
            // arrays need to be treated separately (due to PHP bug?)
            // http://bugs.php.net/bug.php?id=52133
            } else if (is_array($objectOrArray)) {
                $property = $this->elements[$i];
                if (!array_key_exists($property, $objectOrArray)) {
                    $objectOrArray[$property] = array();
                }
                $nestedObject =& $objectOrArray[$property];
            } else {
                throw new UnexpectedTypeException($objectOrArray, 'object or array');
            }

            $objectOrArray =& $nestedObject;
        }

        if (!is_object($objectOrArray) && !is_array($objectOrArray)) {
            throw new UnexpectedTypeException($objectOrArray, 'object or array');
        }

        $this->writeProperty($objectOrArray, $i, $value);
    }

Usage Example

 /**
  * 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 = 'default', array $options = array())
 {
     $options = array_replace(array('routeName' => null, 'routeParams' => array(), 'pageParameter' => 'page'), $options);
     $router = $this->container->get('router');
     $request = $this->container->get('request');
     if (null === $options['routeName']) {
         $options['routeName'] = $request->attributes->get('_route');
         $options['routeParams'] = $request->query->all();
         if ($options['routeName'] === '_internal') {
             throw new \Exception('PagerfantaBundle can not guess the route when used in a subrequest');
         }
         foreach ($router->getRouteCollection()->get($options['routeName'])->compile()->getVariables() as $variable) {
             $options['routeParams'][$variable] = $request->attributes->get($variable);
         }
     }
     $routeName = $options['routeName'];
     $routeParams = $options['routeParams'];
     $pagePropertyPath = new PropertyPath($options['pageParameter']);
     $routeGenerator = function ($page) use($router, $routeName, $routeParams, $pagePropertyPath) {
         $pagePropertyPath->setValue($routeParams, $page);
         return $router->generate($routeName, $routeParams);
     };
     return $this->container->get('white_october_pagerfanta.view_factory')->get($viewName)->render($pagerfanta, $routeGenerator, $options);
 }
All Usage Examples Of Symfony\Component\Form\Util\PropertyPath::setValue