-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAbstractGetterSetterTrait.php
51 lines (45 loc) · 1.39 KB
/
AbstractGetterSetterTrait.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
<?php
namespace AC\ModelTraits;
/**
* Reduces boiler plate by providing automatic getter/setters for any defined property.
*/
trait AbstractGetterSetterTrait
{
protected function acModelTraitsGetMethodMap()
{
$clsname = get_class($this);
static $metaMap = [];
if (!isset($metaMap[$clsname])) {
$metaMap[$clsname] = [];
$cls = new \ReflectionClass($clsname);
while ($cls) {
$map = $this->acModelTraitsGenerateMethodMap($cls);
$metaMap[$clsname] = array_merge($metaMap[$clsname], $map);
$cls = $cls->getParentClass();
}
}
return $metaMap[$clsname];
}
public function __call($method, $args)
{
$map = $this->acModelTraitsGetMethodMap();
if (!isset($map[$method])) {
throw new \BadMethodCallException(sprintf(
"No such method [%s] for class [%s].",
$method, get_class($this)
));
}
$data = $map[$method];
return $this->{$data['method']}($data['name'], $args);
}
protected function acModelTraitsGetProperty($name, $args)
{
return $this->$name;
}
protected function acModelTraitsSetProperty($name, $args)
{
$this->$name = $args[0];
return $this;
}
abstract protected function acModelTraitsGenerateMethodMap($class);
}