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