1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GloBee\PaymentApi\Models; |
4
|
|
|
|
5
|
|
|
trait PropertyTrait |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @param $name |
9
|
|
|
* |
10
|
|
|
* @return mixed |
11
|
|
|
*/ |
12
|
10 |
|
public function getProperty($name) |
13
|
|
|
{ |
14
|
10 |
|
$methodName = 'get'.$this->strToStudlyCase($name); |
15
|
10 |
|
if (method_exists($this, $methodName)) { |
16
|
|
|
return $this->{$methodName}(); |
17
|
|
|
} |
18
|
|
|
|
19
|
10 |
|
if (property_exists($this, $name)) { |
20
|
10 |
|
return $this->{$name}; |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $name |
26
|
|
|
* @param $value |
27
|
|
|
*/ |
28
|
4 |
|
protected function setProperty($name, $value) |
29
|
|
|
{ |
30
|
4 |
|
$methodName = 'set'.$this->strToStudlyCase($name); |
31
|
4 |
|
if (method_exists($this, $methodName)) { |
32
|
2 |
|
$this->{$methodName}($value); |
33
|
|
|
|
34
|
2 |
|
return; |
35
|
|
|
} |
36
|
|
|
|
37
|
4 |
|
if (property_exists($this, $name)) { |
38
|
4 |
|
$this->{$name} = $value; |
39
|
|
|
|
40
|
4 |
|
return; |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param $name |
46
|
|
|
* @param $arguments |
47
|
|
|
* |
48
|
|
|
* @return mixed|void |
49
|
|
|
*/ |
50
|
6 |
|
public function __call($name, $arguments) |
51
|
|
|
{ |
52
|
6 |
|
$mutator = substr($name, 0, 3); |
53
|
6 |
|
if ($mutator === 'get') { |
54
|
6 |
|
return $this->getProperty(lcfirst(substr($name, 3))); |
55
|
|
|
} |
56
|
|
|
|
57
|
1 |
|
if ($mutator === 'set') { |
58
|
1 |
|
$this->setProperty(lcfirst(substr($name, 3)), $arguments[0]); |
59
|
|
|
|
60
|
1 |
|
return; |
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
4 |
|
public function __get($name) |
65
|
|
|
{ |
66
|
4 |
|
return $this->getProperty($name); |
67
|
|
|
} |
68
|
|
|
|
69
|
3 |
|
public function __set($name, $value) |
70
|
|
|
{ |
71
|
3 |
|
$this->setProperty($name, $value); |
72
|
3 |
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @param $key |
76
|
|
|
* |
77
|
|
|
* @return mixed |
78
|
|
|
*/ |
79
|
10 |
|
protected function strToStudlyCase($key) |
80
|
|
|
{ |
81
|
10 |
|
return str_replace(' ', '', ucwords(str_replace('_', ' ', $key))); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|