|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\Form\ChoiceList; |
| 13 | + |
| 14 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceAttr; |
| 15 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceFieldName; |
| 16 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLabel; |
| 17 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceLoader; |
| 18 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\ChoiceValue; |
| 19 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\GroupBy; |
| 20 | +use Symfony\Component\Form\ChoiceList\Factory\Cache\PreferredChoice; |
| 21 | +use Symfony\Component\Form\ChoiceList\Loader\CallbackChoiceLoader; |
| 22 | +use Symfony\Component\Form\ChoiceList\Loader\ChoiceLoaderInterface; |
| 23 | +use Symfony\Component\Form\FormTypeExtensionInterface; |
| 24 | +use Symfony\Component\Form\FormTypeInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * A set of convenient static methods to create cacheable choice list options. |
| 28 | + * |
| 29 | + * @author Jules Pietri <jules@heahprod.com> |
| 30 | + */ |
| 31 | +final class ChoiceList |
| 32 | +{ |
| 33 | + /** |
| 34 | + * Creates a cacheable loader from any callable providing iterable choices. |
| 35 | + * |
| 36 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 37 | + * @param callable $choices A callable that must return iterable choices or grouped choices |
| 38 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader |
| 39 | + */ |
| 40 | + public static function lazy($formType, callable $choices, $vary = null): ChoiceLoader |
| 41 | + { |
| 42 | + return self::loader($formType, new CallbackChoiceLoader($choices), $vary); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Decorates a loader to make it cacheable. |
| 47 | + * |
| 48 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 49 | + * @param ChoiceLoaderInterface $loader A loader responsible for creating loading choices or grouped choices |
| 50 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the loader |
| 51 | + */ |
| 52 | + public static function loader($formType, ChoiceLoaderInterface $loader, $vary = null): ChoiceLoader |
| 53 | + { |
| 54 | + return new ChoiceLoader($formType, $loader, $vary); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Decorates a "choice_value" callback to make it cacheable. |
| 59 | + * |
| 60 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 61 | + * @param callable $value Any pseudo callable to create a unique string value from a choice |
| 62 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback |
| 63 | + */ |
| 64 | + public static function value($formType, $value, $vary = null): ChoiceValue |
| 65 | + { |
| 66 | + return new ChoiceValue($formType, $value, $vary); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Decorates a "choice_label" option to make it cacheable. |
| 71 | + * |
| 72 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 73 | + * @param callable|false $label Any pseudo callable to create a label from a choice or false to discard it |
| 74 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option |
| 75 | + */ |
| 76 | + public static function label($formType, $label, $vary = null): ChoiceLabel |
| 77 | + { |
| 78 | + return new ChoiceLabel($formType, $label, $vary); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Decorates a "choice_name" callback to make it cacheable. |
| 83 | + * |
| 84 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 85 | + * @param callable $fieldName Any pseudo callable to create a field name from a choice |
| 86 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the callback |
| 87 | + */ |
| 88 | + public static function fieldName($formType, $fieldName, $vary = null): ChoiceFieldName |
| 89 | + { |
| 90 | + return new ChoiceFieldName($formType, $fieldName, $vary); |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Decorates a "choice_attr" option to make it cacheable. |
| 95 | + * |
| 96 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 97 | + * @param callable|array $attr Any pseudo callable or array to create html attributes from a choice |
| 98 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option |
| 99 | + */ |
| 100 | + public static function attr($formType, $attr, $vary = null): ChoiceAttr |
| 101 | + { |
| 102 | + return new ChoiceAttr($formType, $attr, $vary); |
| 103 | + } |
| 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 | + /** |
| 118 | + * Decorates a "preferred_choices" option to make it cacheable. |
| 119 | + * |
| 120 | + * @param FormTypeInterface|FormTypeExtensionInterface $formType A form type or type extension configuring a cacheable choice list |
| 121 | + * @param callable|array $preferred Any pseudo callable or array to return a group name from a choice |
| 122 | + * @param mixed|null $vary Dynamic data used to compute a unique hash when caching the option |
| 123 | + */ |
| 124 | + public static function preferred($formType, $preferred, $vary = null): PreferredChoice |
| 125 | + { |
| 126 | + return new PreferredChoice($formType, $preferred, $vary); |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * Should not be instantiated. |
| 131 | + */ |
| 132 | + private function __construct() |
| 133 | + { |
| 134 | + } |
| 135 | +} |
0 commit comments