vendor/symfony/form/ChoiceList/LazyChoiceList.php line 91

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <[email protected]>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Form\ChoiceList;
  11. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  12. /**
  13.  * A choice list that loads its choices lazily.
  14.  *
  15.  * The choices are fetched using a {@link ChoiceLoaderInterface} instance.
  16.  * If only {@link getChoicesForValues()} or {@link getValuesForChoices()} is
  17.  * called, the choice list is only loaded partially for improved performance.
  18.  *
  19.  * Once {@link getChoices()} or {@link getValues()} is called, the list is
  20.  * loaded fully.
  21.  *
  22.  * @author Bernhard Schussek <[email protected]>
  23.  */
  24. class LazyChoiceList implements ChoiceListInterface
  25. {
  26.     private $loader;
  27.     /**
  28.      * The callable creating string values for each choice.
  29.      *
  30.      * If null, choices are cast to strings.
  31.      *
  32.      * @var callable|null
  33.      */
  34.     private $value;
  35.     /**
  36.      * Creates a lazily-loaded list using the given loader.
  37.      *
  38.      * Optionally, a callable can be passed for generating the choice values.
  39.      * The callable receives the choice as first and the array key as the second
  40.      * argument.
  41.      *
  42.      * @param callable|null $value The callable generating the choice values
  43.      */
  44.     public function __construct(ChoiceLoaderInterface $loader, ?callable $value null)
  45.     {
  46.         $this->loader $loader;
  47.         $this->value $value;
  48.     }
  49.     /**
  50.      * {@inheritdoc}
  51.      */
  52.     public function getChoices()
  53.     {
  54.         return $this->loader->loadChoiceList($this->value)->getChoices();
  55.     }
  56.     /**
  57.      * {@inheritdoc}
  58.      */
  59.     public function getValues()
  60.     {
  61.         return $this->loader->loadChoiceList($this->value)->getValues();
  62.     }
  63.     /**
  64.      * {@inheritdoc}
  65.      */
  66.     public function getStructuredValues()
  67.     {
  68.         return $this->loader->loadChoiceList($this->value)->getStructuredValues();
  69.     }
  70.     /**
  71.      * {@inheritdoc}
  72.      */
  73.     public function getOriginalKeys()
  74.     {
  75.         return $this->loader->loadChoiceList($this->value)->getOriginalKeys();
  76.     }
  77.     /**
  78.      * {@inheritdoc}
  79.      */
  80.     public function getChoicesForValues(array $values)
  81.     {
  82.         return $this->loader->loadChoicesForValues($values$this->value);
  83.     }
  84.     /**
  85.      * {@inheritdoc}
  86.      */
  87.     public function getValuesForChoices(array $choices)
  88.     {
  89.         return $this->loader->loadValuesForChoices($choices$this->value);
  90.     }
  91. }