|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Soheilrt\AdobeConnectClient\Client\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionException; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
use Soheilrt\AdobeConnectClient\Client\Helpers\StringCaseTransform as SCT; |
|
8
|
|
|
|
|
9
|
|
|
trait Setter |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* store properties which has not property in parent class. |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $attributes = []; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* magic getter function for attributes that don't exist or inaccessible. |
|
18
|
|
|
* |
|
19
|
|
|
* @param $name |
|
20
|
|
|
* |
|
21
|
|
|
* @throws ReflectionException |
|
22
|
|
|
* @return mixed|null |
|
23
|
|
|
*/ |
|
24
|
|
|
public function __get($name) |
|
25
|
|
|
{ |
|
26
|
|
|
if ($this->hasGetter($name) && !$this->methodHasRequiredParameter($this->getQualifiedGetterMethodName($name))) { |
|
27
|
|
|
return $this->{$this->getQualifiedGetterMethodName($name)}(); |
|
28
|
|
|
} |
|
29
|
|
|
if ($this->hasProperty($name)) { |
|
30
|
|
|
return $this->{$name}; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$QualifiedAttributeName = $this->getQualifiedAttributeName($name); |
|
34
|
|
|
|
|
35
|
|
|
return $this->attributes[$QualifiedAttributeName] ?? null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* magic setter function for attributes that don't exist or inaccessible. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $name |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
* |
|
44
|
|
|
* @return static |
|
45
|
|
|
*/ |
|
46
|
|
|
public function __set($name, $value) |
|
47
|
|
|
{ |
|
48
|
|
|
$setter = $this->getQualifiedSetterMethodName($name); |
|
49
|
|
|
if (method_exists($this, $setter)) { |
|
50
|
|
|
$this->$setter($value); |
|
51
|
|
|
|
|
52
|
|
|
return $this; |
|
53
|
|
|
} else { |
|
54
|
|
|
$this->attributes[$this->getQualifiedAttributeName($name)] = $value; |
|
55
|
|
|
|
|
56
|
|
|
return $this; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Determine if function has getter method. |
|
62
|
|
|
* |
|
63
|
|
|
* @param $name |
|
64
|
|
|
* |
|
65
|
|
|
* @return bool |
|
66
|
|
|
*/ |
|
67
|
|
|
private function hasGetter($name): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return method_exists($this, $this->getQualifiedGetterMethodName($name)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* Return Getter Attribute Qualified Getter Name. |
|
74
|
|
|
* |
|
75
|
|
|
* @param $name |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
|
|
private function getQualifiedGetterMethodName($name): string |
|
80
|
|
|
{ |
|
81
|
|
|
return 'get' . SCT::toUpperCamelCase($name); |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* determine if asked method has any required parameters |
|
86
|
|
|
* |
|
87
|
|
|
* @param $method |
|
88
|
|
|
* |
|
89
|
|
|
* @throws ReflectionException |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
private function methodHasRequiredParameter($method): bool |
|
93
|
|
|
{ |
|
94
|
|
|
return (new ReflectionMethod($this, $method))->getNumberOfRequiredParameters() > 0; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
/** |
|
98
|
|
|
* @param string $name |
|
99
|
|
|
* |
|
100
|
|
|
* @return bool |
|
101
|
|
|
*/ |
|
102
|
|
|
private function hasProperty($name): bool |
|
103
|
|
|
{ |
|
104
|
|
|
return property_exists($this, $name); |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* cast attribute name to qualified hyphen name. |
|
109
|
|
|
* |
|
110
|
|
|
* @param $name |
|
111
|
|
|
* |
|
112
|
|
|
* @return string |
|
113
|
|
|
*/ |
|
114
|
|
|
private function getQualifiedAttributeName($name): string |
|
115
|
|
|
{ |
|
116
|
|
|
return SCT::toCamelCase($name); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Return Setter Method Attribute Qualified Name. |
|
121
|
|
|
* |
|
122
|
|
|
* @param string $name |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
private function getQualifiedSetterMethodName($name): string |
|
127
|
|
|
{ |
|
128
|
|
|
return 'set' . SCT::toUpperCamelCase($name); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* this method will check if attribute is available in object instance. first it'll check for attribute getter and |
|
133
|
|
|
* return value of getter method after that it'll check for attribute value in attributes array If all of |
|
134
|
|
|
* these conditions fails, it'll check for attribute property in class. |
|
135
|
|
|
* |
|
136
|
|
|
* |
|
137
|
|
|
* @param string $name |
|
138
|
|
|
* |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function __isset($name) |
|
142
|
|
|
{ |
|
143
|
|
|
if ($this->hasGetter($name)) { |
|
144
|
|
|
return $this->${$this->getQualifiedGetterMethodName($name)} === null; |
|
145
|
|
|
} |
|
146
|
|
|
if (isset($this->attributes[$this->getQualifiedAttributeName($name)])) { |
|
147
|
|
|
return true; |
|
148
|
|
|
} |
|
149
|
|
|
return isset($this->$name); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|