PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 2.1.0
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v2.1.0
2.1.0 2.0.15 2.0.12 2.0.10 2.0.4 2.0.1 2.0.0 1.95.3 1.95.2 1.95 1.91.6 trunk 1.11 1.12 1.13 1.20 1.21 1.22 1.23 1.30 1.31 1.32 1.35 1.40 1.41 All 42 releases
fluent-boards / vendor / wpfluent / framework / src / WPFluent / Support / Helper.php

Helper.php in FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration 2.1.0, at vendor/wpfluent/framework/src/WPFluent/Support/Helper.php

439 lines 11.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentBoards\Framework\Support;
4
5 use Closure;
6 use Exception;
7 use Throwable;
8 use InvalidArgumentException;
9 use FluentBoards\Framework\Foundation\App;
10 use FluentBoards\Framework\Support\HigherOrderTapProxy;
11
12 class Helper
13 {
14 /**
15 * Create a collection from the given value.
16 *
17 * @param mixed $value
18 * @return \FluentBoards\Framework\Support\Collection
19 */
20 public static function collect($value = null)
21 {
22 return new Collection($value);
23 }
24
25 /**
26 * Fill in data where it's missing.
27 *
28 * @param mixed $target
29 * @param string|array $key
30 * @param mixed $value
31 * @return mixed
32 */
33 public function dataFill(&$target, $key, $value)
34 {
35 return static::dataSet($target, $key, $value, false);
36 }
37
38 /**
39 * Get an item from an array or object using "dot" notation.
40 *
41 * @param mixed $target
42 * @param string|array|int|null $key
43 * @param mixed $default
44 * @return mixed
45 */
46 public static function dataGet($target, $key, $default = null)
47 {
48 if (is_null($key)) {
49 return $target;
50 }
51
52 $key = is_array($key) ? $key : explode('.', $key);
53
54 while (($segment = array_shift($key)) !== null) {
55 if ('*' === $segment) {
56 if ($target instanceof Collection) {
57 $target = $target->all();
58 } elseif (!is_array($target)) {
59 return static::value($default);
60 }
61
62 $result = Arr::pluck($target, $key);
63
64 return in_array('*', $key) ? Arr::collapse($result) : $result;
65 }
66
67 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
68 $target = $target[$segment];
69 } elseif (is_object($target) && isset($target->{$segment})) {
70 $target = $target->{$segment};
71 } else {
72 return static::value($default);
73 }
74 }
75
76 return $target;
77 }
78
79 /**
80 * Set an item on an array or object using dot notation.
81 *
82 * @param mixed $target
83 * @param string|array $key
84 * @param mixed $value
85 * @param bool $overwrite
86 * @return mixed
87 */
88 public static function dataSet(&$target, $key, $value, $overwrite = true)
89 {
90 $segments = is_array($key) ? $key : explode('.', $key);
91
92 if (($segment = array_shift($segments)) === '*') {
93 if (!Arr::accessible($target)) {
94 $target = [];
95 }
96
97 if ($segments) {
98 foreach ($target as &$inner) {
99 static::dataSet($inner, $segments, $value, $overwrite);
100 }
101 } elseif ($overwrite) {
102 foreach ($target as &$inner) {
103 $inner = $value;
104 }
105 }
106 } elseif (Arr::accessible($target)) {
107 if ($segments) {
108 if (!Arr::exists($target, $segment)) {
109 $target[$segment] = [];
110 }
111
112 static::dataSet($target[$segment], $segments, $value, $overwrite);
113 } elseif ($overwrite || !Arr::exists($target, $segment)) {
114 $target[$segment] = $value;
115 }
116 } elseif (is_object($target)) {
117 if ($segments) {
118 if (!isset($target->{$segment})) {
119 $target->{$segment} = [];
120 }
121
122 static::dataSet($target->{$segment}, $segments, $value, $overwrite);
123 } elseif ($overwrite || !isset($target->{$segment})) {
124 $target->{$segment} = $value;
125 }
126 } else {
127 $target = [];
128
129 if ($segments) {
130 static::dataSet($target[$segment], $segments, $value, $overwrite);
131 } elseif ($overwrite) {
132 $target[$segment] = $value;
133 }
134 }
135
136 return $target;
137 }
138
139 /**
140 * Get the first element of an array. Useful for method chaining.
141 *
142 * @param array $array
143 * @return mixed
144 */
145 public static function head($array)
146 {
147 return reset($array);
148 }
149
150 /**
151 * Get the last element from an array.
152 *
153 * @param array $array
154 * @return mixed
155 */
156 public static function last($array)
157 {
158 return end($array);
159 }
160
161 /**
162 * Return the default value of the given value.
163 *
164 * @param mixed $value
165 * @return mixed
166 */
167 public static function value($value, ...$args)
168 {
169 return $value instanceof Closure ? $value(...$args) : $value;
170 }
171
172 /**
173 * Call the given Closure with the given value then return the value.
174 *
175 * @param mixed $value
176 * @param callable|null $callback
177 * @return mixed
178 */
179 public static function tap($value, $callback = null)
180 {
181 if (is_null($callback)) {
182 return new HigherOrderTapProxy($value);
183 }
184
185 $callback($value);
186
187 return $value;
188 }
189
190 /**
191 * Dispatch an event and call its listeners.
192 *
193 * @param string|object $event Event name or object
194 * @param mixed ...$payload Optional arguments passed to listeners
195 * @return mixed Result of the event dispatch
196 */
197 public static function event($event, ...$payload)
198 {
199 return App::make('events')->dispatch($event, ...$payload);
200 }
201
202 /**
203 * Retry an operation a given number of times.
204 *
205 * @param int $times
206 * @param callable $callback
207 * @param int|\Closure $sleepMilliseconds
208 * @param callable|null $when
209 * @return mixed
210 *
211 * @throws \Exception
212 */
213 public static function retry(
214 $times,
215 callable $callback,
216 $sleepMilliseconds = 0,
217 $when = null
218 )
219 {
220 $attempts = 0;
221
222 beginning:
223 $attempts++;
224 $times--;
225
226 try {
227 return $callback($attempts);
228 } catch (Exception $e) {
229 if ($times < 1 || ($when && ! $when($e))) {
230 throw $e;
231 }
232
233 if ($sleepMilliseconds) {
234 usleep(static::value($sleepMilliseconds, $attempts) * 1000);
235 }
236
237 goto beginning;
238 }
239 }
240
241 /**
242 * Retrieve header status text by http code
243 * @param int $code HTTP status code
244 * @return string
245 */
246 public static function getHeaderStatusText($code)
247 {
248 return get_status_header_desc($code);
249 }
250
251 /**
252 * Retrieve the writable temp dir path
253 *
254 * @return string
255 */
256 public static function getTempDirPath()
257 {
258 return get_temp_dir();
259 }
260
261 /**
262 * Retrieves the list of allowed mime types and file extensions.
263 *
264 * @return string[] Mime types keyed by the file extension regex.
265 */
266 public static function getAllowedMimeTypes()
267 {
268 return get_allowed_mime_types();
269 }
270
271 /**
272 * Get the content of a JSON file as array.
273 *
274 * @param string $filename
275 * @param array $options
276 * @return array json decoded content of the file
277 */
278 public static function getJsonFile($filename, $options = [])
279 {
280 return wp_json_file_decode($filename, $options);
281 }
282
283 /**
284 * Gets the size of a directory.
285 *
286 * @param string $dirPath Full path of a directory.
287 * @return int|false|null Size in bytes or false. Null if timeout.
288 */
289 public static function getSizeOf($dirPath)
290 {
291 return recurse_dirsize($dirPath);
292 }
293
294 /**
295 * Creates an \stdClass from an array recursively.
296 *
297 * @param array $array
298 * @return \stdClass
299 */
300 public static function objectCreate(array $array)
301 {
302 $object = new \stdClass;
303
304 foreach ($array as $key => $value) {
305 if (is_array($value)) {
306 $object->{$key} = static::objectCreate($value);
307 } else {
308 $object->{$key} = $value;
309 }
310 }
311
312 return $object;
313 }
314
315 /**
316 * Transforms an \stdClass to array
317 * @param \stdClass $object
318 * @return array
319 */
320 public static function objectToArray(\stdClass $object)
321 {
322 return json_decode(json_encode($object), true);
323 }
324
325 /**
326 * Get an item from an object using "dot" notation.
327 *
328 * @param object $object
329 * @param string|null $key
330 * @param mixed $default
331 * @return mixed
332 */
333 public static function objectGet($object, $key, $default = null)
334 {
335 if (is_null($key) || trim($key) === '') {
336 return $object;
337 }
338
339 foreach (explode('.', $key) as $segment) {
340 if (! is_object($object) || ! isset($object->{$segment})) {
341 return static::value($default);
342 }
343
344 $object = $object->{$segment};
345 }
346
347 return $object;
348 }
349
350 /**
351 * Replace a given pattern with each value in the array in sequentially.
352 *
353 * @param string $pattern
354 * @param array $replacements
355 * @param string $subject
356 * @return string
357 */
358 public static function pregReplaceArray($pattern, array $replacements, $subject)
359 {
360 return preg_replace_callback($pattern, function () use (&$replacements) {
361 foreach ($replacements as $value) {
362 return array_shift($replacements);
363 }
364 }, $subject);
365 }
366
367 /**
368 * Executes a callback and returns the captured output as a string.
369 *
370 * @param callable $callback
371 * @return string
372 */
373 public static function capture(callable $callback)
374 {
375 ob_start(null);
376 try {
377 $callback();
378 return ob_get_clean();
379 } catch (Throwable $e) {
380 ob_end_clean();
381 throw $e;
382 }
383 }
384
385 /**
386 * Executes an action and returns the captured output as a string.
387 *
388 * @param string $action
389 * @param array $params the data to passed in action
390 * @return string
391 * @throws Throwable
392 */
393 public static function captureAction($action, ...$params)
394 {
395 ob_start(null);
396 try {
397 do_action_ref_array($action, $params);
398 return ob_get_clean();
399 } catch (Throwable $e) {
400 ob_end_clean();
401 throw $e;
402 }
403 }
404
405 /**
406 * Compares two values in the same way that PHP does.
407 *
408 * @param mixed $left
409 * @param string $operator
410 * @param mixed $right
411 * @return bool
412 */
413 public static function compare($left, $operator, $right)
414 {
415 switch ($operator) {
416 case '>':
417 return $left > $right;
418 case '>=':
419 return $left >= $right;
420 case '<':
421 return $left < $right;
422 case '<=':
423 return $left <= $right;
424 case '=':
425 case '==':
426 return $left == $right;
427 case '===':
428 return $left === $right;
429 case '!=':
430 case '<>':
431 return $left != $right;
432 case '!==':
433 return $left !== $right;
434 default:
435 throw new InvalidArgumentException("Unknown operator '$operator'");
436 }
437 }
438 }
439