vendor/skyfox/propel-bundle/Form/ChoiceList/PropelChoiceLoader.php line 26

Open in your IDE?
  1. <?php
  2. /**
  3.  * This file is part of the PropelBundle package.
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  *
  7.  * @license    MIT License
  8.  */
  9. namespace Propel\Bundle\PropelBundle\Form\ChoiceList;
  10. use Propel\Runtime\ActiveQuery\Criteria;
  11. use Propel\Runtime\ActiveQuery\ModelCriteria;
  12. use Propel\Runtime\ActiveRecord\ActiveRecordInterface;
  13. use Propel\Runtime\Map\ColumnMap;
  14. use Symfony\Component\Form\ChoiceList\ChoiceListInterface;
  15. use Symfony\Component\Form\ChoiceList\Factory\ChoiceListFactoryInterface;
  16. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  17. /**
  18.  * @author William Durand <william.durand1@gmail.com>
  19.  * @author Toni Uebernickel <tuebernickel@gmail.com>
  20.  * @author Moritz Schroeder <moritz.schroeder@molabs.de>
  21.  */
  22. class PropelChoiceLoader implements ChoiceLoaderInterface
  23. {
  24.     /**
  25.      * @var ChoiceListFactoryInterface
  26.      */
  27.     protected $factory;
  28.     
  29.     /**
  30.      * @var string
  31.      */
  32.     protected $class;
  33.     /**
  34.      * @var ModelCriteria
  35.      */
  36.     protected $query;
  37.     /**
  38.      * The fields of which the identifier of the underlying class consists
  39.      *
  40.      * This property should only be accessed through identifier.
  41.      *
  42.      * @var array
  43.      */
  44.     protected $identifier = array();
  45.     /**
  46.      * Whether to use the identifier for index generation.
  47.      *
  48.      * @var bool
  49.      */
  50.     protected $identifierAsIndex false;
  51.     /**
  52.      * @var ChoiceListInterface
  53.      */
  54.     protected $choiceList;
  55.     /**
  56.      * PropelChoiceListLoader constructor.
  57.      *
  58.      * @param ChoiceListFactoryInterface $factory
  59.      * @param string                     $class
  60.      */
  61.     public function __construct(ChoiceListFactoryInterface $factory$classModelCriteria $queryObject$useAsIdentifier null)
  62.     {
  63.         $this->factory $factory;
  64.         $this->class $class;
  65.         $this->query $queryObject;
  66.         if ($useAsIdentifier) {
  67.             $this->identifier = array($this->query->getTableMap()->getColumn($useAsIdentifier));
  68.         } else {
  69.             $this->identifier $this->query->getTableMap()->getPrimaryKeys();
  70.         }
  71.         if (=== count($this->identifier) && $this->isScalar(current($this->identifier))) {
  72.             $this->identifierAsIndex true;
  73.         }
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function loadChoiceList($value null)
  79.     {
  80.         if ($this->choiceList) {
  81.             return $this->choiceList;
  82.         }
  83.         $models iterator_to_array($this->query->find());
  84.         
  85.         $this->choiceList $this->factory->createListFromChoices($models$value);
  86.         return $this->choiceList;
  87.     }
  88.     /**
  89.      * {@inheritdoc}
  90.      */
  91.     public function loadChoicesForValues(array $values$value null)
  92.     {
  93.         // Performance optimization
  94.         if (empty($values)) {
  95.             return array();
  96.         }
  97.         
  98.         // Optimize performance in case we have a single-field identifier
  99.         if (!$this->choiceList && $this->identifierAsIndex && current($this->identifier) instanceof ColumnMap) {
  100.             $phpName current($this->identifier)->getPhpName();
  101.             $query = clone $this->query;
  102.             $unorderedObjects $query->filterBy($phpName$valuesCriteria::IN);
  103.             $objectsById = array();
  104.             $objects = array();
  105.             // Maintain order and indices from the given $values
  106.             foreach ($unorderedObjects as $object) {
  107.                 $objectsById[(string) current($this->getIdentifierValues($object))] = $object;
  108.             }
  109.             foreach ($values as $i => $id) {
  110.                 if (isset($objectsById[$id])) {
  111.                     $objects[$i] = $objectsById[$id];
  112.                 }
  113.             }
  114.             return $objects;
  115.         }
  116.         return $this->loadChoiceList($value)->getChoicesForValues($values);
  117.     }
  118.     /**
  119.      * {@inheritdoc}
  120.      */
  121.     public function loadValuesForChoices(array $choices$value null)
  122.     {
  123.         // Performance optimization
  124.         if (empty($choices)) {
  125.             return array();
  126.         }
  127.         if (!$this->choiceList && $this->identifierAsIndex) {
  128.             $values = array();
  129.             // Maintain order and indices of the given objects
  130.             foreach ($choices as $i => $object) {
  131.                 if ($object instanceof $this->class) {
  132.                     // Make sure to convert to the right format
  133.                     $values[$i] = (string) current($this->getIdentifierValues($object));
  134.                 }
  135.             }
  136.             return $values;
  137.         }
  138.         return $this->loadChoiceList($value)->getValuesForChoices($choices);
  139.     }
  140.     /**
  141.      * Whether this column contains scalar values (to be used as indices).
  142.      *
  143.      * @param ColumnMap $column
  144.      *
  145.      * @return bool
  146.      */
  147.     private function isScalar(ColumnMap $column)
  148.     {
  149.         return in_array(
  150.             $column->getPdoType(),
  151.             array(
  152.                 \PDO::PARAM_BOOL,
  153.                 \PDO::PARAM_INT,
  154.                 \PDO::PARAM_STR,
  155.             )
  156.         );
  157.     }
  158.     /**
  159.      * Returns the values of the identifier fields of a model.
  160.      *
  161.      * Propel must know about this model, that is, the model must already
  162.      * be persisted or added to the idmodel map before. Otherwise an
  163.      * exception is thrown.
  164.      *
  165.      * @param object $model The model for which to get the identifier
  166.      *
  167.      * @return array
  168.      */
  169.     private function getIdentifierValues($model)
  170.     {
  171.         if (!$model instanceof $this->class) {
  172.             return array();
  173.         }
  174.         if (=== count($this->identifier) && current($this->identifier) instanceof ColumnMap) {
  175.             $phpName current($this->identifier)->getPhpName();
  176.             if (method_exists($model'get' $phpName)) {
  177.                 return array($model->{'get' $phpName}());
  178.             }
  179.         }
  180.         if ($model instanceof ActiveRecordInterface) {
  181.             return array($model->getPrimaryKey());
  182.         }
  183.         return $model->getPrimaryKeys();
  184.     }
  185. }