1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MovingImage\VMProApiClient\Util; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* AccessorTrait for general purposes. |
7
|
|
|
* |
8
|
|
|
* @author Omid Rad <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
trait AccessorTrait |
11
|
|
|
{ |
12
|
|
|
public static $typeSnakeCase = 0; |
13
|
|
|
public static $typeCamelCase = 1; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int Set default type to snake case |
17
|
|
|
*/ |
18
|
|
|
private $type = 0; |
19
|
|
|
|
20
|
|
|
private $container = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param $methodName string Key |
24
|
|
|
* @param $args array Method Arguments |
25
|
|
|
* |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
public function __call($methodName, $args) |
29
|
|
|
{ |
30
|
|
|
// are we getting or setting? |
31
|
|
|
if (preg_match('~^(set|get|is)([A-Z])(.*)$~', $methodName, $matches)) { |
32
|
|
|
$property = strtolower($matches[2]).$matches[3]; |
33
|
|
|
|
34
|
|
|
if ($this->type === self::$typeSnakeCase) { |
35
|
|
|
$property = strtolower(preg_replace('/(?<!^)[A-Z]/', '_$0', $property)); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
switch ($matches[1]) { |
39
|
|
|
case 'set': |
40
|
|
|
$this->checkArguments($args, 1, 1, $methodName); |
41
|
|
|
|
42
|
|
|
return $this->set($property, $args[0]); |
43
|
|
|
case 'is': |
44
|
|
|
case 'get': |
45
|
|
|
$this->checkArguments($args, 0, 0, $methodName); |
46
|
|
|
|
47
|
|
|
return $this->get($property); |
48
|
|
|
case 'default': |
49
|
|
|
throw new \BadMethodCallException("Method $methodName is not exist"); |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return null; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param $property string Key |
58
|
|
|
* |
59
|
|
|
* @return mixed |
60
|
|
|
*/ |
61
|
|
|
public function get($property) |
62
|
|
|
{ |
63
|
|
|
if (isset($this->container[$property])) { |
64
|
|
|
return $this->container[$property]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return null; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param $property string Key |
72
|
|
|
* @param $value string Value |
73
|
|
|
* |
74
|
|
|
* @return self |
75
|
|
|
*/ |
76
|
|
|
public function set($property, $value) |
77
|
|
|
{ |
78
|
|
|
$this->container[$property] = $value; |
79
|
|
|
|
80
|
|
|
return $this; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Check if args are valid or not. |
85
|
|
|
* |
86
|
|
|
* @param array $args List of arguments |
87
|
|
|
* @param int $min integer Minimum valid params |
88
|
|
|
* @param int $max Maximum valid params |
89
|
|
|
* @param string $methodName Method name |
90
|
|
|
*/ |
91
|
|
|
protected function checkArguments(array $args, $min, $max, $methodName) |
92
|
|
|
{ |
93
|
|
|
$argc = count($args); |
94
|
|
|
if ($argc < $min || $argc > $max) { |
95
|
|
|
throw new \BadMethodCallException("Method $methodName is not exist"); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
|
|
public function getContainer() |
103
|
|
|
{ |
104
|
|
|
return $this->container; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return int |
109
|
|
|
*/ |
110
|
|
|
public function getType() |
111
|
|
|
{ |
112
|
|
|
return $this->type; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param int $type |
117
|
|
|
* |
118
|
|
|
* @return $this |
119
|
|
|
*/ |
120
|
|
|
public function setType($type) |
121
|
|
|
{ |
122
|
|
|
$this->type = $type; |
123
|
|
|
|
124
|
|
|
return $this; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|