ComponentFactory
3 days ago
Control
3 days ago
Type
3 days ago
AttributeCollection.php
3 days ago
AttributeFactory.php
3 days ago
Children.php
3 days ago
Component.php
3 days ago
ComponentBuilder.php
3 days ago
ComponentCollection.php
3 days ago
ComponentFactory.php
3 days ago
ConditionalComponentFactory.php
3 days ago
ConditionalComponentFactoryCollection.php
3 days ago
Config.php
3 days ago
ConfigCollection.php
3 days ago
ConfigFactory.php
3 days ago
Context.php
3 days ago
Control.php
3 days ago
DefaultSettingsBuilder.php
3 days ago
Encoder.php
3 days ago
Formatter.php
3 days ago
FormatterCollection.php
3 days ago
Encoder.php
131 lines
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace AC\Setting; |
| 6 | |
| 7 | use AC\Setting\Control\Input\Custom; |
| 8 | use AC\Setting\Control\Input\Number; |
| 9 | use AC\Setting\Control\Input\Open; |
| 10 | use AC\Setting\Control\Input\Option; |
| 11 | use AC\Setting\Control\OptionCollection; |
| 12 | |
| 13 | final class Encoder |
| 14 | { |
| 15 | private ComponentCollection $settings; |
| 16 | |
| 17 | public function __construct(ComponentCollection $settings) |
| 18 | { |
| 19 | $this->settings = $settings; |
| 20 | } |
| 21 | |
| 22 | public function encode(): array |
| 23 | { |
| 24 | $encoded = []; |
| 25 | |
| 26 | foreach ($this->settings as $setting) { |
| 27 | $encoded[] = $this->encode_setting($setting); |
| 28 | } |
| 29 | |
| 30 | return $encoded; |
| 31 | } |
| 32 | |
| 33 | private function encode_setting(Component $component): array |
| 34 | { |
| 35 | $encoded = [ |
| 36 | 'type' => $component->get_type(), |
| 37 | 'label' => $component->has_label() ? $component->get_label() : '', |
| 38 | ]; |
| 39 | |
| 40 | if ($component->has_description()) { |
| 41 | $encoded['description'] = $component->get_description(); |
| 42 | } |
| 43 | |
| 44 | foreach ($component->get_attributes() as $attribute) { |
| 45 | $encoded['attributes'][$attribute->get_name()] = $attribute->get_value(); |
| 46 | } |
| 47 | |
| 48 | if ($component->has_input()) { |
| 49 | $input = $component->get_input(); |
| 50 | |
| 51 | $encoded['conditions'] = $component->get_conditions()->export(); |
| 52 | |
| 53 | $encoded['input'] = [ |
| 54 | 'type' => $input->get_type(), |
| 55 | 'name' => $input->get_name(), |
| 56 | ]; |
| 57 | |
| 58 | if ($input->has_placeholder()) { |
| 59 | $encoded['input']['placeholder'] = $input->get_placeholder(); |
| 60 | } |
| 61 | |
| 62 | foreach ($input->get_attributes() as $attribute) { |
| 63 | $encoded['input']['attributes'][$attribute->get_name()] = $attribute->get_value(); |
| 64 | } |
| 65 | |
| 66 | if ($input->has_value()) { |
| 67 | $encoded['input']['default'] = $input->get_value(); |
| 68 | } |
| 69 | |
| 70 | if ($input instanceof Open && $input->has_append()) { |
| 71 | $encoded['input']['append'] = $input->get_append(); |
| 72 | } |
| 73 | |
| 74 | if ($input instanceof Option) { |
| 75 | $encoded['input'] += [ |
| 76 | 'options' => $this->encode_options($input->get_options()), |
| 77 | ]; |
| 78 | |
| 79 | if ($input->is_multiple()) { |
| 80 | $encoded['input']['type'] = 'select_multiple'; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | if ($input instanceof Number) { |
| 85 | if ($input->has_min()) { |
| 86 | $encoded['input']['min'] = $input->get_min(); |
| 87 | } |
| 88 | |
| 89 | if ($input->has_max()) { |
| 90 | $encoded['input']['max'] = $input->get_max(); |
| 91 | } |
| 92 | |
| 93 | if ($input->has_step()) { |
| 94 | $encoded['input']['step'] = $input->get_step(); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | if ($input instanceof Custom) { |
| 99 | $encoded['input']['data'] = $input->get_data(); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | if ($component->has_children()) { |
| 104 | $encoded['is_parent'] = $component->get_children()->is_parent(); |
| 105 | $encoded['children'] = []; |
| 106 | |
| 107 | foreach ($component->get_children()->get_iterator() as $item) { |
| 108 | $encoded['children'][] = $this->encode_setting($item); |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | return $encoded; |
| 113 | } |
| 114 | |
| 115 | private function encode_options(OptionCollection $options): array |
| 116 | { |
| 117 | $encoded = []; |
| 118 | |
| 119 | foreach ($options as $option) { |
| 120 | $encoded[] = [ |
| 121 | 'value' => $option->get_value(), |
| 122 | 'label' => $option->get_label(), |
| 123 | 'group' => $option->get_group(), |
| 124 | ]; |
| 125 | } |
| 126 | |
| 127 | return $encoded; |
| 128 | } |
| 129 | |
| 130 | } |
| 131 |