PluginProbe
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder / 3.6.31
Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder v3.6.31
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 3.6.66 All 195 releases
fluentform / framework / Helpers / ArrayHelper.php

ArrayHelper.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.31, at framework/Helpers/ArrayHelper.php

270 lines 6.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentForm\Framework\Helpers;
4
5 use Closure;
6 use ArrayAccess;
7
8 class ArrayHelper
9 {
10 /**
11 * Taken from Illuminate\Support\Arr (Laravel Framework)
12 */
13
14 /**
15 * Check if an item or items exist in an array using "dot" notation.
16 *
17 * @param \ArrayAccess|array $array
18 * @param string|array $keys
19 * @return bool
20 */
21 public static function has($array, $keys)
22 {
23 if (is_null($keys)) {
24 return false;
25 }
26
27 $keys = (array) $keys;
28
29 if (! $array) {
30 return false;
31 }
32
33 if ($keys === []) {
34 return false;
35 }
36
37 foreach ($keys as $key) {
38 $subKeyArray = $array;
39
40 if (static::exists($array, $key)) {
41 continue;
42 }
43
44 foreach (explode('.', $key) as $segment) {
45 if (static::accessible($subKeyArray) && static::exists($subKeyArray, $segment)) {
46 $subKeyArray = $subKeyArray[$segment];
47 } else {
48 return false;
49 }
50 }
51 }
52
53 return true;
54 }
55
56 /**
57 * Get an item from an array using "dot" notation.
58 *
59 * @param \ArrayAccess|array $array
60 * @param string $key
61 * @param mixed $default
62 * @return mixed
63 */
64 public static function get($array, $key, $default = null)
65 {
66 if (! static::accessible($array)) {
67 return static::value($default);
68 }
69
70 if (is_null($key)) {
71 return $array;
72 }
73
74 if (static::exists($array, $key)) {
75 return $array[$key];
76 }
77
78 foreach (explode('.', $key) as $segment) {
79 if (static::accessible($array) && static::exists($array, $segment)) {
80 $array = $array[$segment];
81 } else {
82 return static::value($default);
83 }
84 }
85
86 return $array;
87 }
88
89 /**
90 * Set an array item to a given value using "dot" notation.
91 *
92 * If no key is given to the method, the entire array will be replaced.
93 *
94 * @param array $array
95 * @param string $key
96 * @param mixed $value
97 * @return array
98 */
99 public static function set(&$array, $key, $value)
100 {
101 if (is_null($key)) {
102 return $array = $value;
103 }
104
105 $keys = explode('.', $key);
106
107 while (count($keys) > 1) {
108 $key = array_shift($keys);
109
110 // If the key doesn't exist at this depth, we will just create an empty array
111 // to hold the next value, allowing us to create the arrays to hold final
112 // values at the correct depth. Then we'll keep digging into the array.
113 if (! isset($array[$key]) || ! is_array($array[$key])) {
114 $array[$key] = [];
115 }
116
117 $array = &$array[$key];
118 }
119
120 $array[array_shift($keys)] = $value;
121
122 return $array;
123 }
124
125 /**
126 * Get a subset of the items from the given array.
127 *
128 * @param array $array
129 * @param array|string $keys
130 * @return array
131 */
132 public static function only($array, $keys)
133 {
134 return array_intersect_key($array, array_flip((array) $keys));
135 }
136
137 /**
138 * Get all of the given array except for a specified array of items.
139 *
140 * @param array $array
141 * @param array|string $keys
142 * @return array
143 */
144 public static function except($array, $keys)
145 {
146 static::forget($array, $keys);
147
148 return $array;
149 }
150
151 /**
152 * Remove one or many array items from a given array using "dot" notation.
153 *
154 * @param array $array
155 * @param array|string $keys
156 * @return void
157 */
158 public static function forget(&$array, $keys)
159 {
160 $original = &$array;
161
162 $keys = (array) $keys;
163
164 if (count($keys) === 0) {
165 return;
166 }
167
168 foreach ($keys as $key) {
169 // if the exact key exists in the top-level, remove it
170 if (static::exists($array, $key)) {
171 unset($array[$key]);
172
173 continue;
174 }
175
176 $parts = explode('.', $key);
177
178 // clean up before each pass
179 $array = &$original;
180
181 while (count($parts) > 1) {
182 $part = array_shift($parts);
183
184 if (isset($array[$part]) && is_array($array[$part])) {
185 $array = &$array[$part];
186 } else {
187 continue 2;
188 }
189 }
190
191 unset($array[array_shift($parts)]);
192 }
193 }
194
195 /**
196 * Determine whether the given value is array accessible.
197 *
198 * @param mixed $value
199 * @return bool
200 */
201 public static function accessible($value)
202 {
203 return is_array($value) || $value instanceof ArrayAccess;
204 }
205
206 /**
207 * Determine if the given key exists in the provided array.
208 *
209 * @param \ArrayAccess|array $array
210 * @param string|int $key
211 * @return bool
212 */
213 public static function exists($array, $key)
214 {
215 if ($array instanceof ArrayAccess) {
216 return $array->offsetExists($key);
217 }
218
219 return array_key_exists($key, $array);
220 }
221
222 /**
223 * Return the default value of the given value.
224 *
225 * @param mixed $value
226 * @return mixed
227 */
228 public static function value($value)
229 {
230 return $value instanceof Closure ? $value() : $value;
231 }
232
233 /**
234 * Flatten a multi-dimensional associative array with dots.
235 *
236 * @param array $array
237 * @param string $prepend
238 *
239 * @return array
240 */
241 public static function dot($array, $prepend = '')
242 {
243 $results = [];
244
245 foreach ($array as $key => $value) {
246 if (is_array($value) && ! empty($value)) {
247 $results = array_merge($results, static::dot($value, $prepend.$key.'.'));
248 } else {
249 $results[$prepend.$key] = $value;
250 }
251 }
252
253 return $results;
254 }
255
256 public static function isTrue($array, $key)
257 {
258 $value = self::get($array, $key);
259
260 if(is_bool($value)) {
261 return $value;
262 }
263
264 if($value == 'false' || $value == '0' || !$value) {
265 return false;
266 }
267 return true;
268 }
269 }
270