1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the Borobudur-DependencyInjection package. |
4
|
|
|
* |
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Borobudur\DependencyInjection\Definition; |
12
|
|
|
|
13
|
|
|
use Borobudur\DependencyInjection\Exception\InvalidArgumentException; |
14
|
|
|
use Borobudur\DependencyInjection\NumericParameterTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Iqbal Maulana <[email protected]> |
18
|
|
|
* @created 8/8/15 |
19
|
|
|
*/ |
20
|
|
|
class MethodCall |
21
|
|
|
{ |
22
|
|
|
use NumericParameterTrait { |
23
|
|
|
NumericParameterTrait::add as protected addMethod; |
24
|
|
|
NumericParameterTrait::remove as protected removeMethod; |
25
|
|
|
NumericParameterTrait::set as protected; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Clear and replace with sets of method call. |
30
|
|
|
* |
31
|
|
|
* @param array $calls |
32
|
|
|
* |
33
|
|
|
* @return static |
34
|
|
|
*/ |
35
|
|
|
public function replace(array $calls) |
36
|
|
|
{ |
37
|
|
|
$this->clear(); |
38
|
|
|
foreach($calls as $call) { |
39
|
|
|
if (is_array($call)) { |
40
|
|
|
$args = isset($call[1]) ? $call[1] : array(); |
41
|
|
|
$this->add($call[0], $args); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Add method call. |
50
|
|
|
* |
51
|
|
|
* @param string $method |
52
|
|
|
* @param array $arguments |
53
|
|
|
* |
54
|
|
|
* @return static |
55
|
|
|
*/ |
56
|
|
|
public function add($method, array $arguments = array()) |
57
|
|
|
{ |
58
|
|
|
if (empty($method)) { |
59
|
|
|
throw new InvalidArgumentException('Method name cannot be empty.'); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $this->addMethod(array($method, $arguments)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Remove method call. |
67
|
|
|
* |
68
|
|
|
* @param int $method |
69
|
|
|
* |
70
|
|
|
* @return static |
71
|
|
|
*/ |
72
|
|
|
public function remove($method) |
73
|
|
|
{ |
74
|
|
|
return $this->removeMethod($this->index($method)); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Check if has method call. |
79
|
|
|
* |
80
|
|
|
* @param string $method |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
|
|
public function has($method) |
85
|
|
|
{ |
86
|
|
|
return -1 !== $this->index($method); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Get index of method. |
91
|
|
|
* |
92
|
|
|
* @param string $method |
93
|
|
|
* |
94
|
|
|
* @return int |
95
|
|
|
*/ |
96
|
|
|
public function index($method) |
97
|
|
|
{ |
98
|
|
|
foreach ($this->all() as $i => $call) { |
99
|
|
|
if ($call[0] === $method) { |
100
|
|
|
return $i; |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return -1; |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|