PluginProbe
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration / 1.31
FluentBoards – Project Management, Task Management, Goal Tracking, Kanban Board, and, Team Collaboration v1.31
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 1.31, at vendor/wpfluent/framework/src/WPFluent/Support/Helper.php

409 lines 10.5 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 InvalidArgumentException;
8 use FluentBoards\Framework\Foundation\App;
9 use FluentBoards\Framework\Support\HigherOrderTapProxy;
10
11 class Helper
12 {
13 /**
14 * Create a collection from the given value.
15 *
16 * @param mixed $value
17 * @return \FluentBoards\Framework\Support\Collection
18 */
19 public static function collect($value = null)
20 {
21 return new Collection($value);
22 }
23
24 /**
25 * Fill in data where it's missing.
26 *
27 * @param mixed $target
28 * @param string|array $key
29 * @param mixed $value
30 * @return mixed
31 */
32 public function dataFill(&$target, $key, $value)
33 {
34 return static::dataSet($target, $key, $value, false);
35 }
36
37 /**
38 * Get an item from an array or object using "dot" notation.
39 *
40 * @param mixed $target
41 * @param string|array|int|null $key
42 * @param mixed $default
43 * @return mixed
44 */
45 public static function dataGet($target, $key, $default = null)
46 {
47 if (is_null($key)) {
48 return $target;
49 }
50
51 $key = is_array($key) ? $key : explode('.', $key);
52
53 while (($segment = array_shift($key)) !== null) {
54 if ('*' === $segment) {
55 if ($target instanceof Collection) {
56 $target = $target->all();
57 } elseif (!is_array($target)) {
58 return static::value($default);
59 }
60
61 $result = Arr::pluck($target, $key);
62
63 return in_array('*', $key) ? Arr::collapse($result) : $result;
64 }
65
66 if (Arr::accessible($target) && Arr::exists($target, $segment)) {
67 $target = $target[$segment];
68 } elseif (is_object($target) && isset($target->{$segment})) {
69 $target = $target->{$segment};
70 } else {
71 return static::value($default);
72 }
73 }
74
75 return $target;
76 }
77
78 /**
79 * Set an item on an array or object using dot notation.
80 *
81 * @param mixed $target
82 * @param string|array $key
83 * @param mixed $value
84 * @param bool $overwrite
85 * @return mixed
86 */
87 public static function dataSet(&$target, $key, $value, $overwrite = true)
88 {
89 $segments = is_array($key) ? $key : explode('.', $key);
90
91 if (($segment = array_shift($segments)) === '*') {
92 if (!Arr::accessible($target)) {
93 $target = [];
94 }
95
96 if ($segments) {
97 foreach ($target as &$inner) {
98 static::dataSet($inner, $segments, $value, $overwrite);
99 }
100 } elseif ($overwrite) {
101 foreach ($target as &$inner) {
102 $inner = $value;
103 }
104 }
105 } elseif (Arr::accessible($target)) {
106 if ($segments) {
107 if (!Arr::exists($target, $segment)) {
108 $target[$segment] = [];
109 }
110
111 static::dataSet($target[$segment], $segments, $value, $overwrite);
112 } elseif ($overwrite || !Arr::exists($target, $segment)) {
113 $target[$segment] = $value;
114 }
115 } elseif (is_object($target)) {
116 if ($segments) {
117 if (!isset($target->{$segment})) {
118 $target->{$segment} = [];
119 }
120
121 static::dataSet($target->{$segment}, $segments, $value, $overwrite);
122 } elseif ($overwrite || !isset($target->{$segment})) {
123 $target->{$segment} = $value;
124 }
125 } else {
126 $target = [];
127
128 if ($segments) {
129 static::dataSet($target[$segment], $segments, $value, $overwrite);
130 } elseif ($overwrite) {
131 $target[$segment] = $value;
132 }
133 }
134
135 return $target;
136 }
137
138 /**
139 * Get the first element of an array. Useful for method chaining.
140 *
141 * @param array $array
142 * @return mixed
143 */
144 public static function head($array)
145 {
146 return reset($array);
147 }
148
149 /**
150 * Get the last element from an array.
151 *
152 * @param array $array
153 * @return mixed
154 */
155 public static function last($array)
156 {
157 return end($array);
158 }
159
160 /**
161 * Return the default value of the given value.
162 *
163 * @param mixed $value
164 * @return mixed
165 */
166 public static function value($value, ...$args)
167 {
168 return $value instanceof Closure ? $value(...$args) : $value;
169 }
170
171 /**
172 * Call the given Closure with the given value then return the value.
173 *
174 * @param mixed $value
175 * @param callable|null $callback
176 * @return mixed
177 */
178 public static function tap($value, $callback = null)
179 {
180 if (is_null($callback)) {
181 return new HigherOrderTapProxy($value);
182 }
183
184 $callback($value);
185
186 return $value;
187 }
188
189 /**
190 * Dispatch an event and call the listeners.
191 *
192 * @param string|object $event
193 * @param mixed $payload
194 * @param bool $halt
195 * @return array|null
196 */
197 public static function event(...$args)
198 {
199 return App::make('events')->dispatch(...$args);
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 * @param int|WP_User $user Optional. User to check. Defaults to current user.
265 * @return string[] Mime types keyed by the file extension regex corresponding types.
266 */
267 public static function getAllowedMimeTypes()
268 {
269 return get_allowed_mime_types();
270 }
271
272 /**
273 * Get the content of a JSON file as array.
274 *
275 * @param string $filename
276 * @param array $options
277 * @return array json decoded content of the file
278 */
279 public static function getJsonFile($filename, $options = [])
280 {
281 return wp_json_file_decode($filename, $options);
282 }
283
284 /**
285 * Gets the size of a directory.
286 *
287 * @param string $directory Full path of a directory.
288 * @return int|false|null Size in bytes if valid directory or false. Null if timeout.
289 */
290 public static function getSizeOf($dirPath)
291 {
292 return recurse_dirsize($dirPath);
293 }
294
295 /**
296 * Creates an \stdClass from an array
297 * @param array $array
298 * @return \stdClass
299 */
300 public static function objectCreate(array $array)
301 {
302 return StdObject::create($array);
303 }
304
305 /**
306 * Transforms an \stdClass to array
307 * @param \stdClass $object
308 * @return array
309 */
310 public static function objectToArray(\stdClass $object)
311 {
312 return StdObject::toArray($object);
313 }
314
315 /**
316 * Get an item from an object using "dot" notation.
317 *
318 * @param object $object
319 * @param string|null $key
320 * @param mixed $default
321 * @return mixed
322 */
323 public static function objectGet($object, $key, $default = null)
324 {
325 if (is_null($key) || trim($key) === '') {
326 return $object;
327 }
328
329 foreach (explode('.', $key) as $segment) {
330 if (! is_object($object) || ! isset($object->{$segment})) {
331 return static::value($default);
332 }
333
334 $object = $object->{$segment};
335 }
336
337 return $object;
338 }
339
340 /**
341 * Replace a given pattern with each value in the array in sequentially.
342 *
343 * @param string $pattern
344 * @param array $replacements
345 * @param string $subject
346 * @return string
347 */
348 public static function pregReplaceArray($pattern, array $replacements, $subject)
349 {
350 return preg_replace_callback($pattern, function () use (&$replacements) {
351 foreach ($replacements as $value) {
352 return array_shift($replacements);
353 }
354 }, $subject);
355 }
356
357 /**
358 * Executes a callback and returns the captured output as a string.
359 *
360 * @param callable $callback
361 * @return string
362 */
363 public static function capture(callable $callback)
364 {
365 ob_start(null);
366 try {
367 $callback();
368 return ob_get_clean();
369 } catch (\Throwable $e) {
370 ob_end_clean();
371 throw $e;
372 }
373 }
374
375 /**
376 * Compares two values in the same way that PHP does.
377 *
378 * @param mixed $left
379 * @param string $operator
380 * @param mixed $right
381 * @return bool
382 */
383 public static function compare($left, $operator, $right)
384 {
385 switch ($operator) {
386 case '>':
387 return $left > $right;
388 case '>=':
389 return $left >= $right;
390 case '<':
391 return $left < $right;
392 case '<=':
393 return $left <= $right;
394 case '=':
395 case '==':
396 return $left == $right;
397 case '===':
398 return $left === $right;
399 case '!=':
400 case '<>':
401 return $left != $right;
402 case '!==':
403 return $left !== $right;
404 default:
405 throw new InvalidArgumentException("Unknown operator '$operator'");
406 }
407 }
408 }
409