Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 1 | <?php |
| 2 | |
| 3 | use Tightenco\Collect\Support\Arr; |
| 4 | use Tightenco\Collect\Support\Collection; |
| 5 | use Tightenco\Collect\Support\HigherOrderTapProxy; |
| 6 | use Symfony\Component\VarDumper\VarDumper; |
| 7 | |
| 8 | if (! class_exists(Illuminate\Support\Collection::class)) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 9 | if (! function_exists('collect')) { |
| 10 | /** |
| 11 | * Create a collection from the given value. |
| 12 | * |
| 13 | * @param mixed $value |
| 14 | * @return \Tightenco\Collect\Support\Collection |
| 15 | */ |
| 16 | function collect($value = null) |
| 17 | { |
| 18 | return new Collection($value); |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | if (! function_exists('value')) { |
| 23 | /** |
| 24 | * Return the default value of the given value. |
| 25 | * |
| 26 | * @param mixed $value |
| 27 | * @return mixed |
| 28 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 29 | function value($value, ...$args) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 30 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 31 | return $value instanceof Closure ? $value(...$args) : $value; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 32 | } |
| 33 | } |
| 34 | |
| 35 | if (! function_exists('data_get')) { |
| 36 | /** |
| 37 | * Get an item from an array or object using "dot" notation. |
| 38 | * |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 39 | * @param mixed $target |
| 40 | * @param string|array|int|null $key |
| 41 | * @param mixed $default |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 42 | * @return mixed |
| 43 | */ |
| 44 | function data_get($target, $key, $default = null) |
| 45 | { |
| 46 | if (is_null($key)) { |
| 47 | return $target; |
| 48 | } |
| 49 | |
| 50 | $key = is_array($key) ? $key : explode('.', $key); |
| 51 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 52 | foreach ($key as $i => $segment) { |
| 53 | unset($key[$i]); |
| 54 | |
| 55 | if (is_null($segment)) { |
| 56 | return $target; |
| 57 | } |
| 58 | |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 59 | if ($segment === '*') { |
| 60 | if ($target instanceof Collection) { |
| 61 | $target = $target->all(); |
| 62 | } elseif (! is_array($target)) { |
| 63 | return value($default); |
| 64 | } |
| 65 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 66 | $result = []; |
| 67 | |
| 68 | foreach ($target as $item) { |
| 69 | $result[] = data_get($item, $key); |
| 70 | } |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 71 | |
| 72 | return in_array('*', $key) ? Arr::collapse($result) : $result; |
| 73 | } |
| 74 | |
| 75 | if (Arr::accessible($target) && Arr::exists($target, $segment)) { |
| 76 | $target = $target[$segment]; |
| 77 | } elseif (is_object($target) && isset($target->{$segment})) { |
| 78 | $target = $target->{$segment}; |
| 79 | } else { |
| 80 | return value($default); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | return $target; |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | if (! function_exists('tap')) { |
| 89 | /** |
| 90 | * Call the given Closure with the given value then return the value. |
| 91 | * |
| 92 | * @param mixed $value |
| 93 | * @param callable|null $callback |
| 94 | * @return mixed |
| 95 | */ |
| 96 | function tap($value, $callback = null) |
| 97 | { |
| 98 | if (is_null($callback)) { |
| 99 | return new HigherOrderTapProxy($value); |
| 100 | } |
| 101 | |
| 102 | $callback($value); |
| 103 | |
| 104 | return $value; |
| 105 | } |
| 106 | } |
| 107 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 108 | if (! function_exists('class_basename')) { |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 109 | /** |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 110 | * Get the class "basename" of the given object / class. |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 111 | * |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 112 | * @param string|object $class |
| 113 | * @return string |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 114 | */ |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 115 | function class_basename($class) |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 116 | { |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 117 | $class = is_object($class) ? get_class($class) : $class; |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 118 | |
Matthias Andreas Benkard | 1ba5381 | 2022-12-27 17:32:58 +0100 | [diff] [blame^] | 119 | return basename(str_replace('\\', '/', $class)); |
Matthias Andreas Benkard | 7b2a3a1 | 2021-08-16 10:57:25 +0200 | [diff] [blame] | 120 | } |
| 121 | } |
| 122 | } |