helpers.php
| 1 | <?php |
| 2 | |
| 3 | namespace IAWPSCOPED; |
| 4 | |
| 5 | use IAWPSCOPED\Illuminate\Contracts\Support\DeferringDisplayableValue; |
| 6 | use IAWPSCOPED\Illuminate\Contracts\Support\Htmlable; |
| 7 | use IAWPSCOPED\Illuminate\Support\Arr; |
| 8 | use IAWPSCOPED\Illuminate\Support\Env; |
| 9 | use IAWPSCOPED\Illuminate\Support\HigherOrderTapProxy; |
| 10 | use IAWPSCOPED\Illuminate\Support\Optional; |
| 11 | if (!\function_exists('IAWPSCOPED\\append_config')) { |
| 12 | /** |
| 13 | * Assign high numeric IDs to a config item to force appending. |
| 14 | * |
| 15 | * @param array $array |
| 16 | * @return array |
| 17 | * @internal |
| 18 | */ |
| 19 | function append_config(array $array) |
| 20 | { |
| 21 | $start = 9999; |
| 22 | foreach ($array as $key => $value) { |
| 23 | if (\is_numeric($key)) { |
| 24 | $start++; |
| 25 | $array[$start] = Arr::pull($array, $key); |
| 26 | } |
| 27 | } |
| 28 | return $array; |
| 29 | } |
| 30 | } |
| 31 | if (!\function_exists('IAWPSCOPED\\blank')) { |
| 32 | /** |
| 33 | * Determine if the given value is "blank". |
| 34 | * |
| 35 | * @param mixed $value |
| 36 | * @return bool |
| 37 | * @internal |
| 38 | */ |
| 39 | function blank($value) |
| 40 | { |
| 41 | if (\is_null($value)) { |
| 42 | return \true; |
| 43 | } |
| 44 | if (\is_string($value)) { |
| 45 | return \trim($value) === ''; |
| 46 | } |
| 47 | if (\is_numeric($value) || \is_bool($value)) { |
| 48 | return \false; |
| 49 | } |
| 50 | if ($value instanceof \Countable) { |
| 51 | return \count($value) === 0; |
| 52 | } |
| 53 | return empty($value); |
| 54 | } |
| 55 | } |
| 56 | if (!\function_exists('IAWPSCOPED\\class_basename')) { |
| 57 | /** |
| 58 | * Get the class "basename" of the given object / class. |
| 59 | * |
| 60 | * @param string|object $class |
| 61 | * @return string |
| 62 | * @internal |
| 63 | */ |
| 64 | function class_basename($class) |
| 65 | { |
| 66 | $class = \is_object($class) ? \get_class($class) : $class; |
| 67 | return \basename(\str_replace('\\', '/', $class)); |
| 68 | } |
| 69 | } |
| 70 | if (!\function_exists('IAWPSCOPED\\class_uses_recursive')) { |
| 71 | /** |
| 72 | * Returns all traits used by a class, its parent classes and trait of their traits. |
| 73 | * |
| 74 | * @param object|string $class |
| 75 | * @return array |
| 76 | * @internal |
| 77 | */ |
| 78 | function class_uses_recursive($class) |
| 79 | { |
| 80 | if (\is_object($class)) { |
| 81 | $class = \get_class($class); |
| 82 | } |
| 83 | $results = []; |
| 84 | foreach (\array_reverse(\class_parents($class)) + [$class => $class] as $class) { |
| 85 | $results += trait_uses_recursive($class); |
| 86 | } |
| 87 | return \array_unique($results); |
| 88 | } |
| 89 | } |
| 90 | if (!\function_exists('IAWPSCOPED\\e')) { |
| 91 | /** |
| 92 | * Encode HTML special characters in a string. |
| 93 | * |
| 94 | * @param \Illuminate\Contracts\Support\DeferringDisplayableValue|\Illuminate\Contracts\Support\Htmlable|string|null $value |
| 95 | * @param bool $doubleEncode |
| 96 | * @return string |
| 97 | * @internal |
| 98 | */ |
| 99 | function e($value, $doubleEncode = \true) |
| 100 | { |
| 101 | if ($value instanceof DeferringDisplayableValue) { |
| 102 | $value = $value->resolveDisplayableValue(); |
| 103 | } |
| 104 | if ($value instanceof Htmlable) { |
| 105 | return $value->toHtml(); |
| 106 | } |
| 107 | return \htmlspecialchars($value ?? '', \ENT_QUOTES, 'UTF-8', $doubleEncode); |
| 108 | } |
| 109 | } |
| 110 | if (!\function_exists('IAWPSCOPED\\env')) { |
| 111 | /** |
| 112 | * Gets the value of an environment variable. |
| 113 | * |
| 114 | * @param string $key |
| 115 | * @param mixed $default |
| 116 | * @return mixed |
| 117 | * @internal |
| 118 | */ |
| 119 | function env($key, $default = null) |
| 120 | { |
| 121 | return Env::get($key, $default); |
| 122 | } |
| 123 | } |
| 124 | if (!\function_exists('IAWPSCOPED\\filled')) { |
| 125 | /** |
| 126 | * Determine if a value is "filled". |
| 127 | * |
| 128 | * @param mixed $value |
| 129 | * @return bool |
| 130 | * @internal |
| 131 | */ |
| 132 | function filled($value) |
| 133 | { |
| 134 | return !blank($value); |
| 135 | } |
| 136 | } |
| 137 | if (!\function_exists('IAWPSCOPED\\object_get')) { |
| 138 | /** |
| 139 | * Get an item from an object using "dot" notation. |
| 140 | * |
| 141 | * @param object $object |
| 142 | * @param string|null $key |
| 143 | * @param mixed $default |
| 144 | * @return mixed |
| 145 | * @internal |
| 146 | */ |
| 147 | function object_get($object, $key, $default = null) |
| 148 | { |
| 149 | if (\is_null($key) || \trim($key) === '') { |
| 150 | return $object; |
| 151 | } |
| 152 | foreach (\explode('.', $key) as $segment) { |
| 153 | if (!\is_object($object) || !isset($object->{$segment})) { |
| 154 | return \IAWPSCOPED\value($default); |
| 155 | } |
| 156 | $object = $object->{$segment}; |
| 157 | } |
| 158 | return $object; |
| 159 | } |
| 160 | } |
| 161 | if (!\function_exists('IAWPSCOPED\\optional')) { |
| 162 | /** |
| 163 | * Provide access to optional objects. |
| 164 | * |
| 165 | * @param mixed $value |
| 166 | * @param callable|null $callback |
| 167 | * @return mixed |
| 168 | * @internal |
| 169 | */ |
| 170 | function optional($value = null, callable $callback = null) |
| 171 | { |
| 172 | if (\is_null($callback)) { |
| 173 | return new Optional($value); |
| 174 | } elseif (!\is_null($value)) { |
| 175 | return $callback($value); |
| 176 | } |
| 177 | } |
| 178 | } |
| 179 | if (!\function_exists('IAWPSCOPED\\preg_replace_array')) { |
| 180 | /** |
| 181 | * Replace a given pattern with each value in the array in sequentially. |
| 182 | * |
| 183 | * @param string $pattern |
| 184 | * @param array $replacements |
| 185 | * @param string $subject |
| 186 | * @return string |
| 187 | * @internal |
| 188 | */ |
| 189 | function preg_replace_array($pattern, array $replacements, $subject) |
| 190 | { |
| 191 | return \preg_replace_callback($pattern, function () use(&$replacements) { |
| 192 | foreach ($replacements as $key => $value) { |
| 193 | return \array_shift($replacements); |
| 194 | } |
| 195 | }, $subject); |
| 196 | } |
| 197 | } |
| 198 | if (!\function_exists('IAWPSCOPED\\retry')) { |
| 199 | /** |
| 200 | * Retry an operation a given number of times. |
| 201 | * |
| 202 | * @param int $times |
| 203 | * @param callable $callback |
| 204 | * @param int|\Closure $sleepMilliseconds |
| 205 | * @param callable|null $when |
| 206 | * @return mixed |
| 207 | * |
| 208 | * @throws \Exception |
| 209 | * @internal |
| 210 | */ |
| 211 | function retry($times, callable $callback, $sleepMilliseconds = 0, $when = null) |
| 212 | { |
| 213 | $attempts = 0; |
| 214 | beginning: |
| 215 | $attempts++; |
| 216 | $times--; |
| 217 | try { |
| 218 | return $callback($attempts); |
| 219 | } catch (\Exception $e) { |
| 220 | if ($times < 1 || $when && !$when($e)) { |
| 221 | throw $e; |
| 222 | } |
| 223 | if ($sleepMilliseconds) { |
| 224 | \usleep(\IAWPSCOPED\value($sleepMilliseconds, $attempts) * 1000); |
| 225 | } |
| 226 | goto beginning; |
| 227 | } |
| 228 | } |
| 229 | } |
| 230 | if (!\function_exists('IAWPSCOPED\\tap')) { |
| 231 | /** |
| 232 | * Call the given Closure with the given value then return the value. |
| 233 | * |
| 234 | * @param mixed $value |
| 235 | * @param callable|null $callback |
| 236 | * @return mixed |
| 237 | * @internal |
| 238 | */ |
| 239 | function tap($value, $callback = null) |
| 240 | { |
| 241 | if (\is_null($callback)) { |
| 242 | return new HigherOrderTapProxy($value); |
| 243 | } |
| 244 | $callback($value); |
| 245 | return $value; |
| 246 | } |
| 247 | } |
| 248 | if (!\function_exists('IAWPSCOPED\\throw_if')) { |
| 249 | /** |
| 250 | * Throw the given exception if the given condition is true. |
| 251 | * |
| 252 | * @param mixed $condition |
| 253 | * @param \Throwable|string $exception |
| 254 | * @param mixed ...$parameters |
| 255 | * @return mixed |
| 256 | * |
| 257 | * @throws \Throwable |
| 258 | * @internal |
| 259 | */ |
| 260 | function throw_if($condition, $exception = 'RuntimeException', ...$parameters) |
| 261 | { |
| 262 | if ($condition) { |
| 263 | if (\is_string($exception) && \class_exists($exception)) { |
| 264 | $exception = new $exception(...$parameters); |
| 265 | } |
| 266 | throw \is_string($exception) ? new \RuntimeException($exception) : $exception; |
| 267 | } |
| 268 | return $condition; |
| 269 | } |
| 270 | } |
| 271 | if (!\function_exists('IAWPSCOPED\\throw_unless')) { |
| 272 | /** |
| 273 | * Throw the given exception unless the given condition is true. |
| 274 | * |
| 275 | * @param mixed $condition |
| 276 | * @param \Throwable|string $exception |
| 277 | * @param mixed ...$parameters |
| 278 | * @return mixed |
| 279 | * |
| 280 | * @throws \Throwable |
| 281 | * @internal |
| 282 | */ |
| 283 | function throw_unless($condition, $exception = 'RuntimeException', ...$parameters) |
| 284 | { |
| 285 | throw_if(!$condition, $exception, ...$parameters); |
| 286 | return $condition; |
| 287 | } |
| 288 | } |
| 289 | if (!\function_exists('IAWPSCOPED\\trait_uses_recursive')) { |
| 290 | /** |
| 291 | * Returns all traits used by a trait and its traits. |
| 292 | * |
| 293 | * @param string $trait |
| 294 | * @return array |
| 295 | * @internal |
| 296 | */ |
| 297 | function trait_uses_recursive($trait) |
| 298 | { |
| 299 | $traits = \class_uses($trait) ?: []; |
| 300 | foreach ($traits as $trait) { |
| 301 | $traits += trait_uses_recursive($trait); |
| 302 | } |
| 303 | return $traits; |
| 304 | } |
| 305 | } |
| 306 | if (!\function_exists('IAWPSCOPED\\transform')) { |
| 307 | /** |
| 308 | * Transform the given value if it is present. |
| 309 | * |
| 310 | * @param mixed $value |
| 311 | * @param callable $callback |
| 312 | * @param mixed $default |
| 313 | * @return mixed|null |
| 314 | * @internal |
| 315 | */ |
| 316 | function transform($value, callable $callback, $default = null) |
| 317 | { |
| 318 | if (filled($value)) { |
| 319 | return $callback($value); |
| 320 | } |
| 321 | if (\is_callable($default)) { |
| 322 | return $default($value); |
| 323 | } |
| 324 | return $default; |
| 325 | } |
| 326 | } |
| 327 | if (!\function_exists('IAWPSCOPED\\windows_os')) { |
| 328 | /** |
| 329 | * Determine whether the current environment is Windows based. |
| 330 | * |
| 331 | * @return bool |
| 332 | * @internal |
| 333 | */ |
| 334 | function windows_os() |
| 335 | { |
| 336 | return \PHP_OS_FAMILY === 'Windows'; |
| 337 | } |
| 338 | } |
| 339 | if (!\function_exists('IAWPSCOPED\\with')) { |
| 340 | /** |
| 341 | * Return the given value, optionally passed through the given callback. |
| 342 | * |
| 343 | * @param mixed $value |
| 344 | * @param callable|null $callback |
| 345 | * @return mixed |
| 346 | * @internal |
| 347 | */ |
| 348 | function with($value, callable $callback = null) |
| 349 | { |
| 350 | return \is_null($callback) ? $value : $callback($value); |
| 351 | } |
| 352 | } |
| 353 |