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.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 / fluentvalidator / src / Arr.php

Arr.php in Fluent Forms – Customizable Contact Forms, Survey, Quiz, & Conversational Form Builder 3.6.31, at app/Services/fluentvalidator/src/Arr.php

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