1
|
|
|
<?php |
2
|
|
|
namespace Narrowspark\Arr; |
3
|
|
|
|
4
|
|
|
use BadMethodCallException; |
5
|
|
|
use ReflectionClass; |
6
|
|
|
use ReflectionMethod; |
7
|
|
|
use RuntimeException; |
8
|
|
|
|
9
|
|
|
class Arr |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* A mapping of method names to the numbers of arguments it accepts. Each |
13
|
|
|
* should be two more than the equivalent method. |
14
|
|
|
* |
15
|
|
|
* @var string[] |
16
|
|
|
*/ |
17
|
|
|
protected $classes = [ |
18
|
|
|
Access::class, |
19
|
|
|
Enumerator::class, |
20
|
|
|
Transform::class, |
21
|
|
|
Traverse::class |
22
|
|
|
]; |
23
|
|
|
|
24
|
|
|
protected $methodArgs = null; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Invokes the given method with the rest of the passed arguments. |
28
|
|
|
* The result is not cast, so the return value may be of type Arr, array, |
29
|
|
|
* integer, boolean, etc. |
30
|
|
|
* |
31
|
|
|
* @param string $name |
32
|
|
|
* @param mixed[] $args |
33
|
|
|
* |
34
|
|
|
* @throws \BadMethodCallException |
35
|
|
|
* @throws \RuntimeException |
36
|
|
|
* |
37
|
|
|
* @return mixed |
38
|
|
|
*/ |
39
|
|
|
public function __call($name, $args) |
40
|
|
|
{ |
41
|
|
|
$this->getMethodArgs($name); |
42
|
|
|
|
43
|
|
|
if (!isset($this->methodArgs[$name])) { |
44
|
|
|
throw new BadMethodCallException(sprintf('%s is not a valid method.', $name)); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
if (count($args) !== $this->methodArgs[$name]) { |
48
|
|
|
throw new RuntimeException( |
49
|
|
|
sprintf( |
50
|
|
|
'%s counted arguments dont match needed arguments %s for function %s.', |
51
|
|
|
count($args), |
52
|
|
|
count($this->methodArgs[$name]), |
53
|
|
|
$name |
54
|
|
|
) |
55
|
|
|
); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
foreach ($this->classes as $class) { |
59
|
|
|
if (method_exists($class, $name)) { |
60
|
|
|
$this->getFunction(new $class(), $name, $args); |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Get all methods arguments. |
67
|
|
|
* |
68
|
|
|
* @param string $name |
69
|
|
|
*/ |
70
|
|
|
protected function getMethodArgs($name) |
|
|
|
|
71
|
|
|
{ |
72
|
|
|
if (!$this->methodArgs) { |
73
|
|
|
foreach ($this->classes as $classInterface) { |
74
|
|
|
$class = new ReflectionClass($classInterface); |
75
|
|
|
$methods = $class->getMethods(ReflectionMethod::IS_PUBLIC); |
76
|
|
|
|
77
|
|
|
foreach ($methods as $method) { |
78
|
|
|
$params = $method->getNumberOfParameters(); |
79
|
|
|
$this->methodArgs[$method->name] = $params; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get function from the correct object. |
87
|
|
|
* |
88
|
|
|
* @param object $instance |
89
|
|
|
* @param string $method |
90
|
|
|
* @param array $args |
91
|
|
|
* |
92
|
|
|
* @return mixed |
93
|
|
|
*/ |
94
|
|
|
protected function getFunction($instance, $method, $args) |
95
|
|
|
{ |
96
|
|
|
switch (count($args)) { |
97
|
|
|
case 2: |
98
|
|
|
return $instance->$method($args[0], $args[1]); |
99
|
|
|
|
100
|
|
|
case 3: |
101
|
|
|
return $instance->$method($args[0], $args[1], $args[2]); |
102
|
|
|
|
103
|
|
|
case 4: |
104
|
|
|
return $instance->$method($args[0], $args[1], $args[2], $args[3]); |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.