LazyChoiceList.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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 simply cast to strings.
  31. *
  32. * @var callable|null
  33. */
  34. private $value;
  35. /**
  36. * @var ChoiceListInterface|null
  37. */
  38. private $loadedList;
  39. /**
  40. * Creates a lazily-loaded list using the given loader.
  41. *
  42. * Optionally, a callable can be passed for generating the choice values.
  43. * The callable receives the choice as first and the array key as the second
  44. * argument.
  45. *
  46. * @param ChoiceLoaderInterface $loader The choice loader
  47. * @param callable|null $value The callable generating the choice values
  48. */
  49. public function __construct(ChoiceLoaderInterface $loader, $value = null)
  50. {
  51. $this->loader = $loader;
  52. $this->value = $value;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getChoices()
  58. {
  59. if (!$this->loadedList) {
  60. $this->loadedList = $this->loader->loadChoiceList($this->value);
  61. }
  62. return $this->loadedList->getChoices();
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function getValues()
  68. {
  69. if (!$this->loadedList) {
  70. $this->loadedList = $this->loader->loadChoiceList($this->value);
  71. }
  72. return $this->loadedList->getValues();
  73. }
  74. /**
  75. * {@inheritdoc}
  76. */
  77. public function getStructuredValues()
  78. {
  79. if (!$this->loadedList) {
  80. $this->loadedList = $this->loader->loadChoiceList($this->value);
  81. }
  82. return $this->loadedList->getStructuredValues();
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getOriginalKeys()
  88. {
  89. if (!$this->loadedList) {
  90. $this->loadedList = $this->loader->loadChoiceList($this->value);
  91. }
  92. return $this->loadedList->getOriginalKeys();
  93. }
  94. /**
  95. * {@inheritdoc}
  96. */
  97. public function getChoicesForValues(array $values)
  98. {
  99. if (!$this->loadedList) {
  100. return $this->loader->loadChoicesForValues($values, $this->value);
  101. }
  102. return $this->loadedList->getChoicesForValues($values);
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function getValuesForChoices(array $choices)
  108. {
  109. if (!$this->loadedList) {
  110. return $this->loader->loadValuesForChoices($choices, $this->value);
  111. }
  112. return $this->loadedList->getValuesForChoices($choices);
  113. }
  114. }