|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ByTIC\EventDispatcher\Events\Traits; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Trait HasNameTrait |
|
7
|
|
|
* @package ByTIC\EventDispatcher\Events\Traits |
|
8
|
|
|
*/ |
|
9
|
|
|
trait HasArgumentsTrait |
|
10
|
|
|
{ |
|
11
|
|
|
protected $arguments; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Get argument by key. |
|
15
|
|
|
* |
|
16
|
|
|
* @return mixed |
|
17
|
|
|
* |
|
18
|
|
|
* @throws \InvalidArgumentException if key is not found |
|
19
|
|
|
*/ |
|
20
|
|
|
public function getArgument(string $key) |
|
21
|
|
|
{ |
|
22
|
|
|
if ($this->hasArgument($key)) { |
|
23
|
|
|
return $this->arguments[$key]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
throw new \InvalidArgumentException(sprintf('Argument "%s" not found.', $key)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Add argument to event. |
|
31
|
|
|
* |
|
32
|
|
|
* @param mixed $value Value |
|
33
|
|
|
* |
|
34
|
|
|
* @return $this |
|
35
|
|
|
*/ |
|
36
|
|
|
public function setArgument(string $key, $value) |
|
37
|
|
|
{ |
|
38
|
|
|
$this->arguments[$key] = $value; |
|
39
|
|
|
|
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Getter for all arguments. |
|
45
|
|
|
* |
|
46
|
|
|
* @return array |
|
47
|
|
|
*/ |
|
48
|
|
|
public function getArguments() |
|
49
|
|
|
{ |
|
50
|
|
|
return $this->arguments; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Set args property. |
|
55
|
|
|
* |
|
56
|
|
|
* @return $this |
|
57
|
|
|
*/ |
|
58
|
|
|
public function setArguments(array $args = []) |
|
59
|
|
|
{ |
|
60
|
|
|
$this->arguments = $args; |
|
61
|
|
|
|
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Has argument. |
|
67
|
|
|
* |
|
68
|
|
|
* @return bool |
|
69
|
|
|
*/ |
|
70
|
|
|
public function hasArgument(string $key) |
|
71
|
|
|
{ |
|
72
|
|
|
return \array_key_exists($key, $this->arguments); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* ArrayAccess for argument getter. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $key Array key |
|
79
|
|
|
* |
|
80
|
|
|
* @return mixed |
|
81
|
|
|
* |
|
82
|
|
|
* @throws \InvalidArgumentException if key does not exist in $this->args |
|
83
|
|
|
*/ |
|
84
|
|
|
public function offsetGet($key) |
|
85
|
|
|
{ |
|
86
|
|
|
return $this->getArgument($key); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* ArrayAccess for argument setter. |
|
91
|
|
|
* |
|
92
|
|
|
* @param string $key Array key to set |
|
93
|
|
|
* @param mixed $value Value |
|
94
|
|
|
* |
|
95
|
|
|
* @return void |
|
96
|
|
|
*/ |
|
97
|
|
|
public function offsetSet($key, $value) |
|
98
|
|
|
{ |
|
99
|
|
|
$this->setArgument($key, $value); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* ArrayAccess for unset argument. |
|
104
|
|
|
* |
|
105
|
|
|
* @param string $key Array key |
|
106
|
|
|
* |
|
107
|
|
|
* @return void |
|
108
|
|
|
*/ |
|
109
|
|
|
public function offsetUnset($key) |
|
110
|
|
|
{ |
|
111
|
|
|
if ($this->hasArgument($key)) { |
|
112
|
|
|
unset($this->arguments[$key]); |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* ArrayAccess has argument. |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $key Array key |
|
120
|
|
|
* |
|
121
|
|
|
* @return bool |
|
122
|
|
|
*/ |
|
123
|
|
|
public function offsetExists($key) |
|
124
|
|
|
{ |
|
125
|
|
|
return $this->hasArgument($key); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* IteratorAggregate for iterating over the object like an array. |
|
130
|
|
|
* |
|
131
|
|
|
* @return \ArrayIterator<string, mixed> |
|
132
|
|
|
*/ |
|
133
|
|
|
public function getIterator() |
|
134
|
|
|
{ |
|
135
|
|
|
return new \ArrayIterator($this->arguments); |
|
136
|
|
|
} |
|
137
|
|
|
} |
|
138
|
|
|
|