ChoiceList.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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\Factory\Cache\ChoiceAttr;
  12. use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFieldName;
  13. use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFilter;
  14. use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel;
  15. use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader;
  16. use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue;
  17. use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy;
  18. use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice;
  19. use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader;
  20. use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface;
  21. use Symfony\Component\Form\FormTypeExtensionInterface;
  22. use Symfony\Component\Form\FormTypeInterface;
  23. /**
  24. * A set of convenient static methods to create cacheable choice list options.
  25. *
  26. * @author Jules Pietri <jules@heahprod.com>
  27. */
  28. final class ChoiceList
  29. {
  30. /**
  31. * Creates a cacheable loader from any callable providing iterable choices.
  32. *
  33. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  34. * @param callable $choices A callable that must return iterable choices or grouped choices
  35. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
  36. */
  37. public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader
  38. {
  39. return self::loader($formType, new CallbackChoiceLoader($choices), $vary);
  40. }
  41. /**
  42. * Decorates a loader to make it cacheable.
  43. *
  44. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  45. * @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices
  46. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader
  47. */
  48. public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader
  49. {
  50. return new ChoiceLoader($formType, $loader, $vary);
  51. }
  52. /**
  53. * Decorates a "choice_value" callback to make it cacheable.
  54. *
  55. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  56. * @param callable $value Any pseudo callable to create a unique string value from a choice
  57. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
  58. */
  59. public static function value($formType, $value, $vary = null): ChoiceValue
  60. {
  61. return new ChoiceValue($formType, $value, $vary);
  62. }
  63. /**
  64. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  65. * @param callable $filter Any pseudo callable to filter a choice list
  66. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
  67. */
  68. public static function filter($formType, $filter, $vary = null): ChoiceFilter
  69. {
  70. return new ChoiceFilter($formType, $filter, $vary);
  71. }
  72. /**
  73. * Decorates a "choice_label" option to make it cacheable.
  74. *
  75. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  76. * @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it
  77. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
  78. */
  79. public static function label($formType, $label, $vary = null): ChoiceLabel
  80. {
  81. return new ChoiceLabel($formType, $label, $vary);
  82. }
  83. /**
  84. * Decorates a "choice_name" callback to make it cacheable.
  85. *
  86. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  87. * @param callable $fieldName Any pseudo callable to create a field name from a choice
  88. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
  89. */
  90. public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName
  91. {
  92. return new ChoiceFieldName($formType, $fieldName, $vary);
  93. }
  94. /**
  95. * Decorates a "choice_attr" option to make it cacheable.
  96. *
  97. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  98. * @param callable|array $attr Any pseudo callable or array to create html attributes from a choice
  99. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
  100. */
  101. public static function attr($formType, $attr, $vary = null): ChoiceAttr
  102. {
  103. return new ChoiceAttr($formType, $attr, $vary);
  104. }
  105. /**
  106. * Decorates a "group_by" callback to make it cacheable.
  107. *
  108. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  109. * @param callable $groupBy Any pseudo callable to return a group name from a choice
  110. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback
  111. */
  112. public static function groupBy($formType, $groupBy, $vary = null): GroupBy
  113. {
  114. return new GroupBy($formType, $groupBy, $vary);
  115. }
  116. /**
  117. * Decorates a "preferred_choices" option to make it cacheable.
  118. *
  119. * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list
  120. * @param callable|array $preferred Any pseudo callable or array to return a group name from a choice
  121. * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option
  122. */
  123. public static function preferred($formType, $preferred, $vary = null): PreferredChoice
  124. {
  125. return new PreferredChoice($formType, $preferred, $vary);
  126. }
  127. /**
  128. * Should not be instantiated.
  129. */
  130. private function __construct()
  131. {
  132. }
  133. }