PluginProbe
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses / 1.0.92
FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses v1.0.92
2.10.0 2.10.01 2.9.1 2.9.0 2.8.1 2.8.0 2.7.7 2.7.5 2.7.0 2.6.01 2.6.0 2.5.0 2.4.01 trunk 1.0.90 1.0.91 1.0.92 1.0.93 1.0.94 1.0.95 1.0.96 1.0.97 1.0.98 1.0.99 1.1.0 All 77 releases
fluent-community / vendor / wpfluent / framework / src / WPFluent / Support / Helper.php

Helper.php in FluentCommunity – Ultra-Fast High-Performance Social Network, Community, LMS & Online Courses 1.0.92, at vendor/wpfluent/framework/src/WPFluent/Support/Helper.php

429 lines 10.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FluentCommunity\Framework\Support;
4
5 use Closure;
6 use Exception;
7 use Throwable;
8 use InvalidArgumentException;
9 use FluentCommunity\Framework\Foundation\App;
10 use FluentCommunity\Framework\Support\HigherOrderTapProxy;
11
12 class Helper
13 {
14 /**
15 * Create a collection from the given value.
16 *
17 * @param mixed $value
18 * @return \FluentCommunity\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 the listeners.
192 *
193 * @param string|object $event
194 * @param mixed $payload
195 * @param bool $halt
196 * @return array|null
197 */
198 public static function event(...$args)
199 {
200 return App::make('events')->dispatch(...$args);
201 }
202
203 /**
204 * Retry an operation a given number of times.
205 *
206 * @param int $times
207 * @param callable $callback
208 * @param int|\Closure $sleepMilliseconds
209 * @param callable|null $when
210 * @return mixed
211 *
212 * @throws \Exception
213 */
214 public static function retry(
215 $times,
216 callable $callback,
217 $sleepMilliseconds = 0,
218 $when = null
219 )
220 {
221 $attempts = 0;
222
223 beginning:
224 $attempts++;
225 $times--;
226
227 try {
228 return $callback($attempts);
229 } catch (Exception $e) {
230 if ($times < 1 || ($when && ! $when($e))) {
231 throw $e;
232 }
233
234 if ($sleepMilliseconds) {
235 usleep(static::value($sleepMilliseconds, $attempts) * 1000);
236 }
237
238 goto beginning;
239 }
240 }
241
242 /**
243 * Retrieve header status text by http code
244 * @param int $code HTTP status code
245 * @return string
246 */
247 public static function getHeaderStatusText($code)
248 {
249 return get_status_header_desc($code);
250 }
251
252 /**
253 * Retrieve the writable temp dir path
254 *
255 * @return string
256 */
257 public static function getTempDirPath()
258 {
259 return get_temp_dir();
260 }
261
262 /**
263 * Retrieves the list of allowed mime types and file extensions.
264 *
265 * @return string[] Mime types keyed by the file extension regex.
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 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 * Executes an action and returns the captured output as a string.
377 *
378 * @param string $action
379 * @param array $params the data to passed in action
380 * @return string
381 * @throws Throwable
382 */
383 public static function captureAction($action, ...$params)
384 {
385 ob_start(null);
386 try {
387 do_action_ref_array($action, $params);
388 return ob_get_clean();
389 } catch (Throwable $e) {
390 ob_end_clean();
391 throw $e;
392 }
393 }
394
395 /**
396 * Compares two values in the same way that PHP does.
397 *
398 * @param mixed $left
399 * @param string $operator
400 * @param mixed $right
401 * @return bool
402 */
403 public static function compare($left, $operator, $right)
404 {
405 switch ($operator) {
406 case '>':
407 return $left > $right;
408 case '>=':
409 return $left >= $right;
410 case '<':
411 return $left < $right;
412 case '<=':
413 return $left <= $right;
414 case '=':
415 case '==':
416 return $left == $right;
417 case '===':
418 return $left === $right;
419 case '!=':
420 case '<>':
421 return $left != $right;
422 case '!==':
423 return $left !== $right;
424 default:
425 throw new InvalidArgumentException("Unknown operator '$operator'");
426 }
427 }
428 }
429