PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 6.2.13
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v6.2.13
6.2.14 6.2.13 6.2.12 6.2.10 6.2.11 6.2.9 6.2.8 6.2.7 6.2.6 6.2.5 6.2.4 6.2.3 6.2.2 3.6.22 3.6.31 3.6.40 3.6.41 3.6.42 3.6.50 3.6.51 3.6.60 3.6.61 3.6.62 3.6.64 3.6.65 All 196 releases
fluentform / app / Services / FormBuilder / Components.php

Components.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 6.2.13, at app/Services/FormBuilder/Components.php

137 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\App\Services\FormBuilder;
4
5 use Closure;
6
7 class Components implements \JsonSerializable
8 {
9 /**
10 * $items [Components list]
11 *
12 * @var array
13 */
14 protected $items = [];
15
16 /**
17 * Build the object instance
18 *
19 * @param array $items
20 */
21 public function __construct(array $items)
22 {
23 $this->items = $items;
24 }
25
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 }
40
41 return $this;
42 }
43
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 }
57
58 return $this;
59 }
60
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 }
76
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 }
100
101 return $this;
102 }
103
104 /**
105 * Return array [$items]
106 *
107 * @return array
108 */
109 public function toArray()
110 {
111 return $this->items;
112 }
113
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 }
136 }
137