|
| 1 | +function ($base) { |
| 2 | + $arrayPrototype = function ($base, $key) { |
| 3 | + if ($key === 'forEach') { |
| 4 | + return return function ($callback, $userData = null) use (&$base) { |
| 5 | + return array_walk($base, $callback, $userData); |
| 6 | + }; |
| 7 | + } |
| 8 | + if ($key === 'map') { |
| 9 | + return return function ($callback) use (&$base) { |
| 10 | + return array_map($callback, $base); |
| 11 | + }; |
| 12 | + } |
| 13 | + if ($key === 'filter') { |
| 14 | + return return function ($callback, $flag = 0) use ($base) { |
| 15 | + return array_filter($base, $callback, $flag); |
| 16 | + }; |
| 17 | + } |
| 18 | + if ($key === 'pop') { |
| 19 | + return return function () use (&$base) { |
| 20 | + return array_pop($base); |
| 21 | + }; |
| 22 | + } |
| 23 | + if ($key === 'shift') { |
| 24 | + return return function () use (&$base) { |
| 25 | + return array_shift($base); |
| 26 | + }; |
| 27 | + } |
| 28 | + if ($key === 'push') { |
| 29 | + return return function ($item) use (&$base) { |
| 30 | + return array_push($base, $item); |
| 31 | + }; |
| 32 | + } |
| 33 | + if ($key === 'unshift') { |
| 34 | + return return function ($item) use (&$base) { |
| 35 | + return array_unshift($base, $item); |
| 36 | + }; |
| 37 | + } |
| 38 | + if ($key === 'indexOf') { |
| 39 | + return return function ($item) use (&$base) { |
| 40 | + $search = array_search($base, $item); |
| 41 | + |
| 42 | + return $search === false ? -1 : $search; |
| 43 | + }; |
| 44 | + } |
| 45 | + if ($key === 'slice') { |
| 46 | + return return function ($offset, $length = null, $preserveKeys = false) use (&$base) { |
| 47 | + return array_slice($base, $offset, $length, $preserveKeys); |
| 48 | + }; |
| 49 | + } |
| 50 | + if ($key === 'splice') { |
| 51 | + return return function ($offset, $length = null, $replacements = array()) use (&$base) { |
| 52 | + return array_splice($base, $offset, $length, $replacements); |
| 53 | + }; |
| 54 | + } |
| 55 | + if ($key === 'reverse') { |
| 56 | + return return function () use (&$base) { |
| 57 | + return array_reverse($base); |
| 58 | + }; |
| 59 | + } |
| 60 | + if ($key === 'reduce') { |
| 61 | + return return function ($callback, $initial = null) use (&$base) { |
| 62 | + return array_reduce($base, $callback, $initial); |
| 63 | + }; |
| 64 | + } |
| 65 | + if ($key === 'join') { |
| 66 | + return return function ($glue) use (&$base) { |
| 67 | + return implode($glue, $base); |
| 68 | + }; |
| 69 | + } |
| 70 | + if ($key === 'sort') { |
| 71 | + return return function ($callback = null) use (&$base) { |
| 72 | + return $callback ? usort($base, $callback) : sort($base); |
| 73 | + }; |
| 74 | + } |
| 75 | + |
| 76 | + return null; |
| 77 | + }; |
| 78 | + $getFromArray = function ($base, $key) { |
| 79 | + return isset($base[$key]) |
| 80 | + ? $base[$key] |
| 81 | + : $arrayPrototype($base, $key); |
| 82 | + }; |
| 83 | + $getCallable = function ($base, $key) use ($getFromArray) { |
| 84 | + if (is_callable(array($base, $key))) { |
| 85 | + return array($base, $key); |
| 86 | + } |
| 87 | + if ($base instanceof \ArrayAccess) { |
| 88 | + return $getFromArray($base, $key); |
| 89 | + } |
| 90 | + }; |
| 91 | + $fallbackDot = function ($base, $key) use ($getCallable) { |
| 92 | + if (is_string($base)) { |
| 93 | + if ($key === 'substr' || $key === 'slice') { |
| 94 | + return function ($start, $length = null) use ($base) { |
| 95 | + return func_num_args() === 1 ? substr($base, $start) : substr($base, $start, $length); |
| 96 | + }; |
| 97 | + } |
| 98 | + if ($key === 'charAt') { |
| 99 | + return function ($pos) use ($base) { |
| 100 | + return substr($base, $pos, 1); |
| 101 | + }; |
| 102 | + } |
| 103 | + if ($key === 'indexOf') { |
| 104 | + return function ($needle) use ($base) { |
| 105 | + $pos = strpos($base, $needle); |
| 106 | + |
| 107 | + return $pos === false ? -1 : $pos; |
| 108 | + }; |
| 109 | + } |
| 110 | + if ($key === 'toUpperCase') { |
| 111 | + return function () use ($base) { |
| 112 | + return strtoupper($base); |
| 113 | + }; |
| 114 | + } |
| 115 | + if ($key === 'toLowerCase') { |
| 116 | + return function () use ($base) { |
| 117 | + return strtolower($base); |
| 118 | + }; |
| 119 | + } |
| 120 | + if ($key === 'split') { |
| 121 | + return function ($delimiter) use ($base) { |
| 122 | + return explode($delimiter, $base); |
| 123 | + }; |
| 124 | + } |
| 125 | + if ($key === 'replace') { |
| 126 | + return function ($from, $to) use ($base) { |
| 127 | + return str_replace($from, $to, $base); |
| 128 | + }; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return $getCallable($base, $key); |
| 133 | + }; |
| 134 | + foreach (array_slice(func_get_args(), 1) as $key) { |
| 135 | + $base = is_array($base) |
| 136 | + ? $getFromArray($base, $key) |
| 137 | + : (is_object($base) |
| 138 | + ? (isset($base->$key) |
| 139 | + ? $base->$key |
| 140 | + : (method_exists($base, $method = "get" . ucfirst($key)) |
| 141 | + ? $base->$method() |
| 142 | + : (method_exists($base, $key) |
| 143 | + ? array($base, $key) |
| 144 | + : $getCallable($base, $key) |
| 145 | + ) |
| 146 | + ) |
| 147 | + ) |
| 148 | + : $fallbackDot($base, $key) |
| 149 | + ); |
| 150 | + } |
| 151 | + |
| 152 | + return $base; |
| 153 | +} |
0 commit comments