Custom.php
4 days ago
Number.php
4 days ago
Open.php
4 days ago
OpenFactory.php
4 days ago
Option.php
4 days ago
OptionFactory.php
4 days ago
OptionFactory.php
117 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting\Control\Input; |
| 6 | |
| 7 | use AC\Setting\AttributeCollection; |
| 8 | use AC\Setting\Control\OptionCollection; |
| 9 | use AC\Setting\Control\OptionCollectionFactory\ToggleOptionCollection; |
| 10 | use AC\Setting\Type\Attribute; |
| 11 | |
| 12 | final class OptionFactory |
| 13 | { |
| 14 | public static function create_select( |
| 15 | string $name, |
| 16 | OptionCollection $options, |
| 17 | $default = null, |
| 18 | ?string $placeholder = null, |
| 19 | ?bool $multiple = null, |
| 20 | ?AttributeCollection $attributes = null |
| 21 | ): Option { |
| 22 | return new Option( |
| 23 | $name, |
| 24 | 'select', |
| 25 | $options, |
| 26 | $default, |
| 27 | $placeholder, |
| 28 | $multiple, |
| 29 | $attributes |
| 30 | ); |
| 31 | } |
| 32 | |
| 33 | public static function create_select_remote( |
| 34 | string $name, |
| 35 | string $handler, |
| 36 | $default = null, |
| 37 | array $data = [], |
| 38 | ?string $placeholder = null, |
| 39 | ?bool $multiple = null, |
| 40 | ?AttributeCollection $attributes = null |
| 41 | ): Option { |
| 42 | if (null === $attributes) { |
| 43 | $attributes = new AttributeCollection(); |
| 44 | } |
| 45 | |
| 46 | $attributes->add( |
| 47 | new Attribute( |
| 48 | 'data-handler', |
| 49 | $handler |
| 50 | ) |
| 51 | ); |
| 52 | |
| 53 | $default_params = '{}'; |
| 54 | $params = $default_params; |
| 55 | |
| 56 | if ($data) { |
| 57 | $params = json_encode($data) ?: $default_params; |
| 58 | } |
| 59 | |
| 60 | $attributes->add( |
| 61 | new Attribute( |
| 62 | 'data-params', |
| 63 | $params |
| 64 | ) |
| 65 | ); |
| 66 | |
| 67 | return new Option( |
| 68 | $name, |
| 69 | 'select_remote', |
| 70 | new OptionCollection(), |
| 71 | $default, |
| 72 | $placeholder, |
| 73 | $multiple, |
| 74 | $attributes |
| 75 | ); |
| 76 | } |
| 77 | |
| 78 | public static function create_radio( |
| 79 | string $name, |
| 80 | OptionCollection $options, |
| 81 | ?string $default = null, |
| 82 | ?AttributeCollection $attributes = null |
| 83 | ): Option { |
| 84 | return new Option( |
| 85 | $name, |
| 86 | 'radio', |
| 87 | $options, |
| 88 | $default, |
| 89 | null, |
| 90 | null, |
| 91 | $attributes |
| 92 | ); |
| 93 | } |
| 94 | |
| 95 | public static function create_toggle( |
| 96 | string $name, |
| 97 | ?OptionCollection $options = null, |
| 98 | ?string $default = null, |
| 99 | ?AttributeCollection $attributes = null |
| 100 | ): Option { |
| 101 | if (null === $options) { |
| 102 | $options = (new ToggleOptionCollection())->create(); |
| 103 | } |
| 104 | |
| 105 | return new Option( |
| 106 | $name, |
| 107 | 'toggle', |
| 108 | $options, |
| 109 | $default, |
| 110 | null, |
| 111 | null, |
| 112 | $attributes |
| 113 | ); |
| 114 | } |
| 115 | |
| 116 | } |
| 117 |