/
component.php
160 lines (134 loc) · 3.53 KB
/
component.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
namespace Octo;
use ArrayAccess;
use function get_class_methods;
use function is_callable;
class Component implements ArrayAccess
{
/**
* @var mixed
*/
protected $__bag;
/**
* @param array $data
* @return void
*/
public function __construct(array $data = [])
{
$this->__bag = new Parametermemory($data);
}
/**
* @param string $key
* @param $value
*/
public function __set(string $key, $value)
{
$this->__bag[$key] = $value;
}
/**
* @param string $key
* @return mixed
*/
public function __get(string $key)
{
return $this->__bag->{$key} ?? null;
}
/**
* @param $method
* @param $parameters
* @return Component|mixed
* @throws \ReflectionException
*/
public function __call($method, $parameters)
{
if ($callable = $this->__bag->{$method}) {
if (is_callable($callable)) {
if ($callable instanceof \Closure) {
$args = array_merge([$callable], $parameters);
return gi()->makeClosure(...$args);
} elseif (is_array($callable)) {
$args = array_merge($callable, $parameters);
return gi()->call(...$args);
}
} else {
return $callable;
}
}
if (fnmatch('get*', $method) && strlen($method) > 3) {
$key = Inflector::uncamelize(substr($method, 3));
$default = empty($parameters) ? null : current($parameters);
return isset($this->{$key}) ? $this->{$key} : $default;
} elseif (fnmatch('set*', $method) && strlen($method) > 3) {
$key = Inflector::uncamelize(substr($method, 3));
$this->{$key} = current($parameters);
return $this;
} elseif (fnmatch('has*', $method) && strlen($method) > 3) {
$key = Inflector::uncamelize(substr($method, 3));
return isset($this->{$key});
} elseif (fnmatch('remove*', $method) && strlen($method) > 6) {
$key = Inflector::uncamelize(substr($method, 6));
if (isset($this->{$key})) {
unset($this->{$key});
return true;
}
return false;
}
if (!empty($parameters) && count($parameters) === 1 && is_callable(current($parameters))) {
$this->__bag->{$method} = current($parameters);
return $this;
}
if (in_array($method, get_class_methods($this->__bag))) {
return $this->__bag->{$method}(...$parameters);
}
return null;
}
/**
* @param mixed $key
*
* @return bool
*/
public function offsetExists($key)
{
return $this->__bag->has($key);
}
/**
* @param mixed $key
*
* @return bool
*/
public function __isset($key)
{
return $this->__bag->has($key);
}
/**
* @param mixed $key
*
* @return mixed
*/
public function offsetGet($key)
{
return $this->__bag->get($key);
}
/**
* @param mixed $key
* @param mixed $value
*/
public function offsetSet($key, $value)
{
$this->__bag->set($key, $value);
}
/**
* @param mixed $key
*/
public function offsetUnset($key)
{
$this->__bag->remove($key);
}
/**
* @param mixed $key
*/
public function __unset($key)
{
$this->__bag->remove($key);
}
}