| 1 |
<?php declare(strict_types=1); /** @noinspection PhpUndefinedClassInspection */ |
| 2 |
|
| 3 |
namespace MyParcelNL\Sdk\src\Support; |
| 4 |
|
| 5 |
use Exception; |
| 6 |
|
| 7 |
/** |
| 8 |
* Class Helpers |
| 9 |
* @example https://laravel.com/docs/5.7/helpers |
| 10 |
*/ |
| 11 |
class Helpers { |
| 12 |
/** |
| 13 |
* Assign high numeric IDs to a config item to force appending. |
| 14 |
* |
| 15 |
* @param array $array |
| 16 |
* @return array |
| 17 |
*/ |
| 18 |
public function append_config(array $array) |
| 19 |
{ |
| 20 |
$start = 9999; |
| 21 |
|
| 22 |
foreach ($array as $key => $value) { |
| 23 |
if (is_numeric($key)) { |
| 24 |
$start++; |
| 25 |
|
| 26 |
$array[$start] = Arr::pull($array, $key); |
| 27 |
} |
| 28 |
} |
| 29 |
|
| 30 |
return $array; |
| 31 |
} |
| 32 |
|
| 33 |
/** |
| 34 |
* Add an element to an array using "dot" notation if it doesn't exist. |
| 35 |
* |
| 36 |
* @param array $array |
| 37 |
* @param string $key |
| 38 |
* @param mixed $value |
| 39 |
* @return array |
| 40 |
*/ |
| 41 |
public function array_add($array, $key, $value) |
| 42 |
{ |
| 43 |
return Arr::add($array, $key, $value); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Collapse an array of arrays into a single array. |
| 48 |
* |
| 49 |
* @param array $array |
| 50 |
* @return array |
| 51 |
*/ |
| 52 |
public function array_collapse($array) |
| 53 |
{ |
| 54 |
return Arr::collapse($array); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Divide an array into two arrays. One with keys and the other with values. |
| 59 |
* |
| 60 |
* @param array $array |
| 61 |
* @return array |
| 62 |
*/ |
| 63 |
public function array_divide($array) |
| 64 |
{ |
| 65 |
return Arr::divide($array); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Flatten a multi-dimensional associative array with dots. |
| 70 |
* |
| 71 |
* @param array $array |
| 72 |
* @param string $prepend |
| 73 |
* @return array |
| 74 |
*/ |
| 75 |
public function array_dot($array, $prepend = '') |
| 76 |
{ |
| 77 |
return Arr::dot($array, $prepend); |
| 78 |
} |
| 79 |
|
| 80 |
/** |
| 81 |
* Get all of the given array except for a specified array of keys. |
| 82 |
* |
| 83 |
* @param array $array |
| 84 |
* @param array|string $keys |
| 85 |
* @return array |
| 86 |
*/ |
| 87 |
public function array_except($array, $keys) |
| 88 |
{ |
| 89 |
return Arr::except($array, $keys); |
| 90 |
} |
| 91 |
|
| 92 |
/** |
| 93 |
* Return the first element in an array passing a given truth test. |
| 94 |
* |
| 95 |
* @param array $array |
| 96 |
* @param callable|null $callback |
| 97 |
* @param mixed $default |
| 98 |
* @return mixed |
| 99 |
*/ |
| 100 |
public function array_first($array, callable $callback = null, $default = null) |
| 101 |
{ |
| 102 |
return Arr::first($array, $callback, $default); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Flatten a multi-dimensional array into a single level. |
| 107 |
* |
| 108 |
* @param array $array |
| 109 |
* @param int $depth |
| 110 |
* @return array |
| 111 |
*/ |
| 112 |
public function array_flatten($array, $depth = INF) |
| 113 |
{ |
| 114 |
return Arr::flatten($array, $depth); |
| 115 |
} |
| 116 |
|
| 117 |
/** |
| 118 |
* Remove one or many array items from a given array using "dot" notation. |
| 119 |
* |
| 120 |
* @deprecated Not implemented |
| 121 |
* @return void |
| 122 |
* @throws Exception |
| 123 |
*/ |
| 124 |
public function array_forget() |
| 125 |
{ |
| 126 |
throw new Exception('Not implemented'); |
| 127 |
} |
| 128 |
|
| 129 |
/** |
| 130 |
* Get an item from an array using "dot" notation. |
| 131 |
* |
| 132 |
* @param \ArrayAccess|array $array |
| 133 |
* @param string $key |
| 134 |
* @param mixed $default |
| 135 |
* @return mixed |
| 136 |
*/ |
| 137 |
public function array_get($array, $key, $default = null) |
| 138 |
{ |
| 139 |
return Arr::get($array, $key, $default); |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* Check if an item or items exist in an array using "dot" notation. |
| 144 |
* |
| 145 |
* @param \ArrayAccess|array $array |
| 146 |
* @param string|array $keys |
| 147 |
* @return bool |
| 148 |
*/ |
| 149 |
public function array_has($array, $keys) |
| 150 |
{ |
| 151 |
return Arr::has($array, $keys); |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Return the last element in an array passing a given truth test. |
| 156 |
* |
| 157 |
* @param array $array |
| 158 |
* @param callable|null $callback |
| 159 |
* @param mixed $default |
| 160 |
* @return mixed |
| 161 |
*/ |
| 162 |
public function array_last($array, callable $callback = null, $default = null) |
| 163 |
{ |
| 164 |
return Arr::last($array, $callback, $default); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Get a subset of the items from the given array. |
| 169 |
* |
| 170 |
* @param array $array |
| 171 |
* @param array|string $keys |
| 172 |
* @return array |
| 173 |
*/ |
| 174 |
public function array_only($array, $keys) |
| 175 |
{ |
| 176 |
return Arr::only($array, $keys); |
| 177 |
} |
| 178 |
|
| 179 |
/** |
| 180 |
* Pluck an array of values from an array. |
| 181 |
* |
| 182 |
* @param array $array |
| 183 |
* @param string|array $value |
| 184 |
* @param string|array|null $key |
| 185 |
* @return array |
| 186 |
*/ |
| 187 |
public function array_pluck($array, $value, $key = null) |
| 188 |
{ |
| 189 |
return Arr::pluck($array, $value, $key); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* Push an item onto the beginning of an array. |
| 194 |
* |
| 195 |
* @param array $array |
| 196 |
* @param mixed $value |
| 197 |
* @param mixed $key |
| 198 |
* @return array |
| 199 |
*/ |
| 200 |
public function array_prepend($array, $value, $key = null) |
| 201 |
{ |
| 202 |
return Arr::prepend($array, $value, $key); |
| 203 |
} |
| 204 |
|
| 205 |
/** |
| 206 |
* Get a value from the array, and remove it. |
| 207 |
* |
| 208 |
* @param array $array |
| 209 |
* @param string $key |
| 210 |
* @param mixed $default |
| 211 |
* @return mixed |
| 212 |
*/ |
| 213 |
public function array_pull(&$array, $key, $default = null) |
| 214 |
{ |
| 215 |
return Arr::pull($array, $key, $default); |
| 216 |
} |
| 217 |
|
| 218 |
/** |
| 219 |
* Get a random value from an array. |
| 220 |
* |
| 221 |
* @param array $array |
| 222 |
* @param int|null $num |
| 223 |
* @return mixed |
| 224 |
*/ |
| 225 |
public function array_random($array, $num = null) |
| 226 |
{ |
| 227 |
return Arr::random($array, $num); |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* Set an array item to a given value using "dot" notation. |
| 232 |
* |
| 233 |
* If no key is given to the method, the entire array will be replaced. |
| 234 |
* |
| 235 |
* @param array $array |
| 236 |
* @param string $key |
| 237 |
* @param mixed $value |
| 238 |
* @return array |
| 239 |
*/ |
| 240 |
public function array_set(&$array, $key, $value) |
| 241 |
{ |
| 242 |
return Arr::set($array, $key, $value); |
| 243 |
} |
| 244 |
|
| 245 |
/** |
| 246 |
* Sort the array by the given callback or attribute name. |
| 247 |
* |
| 248 |
* @param array $array |
| 249 |
* @param callable|string|null $callback |
| 250 |
* @return array |
| 251 |
*/ |
| 252 |
public function array_sort($array, $callback = null) |
| 253 |
{ |
| 254 |
return Arr::sort($array, $callback); |
| 255 |
} |
| 256 |
|
| 257 |
/** |
| 258 |
* Recursively sort an array by keys and values. |
| 259 |
* |
| 260 |
* @param array $array |
| 261 |
* @return array |
| 262 |
*/ |
| 263 |
public function array_sort_recursive($array) |
| 264 |
{ |
| 265 |
return Arr::sortRecursive($array); |
| 266 |
} |
| 267 |
|
| 268 |
/** |
| 269 |
* Filter the array using the given callback. |
| 270 |
* |
| 271 |
* @param array $array |
| 272 |
* @param callable $callback |
| 273 |
* @return array |
| 274 |
*/ |
| 275 |
public function array_where($array, callable $callback) |
| 276 |
{ |
| 277 |
return Arr::where($array, $callback); |
| 278 |
} |
| 279 |
|
| 280 |
/** |
| 281 |
* If the given value is not an array, wrap it in one. |
| 282 |
* |
| 283 |
* @param mixed $value |
| 284 |
* @return array |
| 285 |
*/ |
| 286 |
public function array_wrap($value) |
| 287 |
{ |
| 288 |
return Arr::wrap($value); |
| 289 |
} |
| 290 |
|
| 291 |
/** |
| 292 |
* Determine if the given value is "blank". |
| 293 |
* |
| 294 |
* @param mixed $value |
| 295 |
* @return bool |
| 296 |
*/ |
| 297 |
public function blank($value) |
| 298 |
{ |
| 299 |
if (is_null($value)) { |
| 300 |
return true; |
| 301 |
} |
| 302 |
|
| 303 |
if (is_string($value)) { |
| 304 |
return trim($value) === ''; |
| 305 |
} |
| 306 |
|
| 307 |
if (is_numeric($value) || is_bool($value)) { |
| 308 |
return false; |
| 309 |
} |
| 310 |
|
| 311 |
if ($value instanceof Countable) { |
| 312 |
return count($value) === 0; |
| 313 |
} |
| 314 |
|
| 315 |
return empty($value); |
| 316 |
} |
| 317 |
|
| 318 |
/** |
| 319 |
* Convert a value to camel case. |
| 320 |
* |
| 321 |
* @param string $value |
| 322 |
* @return string |
| 323 |
*/ |
| 324 |
public function camel_case($value) |
| 325 |
{ |
| 326 |
return Str::camel($value); |
| 327 |
} |
| 328 |
|
| 329 |
/** |
| 330 |
* Get the class "basename" of the given object / class. |
| 331 |
* |
| 332 |
* @param string|object $class |
| 333 |
* @return string |
| 334 |
*/ |
| 335 |
public function class_basename($class) |
| 336 |
{ |
| 337 |
$class = is_object($class) ? get_class($class) : $class; |
| 338 |
|
| 339 |
return basename(str_replace('\\', '/', $class)); |
| 340 |
} |
| 341 |
|
| 342 |
/** |
| 343 |
* Returns all traits used by a class, its parent classes and trait of their traits. |
| 344 |
* |
| 345 |
* @param object|string $class |
| 346 |
* @return array |
| 347 |
*/ |
| 348 |
public function class_uses_recursive($class) |
| 349 |
{ |
| 350 |
if (is_object($class)) { |
| 351 |
$class = get_class($class); |
| 352 |
} |
| 353 |
|
| 354 |
$results = []; |
| 355 |
|
| 356 |
foreach (array_reverse(class_parents($class)) + [$class => $class] as $class) { |
| 357 |
$results += $this->trait_uses_recursive($class); |
| 358 |
} |
| 359 |
|
| 360 |
return array_unique($results); |
| 361 |
} |
| 362 |
|
| 363 |
/** |
| 364 |
* Create a collection from the given value. |
| 365 |
* |
| 366 |
* @param mixed $value |
| 367 |
* @return CollectionProxy |
| 368 |
*/ |
| 369 |
public function collect($value = null) |
| 370 |
{ |
| 371 |
return new CollectionProxy($value); |
| 372 |
} |
| 373 |
|
| 374 |
/** |
| 375 |
* Fill in data where it's missing. |
| 376 |
* |
| 377 |
* @param mixed $target |
| 378 |
* @param string|array $key |
| 379 |
* @param mixed $value |
| 380 |
* @return mixed |
| 381 |
*/ |
| 382 |
public function data_fill(&$target, $key, $value) |
| 383 |
{ |
| 384 |
return $this->data_set($target, $key, $value, false); |
| 385 |
} |
| 386 |
|
| 387 |
/** |
| 388 |
* Get an item from an array or object using "dot" notation. |
| 389 |
* |
| 390 |
* @param mixed $target |
| 391 |
* @param string|array $key |
| 392 |
* @param mixed $default |
| 393 |
* @return mixed |
| 394 |
*/ |
| 395 |
public function data_get($target, $key, $default = null) |
| 396 |
{ |
| 397 |
if (is_null($key)) { |
| 398 |
return $target; |
| 399 |
} |
| 400 |
|
| 401 |
$key = is_array($key) ? $key : explode('.', $key); |
| 402 |
|
| 403 |
while (! is_null($segment = array_shift($key))) { |
| 404 |
if ($segment === '*') { |
| 405 |
if ($target instanceof CollectionProxy) { |
| 406 |
$target = $target->all(); |
| 407 |
} elseif (! is_array($target)) { |
| 408 |
return $this->value($default); |
| 409 |
} |
| 410 |
|
| 411 |
$result = Arr::pluck($target, $key); |
| 412 |
|
| 413 |
return in_array('*', $key) ? Arr::collapse($result) : $result; |
| 414 |
} |
| 415 |
|
| 416 |
if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
| 417 |
$target = $target[$segment]; |
| 418 |
} elseif (is_object($target) && isset($target->{$segment})) { |
| 419 |
$target = $target->{$segment}; |
| 420 |
} else { |
| 421 |
return $this->value($default); |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
return $target; |
| 426 |
} |
| 427 |
|
| 428 |
/** |
| 429 |
* Set an item on an array or object using dot notation. |
| 430 |
* |
| 431 |
* @param mixed $target |
| 432 |
* @param string|array $key |
| 433 |
* @param mixed $value |
| 434 |
* @param bool $overwrite |
| 435 |
* @return mixed |
| 436 |
*/ |
| 437 |
public function data_set(&$target, $key, $value, $overwrite = true) |
| 438 |
{ |
| 439 |
$segments = is_array($key) ? $key : explode('.', $key); |
| 440 |
|
| 441 |
if (($segment = array_shift($segments)) === '*') { |
| 442 |
if (! Arr::accessible($target)) { |
| 443 |
$target = []; |
| 444 |
} |
| 445 |
|
| 446 |
if ($segments) { |
| 447 |
foreach ($target as &$inner) { |
| 448 |
$this->data_set($inner, $segments, $value, $overwrite); |
| 449 |
} |
| 450 |
} elseif ($overwrite) { |
| 451 |
foreach ($target as &$inner) { |
| 452 |
$inner = $value; |
| 453 |
} |
| 454 |
} |
| 455 |
} elseif (Arr::accessible($target)) { |
| 456 |
if ($segments) { |
| 457 |
if (! Arr::exists($target, $segment)) { |
| 458 |
$target[$segment] = []; |
| 459 |
} |
| 460 |
|
| 461 |
$this->data_set($target[$segment], $segments, $value, $overwrite); |
| 462 |
} elseif ($overwrite || ! Arr::exists($target, $segment)) { |
| 463 |
$target[$segment] = $value; |
| 464 |
} |
| 465 |
} elseif (is_object($target)) { |
| 466 |
if ($segments) { |
| 467 |
if (! isset($target->{$segment})) { |
| 468 |
$target->{$segment} = []; |
| 469 |
} |
| 470 |
|
| 471 |
$this->data_set($target->{$segment}, $segments, $value, $overwrite); |
| 472 |
} elseif ($overwrite || ! isset($target->{$segment})) { |
| 473 |
$target->{$segment} = $value; |
| 474 |
} |
| 475 |
} else { |
| 476 |
$target = []; |
| 477 |
|
| 478 |
if ($segments) { |
| 479 |
$this->data_set($target[$segment], $segments, $value, $overwrite); |
| 480 |
} elseif ($overwrite) { |
| 481 |
$target[$segment] = $value; |
| 482 |
} |
| 483 |
} |
| 484 |
|
| 485 |
return $target; |
| 486 |
} |
| 487 |
|
| 488 |
/** |
| 489 |
* Dump the passed variables and end the script. |
| 490 |
* |
| 491 |
* @param mixed $args |
| 492 |
* @return void |
| 493 |
*/ |
| 494 |
public function dd(...$args) |
| 495 |
{ |
| 496 |
foreach ($args as $x) { |
| 497 |
var_dump($x); |
| 498 |
} |
| 499 |
|
| 500 |
die(1); |
| 501 |
} |
| 502 |
|
| 503 |
/** |
| 504 |
* Escape HTML special characters in a string. |
| 505 |
* |
| 506 |
* @param string $value |
| 507 |
* @param bool $doubleEncode |
| 508 |
* @return string |
| 509 |
*/ |
| 510 |
public function e($value, $doubleEncode = true) |
| 511 |
{ |
| 512 |
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8', $doubleEncode); |
| 513 |
} |
| 514 |
|
| 515 |
/** |
| 516 |
* Determine if a given string ends with a given substring. |
| 517 |
* |
| 518 |
* @param string $haystack |
| 519 |
* @param string|array $needles |
| 520 |
* @return bool |
| 521 |
*/ |
| 522 |
public function ends_with($haystack, $needles) |
| 523 |
{ |
| 524 |
return Str::endsWith($haystack, $needles); |
| 525 |
} |
| 526 |
|
| 527 |
/** |
| 528 |
* Gets the value of an environment variable. |
| 529 |
* |
| 530 |
* @param string $key |
| 531 |
* @param mixed $default |
| 532 |
* @return mixed |
| 533 |
*/ |
| 534 |
public function env($key, $default = null) |
| 535 |
{ |
| 536 |
$value = getenv($key); |
| 537 |
|
| 538 |
if ($value === false) { |
| 539 |
return $this->value($default); |
| 540 |
} |
| 541 |
|
| 542 |
switch (strtolower($value)) { |
| 543 |
case 'true': |
| 544 |
case '(true)': |
| 545 |
return true; |
| 546 |
case 'false': |
| 547 |
case '(false)': |
| 548 |
return false; |
| 549 |
case 'empty': |
| 550 |
case '(empty)': |
| 551 |
return ''; |
| 552 |
case 'null': |
| 553 |
case '(null)': |
| 554 |
return null; |
| 555 |
} |
| 556 |
|
| 557 |
if (($valueLength = strlen($value)) > 1 && $value[0] === '"' && $value[$valueLength - 1] === '"') { |
| 558 |
return substr($value, 1, -1); |
| 559 |
} |
| 560 |
|
| 561 |
return $value; |
| 562 |
} |
| 563 |
|
| 564 |
/** |
| 565 |
* Determine if a value is "filled". |
| 566 |
* |
| 567 |
* @param mixed $value |
| 568 |
* @return bool |
| 569 |
*/ |
| 570 |
public function filled($value) |
| 571 |
{ |
| 572 |
return ! $this->blank($value); |
| 573 |
} |
| 574 |
|
| 575 |
/** |
| 576 |
* Get the first element of an array. Useful for method chaining. |
| 577 |
* |
| 578 |
* @param array $array |
| 579 |
* @return mixed |
| 580 |
*/ |
| 581 |
public function head($array) |
| 582 |
{ |
| 583 |
return reset($array); |
| 584 |
} |
| 585 |
|
| 586 |
/** |
| 587 |
* Convert a string to kebab case. |
| 588 |
* |
| 589 |
* @param string $value |
| 590 |
* @return string |
| 591 |
*/ |
| 592 |
public function kebab_case($value) |
| 593 |
{ |
| 594 |
return Str::kebab($value); |
| 595 |
} |
| 596 |
|
| 597 |
/** |
| 598 |
* Get the last element from an array. |
| 599 |
* |
| 600 |
* @param array $array |
| 601 |
* @return mixed |
| 602 |
*/ |
| 603 |
public function last($array) |
| 604 |
{ |
| 605 |
return end($array); |
| 606 |
} |
| 607 |
|
| 608 |
/** |
| 609 |
* Get an item from an object using "dot" notation. |
| 610 |
* |
| 611 |
* @param object $object |
| 612 |
* @param string $key |
| 613 |
* @param mixed $default |
| 614 |
* @return mixed |
| 615 |
*/ |
| 616 |
public function object_get($object, $key, $default = null) |
| 617 |
{ |
| 618 |
if (is_null($key) || trim($key) == '') { |
| 619 |
return $object; |
| 620 |
} |
| 621 |
|
| 622 |
foreach (explode('.', $key) as $segment) { |
| 623 |
if (! is_object($object) || ! isset($object->{$segment})) { |
| 624 |
return $this->value($default); |
| 625 |
} |
| 626 |
|
| 627 |
$object = $object->{$segment}; |
| 628 |
} |
| 629 |
|
| 630 |
return $object; |
| 631 |
} |
| 632 |
|
| 633 |
/** |
| 634 |
* Provide access to optional objects. |
| 635 |
* |
| 636 |
* @param mixed $value |
| 637 |
* @param callable|null $callback |
| 638 |
* @return mixed |
| 639 |
*/ |
| 640 |
public function optional($value = null, callable $callback = null) |
| 641 |
{ |
| 642 |
if (is_null($callback)) { |
| 643 |
return new Optional($value); |
| 644 |
} elseif (! is_null($value)) { |
| 645 |
return $callback($value); |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/** |
| 650 |
* Replace a given pattern with each value in the array in sequentially. |
| 651 |
* |
| 652 |
* @param string $pattern |
| 653 |
* @param array $replacements |
| 654 |
* @param string $subject |
| 655 |
* @return string |
| 656 |
*/ |
| 657 |
public function preg_replace_array($pattern, array $replacements, $subject) |
| 658 |
{ |
| 659 |
return preg_replace_callback($pattern, function () use (&$replacements) { |
| 660 |
foreach ($replacements as $key => $value) { |
| 661 |
return array_shift($replacements); |
| 662 |
} |
| 663 |
}, $subject); |
| 664 |
} |
| 665 |
|
| 666 |
/** |
| 667 |
* Retry an operation a given number of times. |
| 668 |
* |
| 669 |
* @param int $times |
| 670 |
* @param callable $callback |
| 671 |
* @param int $sleep |
| 672 |
* @return mixed |
| 673 |
* |
| 674 |
* @throws Exception |
| 675 |
*/ |
| 676 |
public function retry($times, callable $callback, $sleep = 0) |
| 677 |
{ |
| 678 |
$times--; |
| 679 |
|
| 680 |
beginning: |
| 681 |
try { |
| 682 |
return $callback(); |
| 683 |
} catch (Exception $e) { |
| 684 |
if (! $times) { |
| 685 |
throw $e; |
| 686 |
} |
| 687 |
|
| 688 |
$times--; |
| 689 |
|
| 690 |
if ($sleep) { |
| 691 |
usleep($sleep * 1000); |
| 692 |
} |
| 693 |
|
| 694 |
goto beginning; |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
/** |
| 699 |
* Convert a string to snake case. |
| 700 |
* |
| 701 |
* @param string $value |
| 702 |
* @param string $delimiter |
| 703 |
* @return string |
| 704 |
*/ |
| 705 |
public function snake_case($value, $delimiter = '_') |
| 706 |
{ |
| 707 |
return Str::snake($value, $delimiter); |
| 708 |
} |
| 709 |
|
| 710 |
/** |
| 711 |
* Determine if a given string starts with a given substring. |
| 712 |
* |
| 713 |
* @param string $haystack |
| 714 |
* @param string|array $needles |
| 715 |
* @return bool |
| 716 |
*/ |
| 717 |
public function starts_with($haystack, $needles) |
| 718 |
{ |
| 719 |
return Str::startsWith($haystack, $needles); |
| 720 |
} |
| 721 |
|
| 722 |
/** |
| 723 |
* Return the remainder of a string after a given value. |
| 724 |
* |
| 725 |
* @param string $subject |
| 726 |
* @param string $search |
| 727 |
* @return string |
| 728 |
*/ |
| 729 |
public function str_after($subject, $search) |
| 730 |
{ |
| 731 |
return Str::after($subject, $search); |
| 732 |
} |
| 733 |
|
| 734 |
/** |
| 735 |
* Get the portion of a string before a given value. |
| 736 |
* |
| 737 |
* @param string $subject |
| 738 |
* @param string $search |
| 739 |
* @return string |
| 740 |
*/ |
| 741 |
public function str_before($subject, $search) |
| 742 |
{ |
| 743 |
return Str::before($subject, $search); |
| 744 |
} |
| 745 |
|
| 746 |
/** |
| 747 |
* Determine if a given string contains a given substring. |
| 748 |
* |
| 749 |
* @param string $haystack |
| 750 |
* @param string|array $needles |
| 751 |
* @return bool |
| 752 |
*/ |
| 753 |
public function str_contains($haystack, $needles) |
| 754 |
{ |
| 755 |
return Str::contains($haystack, $needles); |
| 756 |
} |
| 757 |
|
| 758 |
/** |
| 759 |
* Cap a string with a single instance of a given value. |
| 760 |
* |
| 761 |
* @param string $value |
| 762 |
* @param string $cap |
| 763 |
* @return string |
| 764 |
*/ |
| 765 |
public function str_finish($value, $cap) |
| 766 |
{ |
| 767 |
return Str::finish($value, $cap); |
| 768 |
} |
| 769 |
|
| 770 |
/** |
| 771 |
* Determine if a given string matches a given pattern. |
| 772 |
* |
| 773 |
* @param string|array $pattern |
| 774 |
* @param string $value |
| 775 |
* @return bool |
| 776 |
*/ |
| 777 |
public function str_is($pattern, $value) |
| 778 |
{ |
| 779 |
return Str::is($pattern, $value); |
| 780 |
} |
| 781 |
|
| 782 |
/** |
| 783 |
* Limit the number of characters in a string. |
| 784 |
* |
| 785 |
* @param string $value |
| 786 |
* @param int $limit |
| 787 |
* @param string $end |
| 788 |
* @return string |
| 789 |
*/ |
| 790 |
public function str_limit($value, $limit = 100, $end = '...') |
| 791 |
{ |
| 792 |
return Str::limit($value, $limit, $end); |
| 793 |
} |
| 794 |
|
| 795 |
/** |
| 796 |
* Get the plural form of an English word. |
| 797 |
* |
| 798 |
* @deprecated Not implemented |
| 799 |
* @return string |
| 800 |
* @throws Exception |
| 801 |
*/ |
| 802 |
public function str_plural() |
| 803 |
{ |
| 804 |
throw new Exception('Not implemented'); |
| 805 |
} |
| 806 |
|
| 807 |
/** |
| 808 |
* Generate a more truly "random" alpha-numeric string. |
| 809 |
* |
| 810 |
* @param int $length |
| 811 |
* @return string |
| 812 |
* |
| 813 |
* @throws \RuntimeException |
| 814 |
* @throws Exception |
| 815 |
*/ |
| 816 |
public function str_random($length = 16) |
| 817 |
{ |
| 818 |
return Str::random($length); |
| 819 |
} |
| 820 |
|
| 821 |
/** |
| 822 |
* Replace a given value in the string sequentially with an array. |
| 823 |
* |
| 824 |
* @param string $search |
| 825 |
* @param array $replace |
| 826 |
* @param string $subject |
| 827 |
* @return string |
| 828 |
*/ |
| 829 |
public function str_replace_array($search, array $replace, $subject) |
| 830 |
{ |
| 831 |
return Str::replaceArray($search, $replace, $subject); |
| 832 |
} |
| 833 |
|
| 834 |
/** |
| 835 |
* Replace the first occurrence of a given value in the string. |
| 836 |
* |
| 837 |
* @param string $search |
| 838 |
* @param string $replace |
| 839 |
* @param string $subject |
| 840 |
* @return string |
| 841 |
*/ |
| 842 |
public function str_replace_first($search, $replace, $subject) |
| 843 |
{ |
| 844 |
return Str::replaceFirst($search, $replace, $subject); |
| 845 |
} |
| 846 |
|
| 847 |
/** |
| 848 |
* Replace the last occurrence of a given value in the string. |
| 849 |
* |
| 850 |
* @param string $search |
| 851 |
* @param string $replace |
| 852 |
* @param string $subject |
| 853 |
* @return string |
| 854 |
*/ |
| 855 |
public function str_replace_last($search, $replace, $subject) |
| 856 |
{ |
| 857 |
return Str::replaceLast($search, $replace, $subject); |
| 858 |
} |
| 859 |
|
| 860 |
/** |
| 861 |
* Get the singular form of an English word. |
| 862 |
* |
| 863 |
* @deprecated Not implemented |
| 864 |
* @return string |
| 865 |
* @throws Exception |
| 866 |
*/ |
| 867 |
public function str_singular() |
| 868 |
{ |
| 869 |
throw new Exception('Not implemented'); |
| 870 |
} |
| 871 |
|
| 872 |
/** |
| 873 |
* Generate a URL friendly "slug" from a given string. |
| 874 |
* |
| 875 |
* @param string $title |
| 876 |
* @param string $separator |
| 877 |
* @param string $language |
| 878 |
* @return string |
| 879 |
*/ |
| 880 |
public function str_slug($title, $separator = '-', $language = 'en') |
| 881 |
{ |
| 882 |
return Str::slug($title, $separator, $language); |
| 883 |
} |
| 884 |
|
| 885 |
/** |
| 886 |
* Begin a string with a single instance of a given value. |
| 887 |
* |
| 888 |
* @param string $value |
| 889 |
* @param string $prefix |
| 890 |
* @return string |
| 891 |
*/ |
| 892 |
public function str_start($value, $prefix) |
| 893 |
{ |
| 894 |
return Str::start($value, $prefix); |
| 895 |
} |
| 896 |
|
| 897 |
/** |
| 898 |
* Convert a value to studly caps case. |
| 899 |
* |
| 900 |
* @param string $value |
| 901 |
* @return string |
| 902 |
*/ |
| 903 |
public function studly_case($value) |
| 904 |
{ |
| 905 |
return Str::studly($value); |
| 906 |
} |
| 907 |
|
| 908 |
/** |
| 909 |
* Call the given Closure with the given value then return the value. |
| 910 |
* |
| 911 |
* @param mixed $value |
| 912 |
* @param callable|null $callback |
| 913 |
* @return mixed |
| 914 |
*/ |
| 915 |
public function tap($value, $callback = null) |
| 916 |
{ |
| 917 |
if (is_null($callback)) { |
| 918 |
return new HigherOrderTapProxy($value); |
| 919 |
} |
| 920 |
|
| 921 |
$callback($value); |
| 922 |
|
| 923 |
return $value; |
| 924 |
} |
| 925 |
|
| 926 |
/** |
| 927 |
* Throw the given exception if the given condition is true. |
| 928 |
* |
| 929 |
* @param mixed $condition |
| 930 |
* @param \Throwable|string $exception |
| 931 |
* @param array ...$parameters |
| 932 |
* @return mixed |
| 933 |
* @throws \Throwable |
| 934 |
*/ |
| 935 |
public function throw_if($condition, $exception, ...$parameters) |
| 936 |
{ |
| 937 |
if ($condition) { |
| 938 |
throw (is_string($exception) ? new $exception(...$parameters) : $exception); |
| 939 |
} |
| 940 |
|
| 941 |
return $condition; |
| 942 |
} |
| 943 |
|
| 944 |
/** |
| 945 |
* Throw the given exception unless the given condition is true. |
| 946 |
* |
| 947 |
* @param mixed $condition |
| 948 |
* @param \Throwable|string $exception |
| 949 |
* @param array ...$parameters |
| 950 |
* @return mixed |
| 951 |
* @throws \Throwable |
| 952 |
*/ |
| 953 |
public function throw_unless($condition, $exception, ...$parameters) |
| 954 |
{ |
| 955 |
if (! $condition) { |
| 956 |
throw (is_string($exception) ? new $exception(...$parameters) : $exception); |
| 957 |
} |
| 958 |
|
| 959 |
return $condition; |
| 960 |
} |
| 961 |
|
| 962 |
/** |
| 963 |
* Convert a value to title case. |
| 964 |
* |
| 965 |
* @param string $value |
| 966 |
* @return string |
| 967 |
*/ |
| 968 |
public function title_case($value) |
| 969 |
{ |
| 970 |
return Str::title($value); |
| 971 |
} |
| 972 |
|
| 973 |
/** |
| 974 |
* Returns all traits used by a trait and its traits. |
| 975 |
* |
| 976 |
* @param string $trait |
| 977 |
* @return array |
| 978 |
*/ |
| 979 |
public function trait_uses_recursive($trait) |
| 980 |
{ |
| 981 |
$traits = class_uses($trait); |
| 982 |
|
| 983 |
foreach ($traits as $trait) { |
| 984 |
$traits += $this->trait_uses_recursive($trait); |
| 985 |
} |
| 986 |
|
| 987 |
return $traits; |
| 988 |
} |
| 989 |
|
| 990 |
/** |
| 991 |
* Transform the given value if it is present. |
| 992 |
* |
| 993 |
* @param mixed $value |
| 994 |
* @param callable $callback |
| 995 |
* @param mixed $default |
| 996 |
* @return mixed|null |
| 997 |
*/ |
| 998 |
public function transform($value, callable $callback, $default = null) |
| 999 |
{ |
| 1000 |
if ($this->filled($value)) { |
| 1001 |
return $callback($value); |
| 1002 |
} |
| 1003 |
|
| 1004 |
if (is_callable($default)) { |
| 1005 |
return $default($value); |
| 1006 |
} |
| 1007 |
|
| 1008 |
return $default; |
| 1009 |
} |
| 1010 |
|
| 1011 |
/** |
| 1012 |
* Return the default value of the given value. |
| 1013 |
* |
| 1014 |
* @param mixed $value |
| 1015 |
* @return mixed |
| 1016 |
*/ |
| 1017 |
public function value($value) |
| 1018 |
{ |
| 1019 |
return $value instanceof Closure ? $value() : $value; |
| 1020 |
} |
| 1021 |
|
| 1022 |
/** |
| 1023 |
* Determine whether the current environment is Windows based. |
| 1024 |
* |
| 1025 |
* @return bool |
| 1026 |
*/ |
| 1027 |
public function windows_os() |
| 1028 |
{ |
| 1029 |
return strtolower(substr(PHP_OS, 0, 3)) === 'win'; |
| 1030 |
} |
| 1031 |
|
| 1032 |
/** |
| 1033 |
* Return the given value, optionally passed through the given callback. |
| 1034 |
* |
| 1035 |
* @param mixed $value |
| 1036 |
* @param callable|null $callback |
| 1037 |
* @return mixed |
| 1038 |
*/ |
| 1039 |
public function with($value, callable $callback = null) |
| 1040 |
{ |
| 1041 |
return is_null($callback) ? $value : $callback($value); |
| 1042 |
} |
| 1043 |
} |
| 1044 |
|