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

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  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 <bschussek@gmail.com>
  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.     private ?\Closure $value;
  33.     /**
  34.      * Creates a lazily-loaded list using the given loader.
  35.      *
  36.      * Optionally, a callable can be passed for generating the choice values.
  37.      * The callable receives the choice as first and the array key as the second
  38.      * argument.
  39.      *
  40.      * @param callable|null $value The callable generating the choice values
  41.      */
  42.     public function __construct(ChoiceLoaderInterface $loader, callable $value null)
  43.     {
  44.         $this->loader $loader;
  45.         $this->value null === $value || $value instanceof \Closure $value \Closure::fromCallable($value);
  46.     }
  47.     /**
  48.      * {@inheritdoc}
  49.      */
  50.     public function getChoices(): array
  51.     {
  52.         return $this->loader->loadChoiceList($this->value)->getChoices();
  53.     }
  54.     /**
  55.      * {@inheritdoc}
  56.      */
  57.     public function getValues(): array
  58.     {
  59.         return $this->loader->loadChoiceList($this->value)->getValues();
  60.     }
  61.     /**
  62.      * {@inheritdoc}
  63.      */
  64.     public function getStructuredValues(): array
  65.     {
  66.         return $this->loader->loadChoiceList($this->value)->getStructuredValues();
  67.     }
  68.     /**
  69.      * {@inheritdoc}
  70.      */
  71.     public function getOriginalKeys(): array
  72.     {
  73.         return $this->loader->loadChoiceList($this->value)->getOriginalKeys();
  74.     }
  75.     /**
  76.      * {@inheritdoc}
  77.      */
  78.     public function getChoicesForValues(array $values): array
  79.     {
  80.         return $this->loader->loadChoicesForValues($values$this->value);
  81.     }
  82.     /**
  83.      * {@inheritdoc}
  84.      */
  85.     public function getValuesForChoices(array $choices): array
  86.     {
  87.         return $this->loader->loadValuesForChoices($choices$this->value);
  88.     }
  89. }