1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: gareth |
5
|
|
|
* Date: 27-8-15 |
6
|
|
|
* Time: 10:27 |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace garethp\ews\API; |
10
|
|
|
|
11
|
|
|
use garethp\ews\Caster; |
12
|
|
|
|
13
|
|
|
trait MagicMethodsTrait |
14
|
|
|
{ |
15
|
|
|
protected $_typeMap = []; |
16
|
|
|
|
17
|
37 |
|
public function __call($name, $arguments) |
18
|
|
|
{ |
19
|
37 |
|
$callTypeIndex = 3; |
20
|
37 |
|
if (substr($name, 0, 2) == "is") { |
21
|
4 |
|
$callTypeIndex = 2; |
22
|
4 |
|
} |
23
|
|
|
|
24
|
37 |
|
$callType = substr($name, 0, $callTypeIndex); |
25
|
37 |
|
$propertyName = substr($name, $callTypeIndex); |
26
|
|
|
|
27
|
37 |
|
if (in_array($callType, array('get', 'is')) && count($arguments) == 0) { |
28
|
33 |
|
return $this->{$callType}($propertyName); |
29
|
|
|
} |
30
|
|
|
|
31
|
12 |
|
if (in_array($callType, array('add', 'set')) && count($arguments) == 1) { |
32
|
11 |
|
return $this->{$callType}($propertyName, $arguments[0]); |
33
|
|
|
} |
34
|
|
|
|
35
|
1 |
|
throw new \Exception("The method you tried to call doesn't exist"); |
36
|
|
|
} |
37
|
|
|
|
38
|
31 |
|
public function __set($name, $value) |
39
|
|
|
{ |
40
|
31 |
View Code Duplication |
if (!$this->exists($name) && $this->exists(lcfirst($name))) { |
|
|
|
|
41
|
25 |
|
$name = lcfirst($name); |
42
|
25 |
|
} |
43
|
|
|
|
44
|
31 |
|
$this->$name = $value; |
45
|
31 |
|
} |
46
|
|
|
|
47
|
37 |
|
public function exists($name) |
48
|
|
|
{ |
49
|
37 |
|
return property_exists($this, $name); |
50
|
|
|
} |
51
|
|
|
|
52
|
30 |
|
public function get($name) |
53
|
|
|
{ |
54
|
30 |
|
$name = $this->getValidNameInCorrectCase([$name, "get$name"]); |
55
|
29 |
|
return $this->$name; |
56
|
|
|
} |
57
|
|
|
|
58
|
10 |
|
public function set($name, $value) |
59
|
|
|
{ |
60
|
10 |
|
$name = $this->getNameInCorrectCase($name); |
61
|
9 |
|
$value = $this->castValueIfNeeded($name, $value); |
|
|
|
|
62
|
|
|
|
63
|
9 |
|
$this->$name = $value; |
64
|
|
|
|
65
|
9 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
5 |
|
public function add($name, $value) |
69
|
|
|
{ |
70
|
5 |
|
$name = $this->getNameInCorrectCase($name); |
71
|
4 |
|
$value = $this->castValueIfNeeded($name, $value); |
|
|
|
|
72
|
|
|
|
73
|
4 |
|
if ($this->$name == null) { |
74
|
4 |
|
$this->$name = array(); |
75
|
4 |
|
} |
76
|
|
|
|
77
|
4 |
|
if (!is_array($this->$name)) { |
78
|
1 |
|
$this->$name = array($this->$name); |
79
|
1 |
|
} |
80
|
|
|
|
81
|
4 |
|
$this->{$name}[] = $value; |
82
|
|
|
|
83
|
4 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
|
86
|
4 |
|
public function is($name) |
87
|
|
|
{ |
88
|
4 |
|
$nameWithIs = "Is$name"; |
89
|
4 |
|
$name = $this->getValidNameInCorrectCase([$nameWithIs, $name]); |
90
|
|
|
|
91
|
3 |
|
return ((bool) $this->$name); |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
public function cast($value, $type) |
95
|
|
|
{ |
96
|
2 |
|
return Caster::cast($value, $type); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function castToExchange($value, $type) |
100
|
|
|
{ |
101
|
|
|
if (Caster::castExists($type, 'ExchangeFormat')) { |
102
|
|
|
$value = Caster::cast($value, 'ExchangeFormat'); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return $value; |
106
|
|
|
} |
107
|
|
|
|
108
|
33 |
|
protected function getValidNameInCorrectCase($names) |
109
|
|
|
{ |
110
|
33 |
|
foreach ($names as $name) { |
111
|
|
|
try { |
112
|
33 |
|
return $this->getNameInCorrectCase($name); |
113
|
4 |
|
} catch (\Exception $e) { |
114
|
|
|
//Nothing needed here. If everything errors out, we'll throw a new exception below |
115
|
|
|
} |
116
|
4 |
|
} |
117
|
|
|
|
118
|
2 |
|
throw new \Exception('Property ' . $names[0] . ' does not exist'); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $name |
123
|
|
|
* @return string |
124
|
|
|
* @throws \Exception |
125
|
|
|
*/ |
126
|
36 |
|
protected function getNameInCorrectCase($name) |
127
|
|
|
{ |
128
|
36 |
View Code Duplication |
if (!$this->exists($name) && $this->exists(lcfirst($name))) { |
|
|
|
|
129
|
31 |
|
$name = lcfirst($name); |
130
|
31 |
|
} |
131
|
|
|
|
132
|
36 |
|
if (!$this->exists($name)) { |
133
|
6 |
|
throw new \Exception('Property ' . $name . ' does not exist'); |
134
|
|
|
} |
135
|
|
|
|
136
|
32 |
|
return $name; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* @param $name |
141
|
|
|
* @param $value |
142
|
|
|
* @return null |
143
|
|
|
*/ |
144
|
9 |
|
protected function castValueIfNeeded($name, $value) |
145
|
|
|
{ |
146
|
9 |
|
if (isset($this->_typeMap[$name])) { |
147
|
1 |
|
$value = $this->cast($value, $this->_typeMap[$name]); |
148
|
|
|
|
149
|
1 |
|
return $value; |
150
|
|
|
} |
151
|
|
|
|
152
|
8 |
|
return $value; |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.