| @@ -5,112 +5,132 @@ | ||
| 5 | 5 | use Closure; |
| 6 | 6 | |
| 7 | 7 | class Components implements \JsonSerializable |
| 8 | 8 | { |
| 9 | - /** | |
| 10 | - * $items [Components list] | |
| 11 | - * @var array | |
| 12 | - */ | |
| 13 | - protected $items = array(); | |
| 9 | + /** | |
| 10 | + * $items [Components list] | |
| 11 | + * | |
| 12 | + * @var array | |
| 13 | + */ | |
| 14 | + protected $items = []; | |
| 14 | 15 | |
| 15 | - /** | |
| 16 | - * Build the object instance | |
| 17 | - * @param array $items | |
| 18 | - */ | |
| 19 | - public function __construct(array $items) | |
| 20 | - { | |
| 21 | - $this->items = $items; | |
| 22 | - } | |
| 16 | + /** | |
| 17 | + * Build the object instance | |
| 18 | + * | |
| 19 | + * @param array $items | |
| 20 | + */ | |
| 21 | + public function __construct(array $items) | |
| 22 | + { | |
| 23 | + $this->items = $items; | |
| 24 | + } | |
| 23 | 25 | |
| 24 | - /** | |
| 25 | - * Add a component into list [$items] | |
| 26 | - * @param string $name | |
| 27 | - * @param array $component | |
| 28 | - * @param string $group ['general'|'advanced'] | |
| 29 | - * @return $this | |
| 30 | - */ | |
| 31 | - public function add($name, array $component, $group) | |
| 32 | - { | |
| 33 | - if (isset($this->items[$group])) { | |
| 34 | - $this->items[$group][$name] = $component; | |
| 35 | - } | |
| 26 | + /** | |
| 27 | + * Add a component into list [$items] | |
| 28 | + * | |
| 29 | + * @param string $name | |
| 30 | + * @param array $component | |
| 31 | + * @param string $group ['general'|'advanced'] | |
| 32 | + * | |
| 33 | + * @return $this | |
| 34 | + */ | |
| 35 | + public function add($name, array $component, $group) | |
| 36 | + { | |
| 37 | + if (isset($this->items[$group])) { | |
| 38 | + $this->items[$group][$name] = $component; | |
| 39 | + } | |
| 36 | 40 | |
| 37 | - return $this; | |
| 38 | - } | |
| 41 | + return $this; | |
| 42 | + } | |
| 39 | 43 | |
| 40 | - /** | |
| 41 | - * Remove a component from the list [$items] | |
| 42 | - * @param string $name | |
| 43 | - * @param string $group ['general'|'advanced'] | |
| 44 | - * @return $this | |
| 45 | - */ | |
| 46 | - public function remove($name, $group) | |
| 47 | - { | |
| 48 | - if (isset($this->items[$group])) { | |
| 49 | - unset($this->items[$group][$name]); | |
| 50 | - } | |
| 44 | + /** | |
| 45 | + * Remove a component from the list [$items] | |
| 46 | + * | |
| 47 | + * @param string $name | |
| 48 | + * @param string $group ['general'|'advanced'] | |
| 49 | + * | |
| 50 | + * @return $this | |
| 51 | + */ | |
| 52 | + public function remove($name, $group) | |
| 53 | + { | |
| 54 | + if (isset($this->items[$group])) { | |
| 55 | + unset($this->items[$group][$name]); | |
| 56 | + } | |
| 51 | 57 | |
| 52 | - return $this; | |
| 53 | - } | |
| 58 | + return $this; | |
| 59 | + } | |
| 54 | 60 | |
| 55 | - /** | |
| 56 | - * Modify an existing component | |
| 57 | - * @param string $name | |
| 58 | - * @param Closure $callback [to modify the component within] | |
| 59 | - * @param string $group | |
| 60 | - * @return $this | |
| 61 | - */ | |
| 62 | - public function update($name, Closure $callback, $group) | |
| 63 | - { | |
| 64 | - $element = $callback($this->items[$group][$name]); | |
| 65 | - $this->items[$group][$name] = $element; | |
| 66 | - return $this; | |
| 67 | - } | |
| 61 | + /** | |
| 62 | + * Modify an existing component | |
| 63 | + * | |
| 64 | + * @param string $name | |
| 65 | + * @param Closure $callback [to modify the component within] | |
| 66 | + * @param string $group | |
| 67 | + * | |
| 68 | + * @return $this | |
| 69 | + */ | |
| 70 | + public function update($name, Closure $callback, $group) | |
| 71 | + { | |
| 72 | + $element = $callback($this->items[$group][$name]); | |
| 73 | + $this->items[$group][$name] = $element; | |
| 74 | + return $this; | |
| 75 | + } | |
| 68 | 76 | |
| 69 | - /** | |
| 70 | - * Sort the components in list [$items] | |
| 71 | - * @param string $sortBy [key to sort by] | |
| 72 | - * @return $this | |
| 73 | - */ | |
| 74 | - public function sort($sortBy = 'index') | |
| 75 | - { | |
| 76 | - foreach ($this->items as $group => &$items) { | |
| 77 | - usort($items, function($a, $b) { | |
| 78 | - if (@$a['index'] == @$b['index']) { | |
| 79 | - return 0; | |
| 80 | - } | |
| 81 | - return @$a['index'] < @$b['index'] ? -1 : 1; | |
| 82 | - }); | |
| 83 | - } | |
| 84 | - | |
| 85 | - return $this; | |
| 86 | - } | |
| 77 | + /** | |
| 78 | + * Sort the components in list [$items] | |
| 79 | + * | |
| 80 | + * @param string $sortBy [key to sort by] | |
| 81 | + * | |
| 82 | + * @return $this | |
| 83 | + */ | |
| 84 | + public function sort($sortBy = 'index') | |
| 85 | + { | |
| 86 | + foreach ($this->items as $group => &$items) { | |
| 87 | + // NOTE: usort renumbers the groups 0..n, which is deliberate - the | |
| 88 | + // editor receives these as JSON and editor-inserter.vue declares | |
| 89 | + // generalMockList/advancedMockList/containerMockList as Array props, | |
| 90 | + // so they must not become JSON objects. Consumers that need to look | |
| 91 | + // an element up by name must re-key it themselves (see | |
| 92 | + // AiFormBuilder::getDefaultFields). | |
| 93 | + usort($items, function ($a, $b) { | |
| 94 | + if (@$a['index'] == @$b['index']) { | |
| 95 | + return 0; | |
| 96 | + } | |
| 97 | + return @$a['index'] < @$b['index'] ? -1 : 1; | |
| 98 | + }); | |
| 99 | + } | |
| 87 | 100 | |
| 88 | - /** | |
| 89 | - * Return array [$items] | |
| 90 | - * @return array | |
| 91 | - */ | |
| 92 | - public function toArray() | |
| 93 | - { | |
| 94 | - return $this->items; | |
| 95 | - } | |
| 101 | + return $this; | |
| 102 | + } | |
| 96 | 103 | |
| 97 | - /** | |
| 98 | - * Return array [$items] | |
| 99 | - * @return array | |
| 100 | - */ | |
| 101 | - public function jsonSerialize() | |
| 102 | - { | |
| 103 | - return $this->toArray(); | |
| 104 | - } | |
| 104 | + /** | |
| 105 | + * Return array [$items] | |
| 106 | + * | |
| 107 | + * @return array | |
| 108 | + */ | |
| 109 | + public function toArray() | |
| 110 | + { | |
| 111 | + return $this->items; | |
| 112 | + } | |
| 105 | 113 | |
| 106 | - /** | |
| 107 | - * Getter to hook proxy call | |
| 108 | - * @return mixed | |
| 109 | - */ | |
| 110 | - public function __get($key) | |
| 111 | - { | |
| 112 | - if (in_array($key, ['general', 'advanced'])) { | |
| 113 | - return new GroupSetterProxy($this, $key); | |
| 114 | - } | |
| 115 | - } | |
| 114 | + /** | |
| 115 | + * Return array [$items] | |
| 116 | + * | |
| 117 | + * @return array | |
| 118 | + */ | |
| 119 | + #[\ReturnTypeWillChange] | |
| 120 | + public function jsonSerialize() | |
| 121 | + { | |
| 122 | + return $this->toArray(); | |
| 123 | + } | |
| 124 | + | |
| 125 | + /** | |
| 126 | + * Getter to hook proxy call | |
| 127 | + * | |
| 128 | + * @return mixed | |
| 129 | + */ | |
| 130 | + public function __get($key) | |
| 131 | + { | |
| 132 | + if (in_array($key, ['general', 'advanced'])) { | |
| 133 | + return new GroupSetterProxy($this, $key); | |
| 134 | + } | |
| 135 | + } | |
| 116 | 136 | } |