PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 4.3.18
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v4.3.18
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 4.3.18, at app/Services/FormBuilder/Components.php

130 lines 2.6 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 usort($items, function ($a, $b) {
88 if (@$a['index'] == @$b['index']) {
89 return 0;
90 }
91 return @$a['index'] < @$b['index'] ? -1 : 1;
92 });
93 }
94
95 return $this;
96 }
97
98 /**
99 * Return array [$items]
100 *
101 * @return array
102 */
103 public function toArray()
104 {
105 return $this->items;
106 }
107
108 /**
109 * Return array [$items]
110 *
111 * @return array
112 */
113 public function jsonSerialize()
114 {
115 return $this->toArray();
116 }
117
118 /**
119 * Getter to hook proxy call
120 *
121 * @return mixed
122 */
123 public function __get($key)
124 {
125 if (in_array($key, ['general', 'advanced'])) {
126 return new GroupSetterProxy($this, $key);
127 }
128 }
129 }
130