|
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
|
1 |
|
public function __call($name, $arguments) |
|
18
|
|
|
{ |
|
19
|
1 |
|
$callTypeIndex = 3; |
|
20
|
1 |
|
if (substr($name, 0, 2) == "is") { |
|
21
|
|
|
$callTypeIndex = 2; |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
1 |
|
$callType = substr($name, 0, $callTypeIndex); |
|
25
|
1 |
|
$propertyName = substr($name, $callTypeIndex); |
|
26
|
|
|
|
|
27
|
1 |
|
if (in_array($callType, array('get', 'is')) && count($arguments) == 0) { |
|
28
|
|
|
return $this->{$callType}($propertyName); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
1 |
|
if (in_array($callType, array('add', 'set')) && count($arguments) == 1) { |
|
32
|
1 |
|
return $this->{$callType}($propertyName, $arguments[0]); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
throw new \Exception("The method you tried to call doesn't exist"); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
31 |
|
public function __set($name, $value) |
|
39
|
|
|
{ |
|
40
|
31 |
|
if (!$this->exists($name) && $this->exists(lcfirst($name))) { |
|
41
|
31 |
|
$name = lcfirst($name); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
31 |
|
$this->$name = $value; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
31 |
|
public function exists($name) |
|
48
|
|
|
{ |
|
49
|
31 |
|
return property_exists($this, $name); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
public function get($name) |
|
53
|
|
|
{ |
|
54
|
|
|
$name = $this->getValidNameInCorrectCase([$name, "get$name"]); |
|
55
|
|
|
return $this->$name; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function set($name, $value) |
|
59
|
|
|
{ |
|
60
|
|
|
$name = $this->getNameInCorrectCase($name); |
|
61
|
|
|
$value = $this->castValueIfNeeded($name, $value); |
|
|
|
|
|
|
62
|
|
|
|
|
63
|
|
|
$this->$name = $value; |
|
64
|
|
|
|
|
65
|
|
|
return $this; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
public function add($name, $value) |
|
69
|
|
|
{ |
|
70
|
1 |
|
$name = $this->getNameInCorrectCase($name); |
|
71
|
1 |
|
$value = $this->castValueIfNeeded($name, $value); |
|
|
|
|
|
|
72
|
|
|
|
|
73
|
1 |
|
if ($this->$name == null) { |
|
74
|
1 |
|
$this->$name = array(); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
1 |
|
if (!is_array($this->$name)) { |
|
78
|
|
|
$this->$name = array($this->$name); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
1 |
|
$this->{$name}[] = $value; |
|
82
|
|
|
|
|
83
|
1 |
|
return $this; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
public function is($name) |
|
87
|
|
|
{ |
|
88
|
|
|
$nameWithIs = "Is$name"; |
|
89
|
|
|
$name = $this->getValidNameInCorrectCase([$nameWithIs, $name]); |
|
90
|
|
|
|
|
91
|
|
|
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
|
|
|
protected function getValidNameInCorrectCase($names) |
|
109
|
|
|
{ |
|
110
|
|
|
foreach ($names as $name) { |
|
111
|
|
|
try { |
|
112
|
|
|
return $this->getNameInCorrectCase($name); |
|
113
|
|
|
} catch (\Exception $e) { |
|
114
|
|
|
//Nothing needed here. If everything errors out, we'll throw a new exception below |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
throw new \Exception('Property ' . $names[0] . ' does not exist'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* @param $name |
|
123
|
|
|
* @return string |
|
124
|
|
|
* @throws \Exception |
|
125
|
|
|
*/ |
|
126
|
1 |
|
protected function getNameInCorrectCase($name) |
|
127
|
|
|
{ |
|
128
|
1 |
|
if (!$this->exists($name) && $this->exists(lcfirst($name))) { |
|
129
|
1 |
|
$name = lcfirst($name); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
1 |
|
if (!$this->exists($name)) { |
|
133
|
|
|
throw new \Exception('Property ' . $name . ' does not exist'); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
1 |
|
return $name; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* @param $name |
|
141
|
|
|
* @param $value |
|
142
|
|
|
* @return null |
|
143
|
|
|
*/ |
|
144
|
11 |
|
protected function castValueIfNeeded($name, $value) |
|
145
|
|
|
{ |
|
146
|
11 |
|
if (isset($this->_typeMap[$name])) { |
|
147
|
1 |
|
$value = $this->cast($value, $this->_typeMap[$name]); |
|
148
|
|
|
|
|
149
|
1 |
|
return $value; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
10 |
|
return $value; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.