| 1 |
<?php |
| 2 |
|
| 3 |
namespace FluentBooking\Framework\Support; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use Exception; |
| 7 |
use FluentBooking\Framework\Foundation\App; |
| 8 |
use FluentBooking\Framework\Support\HigherOrderTapProxy; |
| 9 |
|
| 10 |
class Helper |
| 11 |
{ |
| 12 |
/** |
| 13 |
* Create a collection from the given value. |
| 14 |
* |
| 15 |
* @param mixed $value |
| 16 |
* @return \FluentBooking\Framework\Support\Collection |
| 17 |
*/ |
| 18 |
public static function collect($value = null) |
| 19 |
{ |
| 20 |
return new Collection($value); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Fill in data where it's missing. |
| 25 |
* |
| 26 |
* @param mixed $target |
| 27 |
* @param string|array $key |
| 28 |
* @param mixed $value |
| 29 |
* @return mixed |
| 30 |
*/ |
| 31 |
public function dataFill(&$target, $key, $value) |
| 32 |
{ |
| 33 |
return static::dataSet($target, $key, $value, false); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Get an item from an array or object using "dot" notation. |
| 38 |
* |
| 39 |
* @param mixed $target |
| 40 |
* @param string|array|int|null $key |
| 41 |
* @param mixed $default |
| 42 |
* @return mixed |
| 43 |
*/ |
| 44 |
public static function dataGet($target, $key, $default = null) |
| 45 |
{ |
| 46 |
if (is_null($key)) { |
| 47 |
return $target; |
| 48 |
} |
| 49 |
|
| 50 |
$key = is_array($key) ? $key : explode('.', $key); |
| 51 |
|
| 52 |
while (($segment = array_shift($key)) !== null) { |
| 53 |
if ('*' === $segment) { |
| 54 |
if ($target instanceof Collection) { |
| 55 |
$target = $target->all(); |
| 56 |
} elseif (!is_array($target)) { |
| 57 |
return static::value($default); |
| 58 |
} |
| 59 |
|
| 60 |
$result = Arr::pluck($target, $key); |
| 61 |
|
| 62 |
return in_array('*', $key) ? Arr::collapse($result) : $result; |
| 63 |
} |
| 64 |
|
| 65 |
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
| 66 |
$target = $target[$segment]; |
| 67 |
} elseif (is_object($target) && isset($target->{$segment})) { |
| 68 |
$target = $target->{$segment}; |
| 69 |
} else { |
| 70 |
return static::value($default); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
return $target; |
| 75 |
} |
| 76 |
|
| 77 |
/** |
| 78 |
* Set an item on an array or object using dot notation. |
| 79 |
* |
| 80 |
* @param mixed $target |
| 81 |
* @param string|array $key |
| 82 |
* @param mixed $value |
| 83 |
* @param bool $overwrite |
| 84 |
* @return mixed |
| 85 |
*/ |
| 86 |
public static function dataSet(&$target, $key, $value, $overwrite = true) |
| 87 |
{ |
| 88 |
$segments = is_array($key) ? $key : explode('.', $key); |
| 89 |
|
| 90 |
if (($segment = array_shift($segments)) === '*') { |
| 91 |
if (!Arr::accessible($target)) { |
| 92 |
$target = []; |
| 93 |
} |
| 94 |
|
| 95 |
if ($segments) { |
| 96 |
foreach ($target as &$inner) { |
| 97 |
static::dataSet($inner, $segments, $value, $overwrite); |
| 98 |
} |
| 99 |
} elseif ($overwrite) { |
| 100 |
foreach ($target as &$inner) { |
| 101 |
$inner = $value; |
| 102 |
} |
| 103 |
} |
| 104 |
} elseif (Arr::accessible($target)) { |
| 105 |
if ($segments) { |
| 106 |
if (!Arr::exists($target, $segment)) { |
| 107 |
$target[$segment] = []; |
| 108 |
} |
| 109 |
|
| 110 |
static::dataSet($target[$segment], $segments, $value, $overwrite); |
| 111 |
} elseif ($overwrite || !Arr::exists($target, $segment)) { |
| 112 |
$target[$segment] = $value; |
| 113 |
} |
| 114 |
} elseif (is_object($target)) { |
| 115 |
if ($segments) { |
| 116 |
if (!isset($target->{$segment})) { |
| 117 |
$target->{$segment} = []; |
| 118 |
} |
| 119 |
|
| 120 |
static::dataSet($target->{$segment}, $segments, $value, $overwrite); |
| 121 |
} elseif ($overwrite || !isset($target->{$segment})) { |
| 122 |
$target->{$segment} = $value; |
| 123 |
} |
| 124 |
} else { |
| 125 |
$target = []; |
| 126 |
|
| 127 |
if ($segments) { |
| 128 |
static::dataSet($target[$segment], $segments, $value, $overwrite); |
| 129 |
} elseif ($overwrite) { |
| 130 |
$target[$segment] = $value; |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return $target; |
| 135 |
} |
| 136 |
|
| 137 |
/** |
| 138 |
* Get the first element of an array. Useful for method chaining. |
| 139 |
* |
| 140 |
* @param array $array |
| 141 |
* @return mixed |
| 142 |
*/ |
| 143 |
public static function head($array) |
| 144 |
{ |
| 145 |
return reset($array); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* Get the last element from an array. |
| 150 |
* |
| 151 |
* @param array $array |
| 152 |
* @return mixed |
| 153 |
*/ |
| 154 |
public static function last($array) |
| 155 |
{ |
| 156 |
return end($array); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Return the default value of the given value. |
| 161 |
* |
| 162 |
* @param mixed $value |
| 163 |
* @return mixed |
| 164 |
*/ |
| 165 |
public static function value($value, ...$args) |
| 166 |
{ |
| 167 |
return $value instanceof Closure ? $value(...$args) : $value; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* Call the given Closure with the given value then return the value. |
| 172 |
* |
| 173 |
* @param mixed $value |
| 174 |
* @param callable|null $callback |
| 175 |
* @return mixed |
| 176 |
*/ |
| 177 |
public static function tap($value, $callback = null) |
| 178 |
{ |
| 179 |
if (is_null($callback)) { |
| 180 |
return new HigherOrderTapProxy($value); |
| 181 |
} |
| 182 |
|
| 183 |
$callback($value); |
| 184 |
|
| 185 |
return $value; |
| 186 |
} |
| 187 |
|
| 188 |
/** |
| 189 |
* Dispatch an event and call the listeners. |
| 190 |
* |
| 191 |
* @param string|object $event |
| 192 |
* @param mixed $payload |
| 193 |
* @param bool $halt |
| 194 |
* @return array|null |
| 195 |
*/ |
| 196 |
public static function event(...$args) |
| 197 |
{ |
| 198 |
return App::make('events')->dispatch(...$args); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* Retry an operation a given number of times. |
| 203 |
* |
| 204 |
* @param int $times |
| 205 |
* @param callable $callback |
| 206 |
* @param int|\Closure $sleepMilliseconds |
| 207 |
* @param callable|null $when |
| 208 |
* @return mixed |
| 209 |
* |
| 210 |
* @throws \Exception |
| 211 |
*/ |
| 212 |
public static function retry( |
| 213 |
$times, |
| 214 |
callable $callback, |
| 215 |
$sleepMilliseconds = 0, |
| 216 |
$when = null |
| 217 |
) |
| 218 |
{ |
| 219 |
$attempts = 0; |
| 220 |
|
| 221 |
beginning: |
| 222 |
$attempts++; |
| 223 |
$times--; |
| 224 |
|
| 225 |
try { |
| 226 |
return $callback($attempts); |
| 227 |
} catch (Exception $e) { |
| 228 |
if ($times < 1 || ($when && ! $when($e))) { |
| 229 |
throw $e; |
| 230 |
} |
| 231 |
|
| 232 |
if ($sleepMilliseconds) { |
| 233 |
usleep(static::value($sleepMilliseconds, $attempts) * 1000); |
| 234 |
} |
| 235 |
|
| 236 |
goto beginning; |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
/** |
| 241 |
* Retrieve header status text by http code |
| 242 |
* @param int $code HTTP status code |
| 243 |
* @return string |
| 244 |
*/ |
| 245 |
public static function getHeaderStatusText($code) |
| 246 |
{ |
| 247 |
return get_status_header_desc($code); |
| 248 |
} |
| 249 |
|
| 250 |
/** |
| 251 |
* Retrieve the writable temp dir path |
| 252 |
* |
| 253 |
* @return string |
| 254 |
*/ |
| 255 |
public static function getTempDirPath() |
| 256 |
{ |
| 257 |
return get_temp_dir(); |
| 258 |
} |
| 259 |
|
| 260 |
/** |
| 261 |
* Retrieves the list of allowed mime types and file extensions. |
| 262 |
* |
| 263 |
* @param int|WP_User $user Optional. User to check. Defaults to current user. |
| 264 |
* @return string[] Mime types keyed by the file extension regex corresponding types. |
| 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 $directory Full path of a directory. |
| 287 |
* @return int|false|null Size in bytes if valid directory 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 |
| 296 |
* @param array $array |
| 297 |
* @return \stdClass |
| 298 |
*/ |
| 299 |
public static function objectCreate(array $array) |
| 300 |
{ |
| 301 |
return StdObject::create($array); |
| 302 |
} |
| 303 |
|
| 304 |
/** |
| 305 |
* Transforms an \stdClass to array |
| 306 |
* @param \stdClass $object |
| 307 |
* @return array |
| 308 |
*/ |
| 309 |
public static function objectToArray(\stdClass $object) |
| 310 |
{ |
| 311 |
return StdObject::toArray($object); |
| 312 |
} |
| 313 |
|
| 314 |
/** |
| 315 |
* Get an item from an object using "dot" notation. |
| 316 |
* |
| 317 |
* @param object $object |
| 318 |
* @param string|null $key |
| 319 |
* @param mixed $default |
| 320 |
* @return mixed |
| 321 |
*/ |
| 322 |
public static function objectGet($object, $key, $default = null) |
| 323 |
{ |
| 324 |
if (is_null($key) || trim($key) === '') { |
| 325 |
return $object; |
| 326 |
} |
| 327 |
|
| 328 |
foreach (explode('.', $key) as $segment) { |
| 329 |
if (! is_object($object) || ! isset($object->{$segment})) { |
| 330 |
return static::value($default); |
| 331 |
} |
| 332 |
|
| 333 |
$object = $object->{$segment}; |
| 334 |
} |
| 335 |
|
| 336 |
return $object; |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Replace a given pattern with each value in the array in sequentially. |
| 341 |
* |
| 342 |
* @param string $pattern |
| 343 |
* @param array $replacements |
| 344 |
* @param string $subject |
| 345 |
* @return string |
| 346 |
*/ |
| 347 |
public static function pregReplaceArray($pattern, array $replacements, $subject) |
| 348 |
{ |
| 349 |
return preg_replace_callback($pattern, function () use (&$replacements) { |
| 350 |
foreach ($replacements as $value) { |
| 351 |
return array_shift($replacements); |
| 352 |
} |
| 353 |
}, $subject); |
| 354 |
} |
| 355 |
} |
| 356 |
|