1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace garethp\ews; |
4
|
|
|
|
5
|
|
|
use garethp\ews\API\Type; |
6
|
|
|
use garethp\ews\API\XmlObject; |
7
|
|
|
|
8
|
|
|
trait BuildableTrait |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
public $_ = ''; |
14
|
|
|
|
15
|
|
|
public $_value = null; |
16
|
|
|
|
17
|
38 |
|
public function getNonNullItems($includeHiddenValue = false) |
18
|
|
|
{ |
19
|
38 |
|
$items = get_object_vars($this); |
20
|
|
|
|
21
|
38 |
|
foreach ($items as $key => $item) { |
22
|
38 |
|
if (substr($key, 0, 1) == "_" || $item === null) { |
23
|
38 |
|
unset($items[$key]); |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
38 |
|
if ($includeHiddenValue && $this->_value !== null) { |
28
|
1 |
|
$items['_value'] = $this->_value; |
29
|
|
|
} |
30
|
|
|
|
31
|
38 |
|
return $items; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @param $array |
36
|
|
|
* @param bool $strict When set to true, we'll use reflection to build the objects |
37
|
|
|
* |
38
|
|
|
* @return static|XmlObject |
39
|
|
|
*/ |
40
|
39 |
|
public static function buildFromArray($array, bool $strict = false) |
41
|
|
|
{ |
42
|
39 |
|
if (static::class === Type::class) { |
|
|
|
|
43
|
39 |
|
return XmlObject::buildFromArray($array, $strict); |
44
|
|
|
} |
45
|
|
|
|
46
|
39 |
|
if ($array instanceof XmlObject && $strict) { |
47
|
2 |
|
$array = (array)$array; |
48
|
|
|
} |
49
|
|
|
|
50
|
39 |
|
if (!is_array($array)) { |
51
|
22 |
|
return $array; |
52
|
|
|
} |
53
|
|
|
|
54
|
39 |
|
if (!self::arrayIsAssoc($array)) { |
55
|
25 |
|
return self::buildArrayFromArray($array, $strict); |
|
|
|
|
56
|
|
|
} else { |
57
|
39 |
|
return self::buildObjectFromArray($array, $strict); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
39 |
|
protected static function buildObjectFromArray($array, bool $strict = false) |
62
|
|
|
{ |
63
|
39 |
|
$object = new static(); |
64
|
39 |
|
$reflect = new \ReflectionClass(static::class); |
65
|
|
|
|
66
|
39 |
|
foreach ($array as $key => $value) { |
67
|
|
|
// If we're in strict mode, let's take the reflection class, check for a setter and try to use that to build |
68
|
|
|
// the array, resulting in type-correct responses the whole way down. |
69
|
39 |
|
if ($strict === true && $reflect->hasMethod("set" . ucfirst($key))) { |
70
|
2 |
|
$parameters = $reflect->getMethod("set" . ucfirst($key))->getParameters(); |
71
|
|
|
|
72
|
2 |
|
if (count($parameters) === 1 |
73
|
2 |
|
&& $parameters[0]->hasType() |
74
|
2 |
|
&& $parameters[0]->getClass() !== null) { |
75
|
2 |
|
$classToBuild = $parameters[0]->getClass()->getName(); |
76
|
|
|
|
77
|
2 |
|
$newValue = call_user_func("$classToBuild::buildFromArray", $value, true); |
78
|
2 |
|
$object->{ucfirst($key)} = $newValue; |
79
|
2 |
|
continue; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
39 |
|
if (is_array($value)) { |
84
|
38 |
|
$value = self::buildFromArray($value); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
//I think _value is a more expressive way to set string value, but Soap needs _ |
88
|
39 |
|
if ($key === "_value") { |
89
|
10 |
|
$key = "_"; |
90
|
|
|
} |
91
|
|
|
|
92
|
39 |
|
if ($value instanceof Type) { |
93
|
1 |
|
$value = $value->toXmlObject(); |
94
|
|
|
} |
95
|
|
|
|
96
|
39 |
|
$object->{ucfirst($key)} = $value; |
97
|
|
|
} |
98
|
|
|
|
99
|
39 |
|
return $object; |
100
|
|
|
} |
101
|
|
|
|
102
|
25 |
|
public static function buildArrayFromArray($array) |
103
|
|
|
{ |
104
|
25 |
|
foreach ($array as $key => $value) { |
105
|
25 |
|
$array[$key] = self::buildFromArray($value); |
106
|
|
|
} |
107
|
|
|
|
108
|
25 |
|
return $array; |
109
|
|
|
} |
110
|
|
|
|
111
|
7 |
|
public function toXmlObject() |
112
|
|
|
{ |
113
|
7 |
|
$objectToReturn = new XmlObject(); |
114
|
7 |
|
$objectToReturn->_ = (string)$this; |
115
|
|
|
|
116
|
7 |
|
$properties = $this->getNonNullItems(true); |
117
|
|
|
|
118
|
7 |
|
foreach ($properties as $name => $property) { |
119
|
|
|
//I think _value is a more expressive way to set string value, but Soap needs _ |
120
|
7 |
|
if ($name == "_value") { |
121
|
|
|
$name = "_"; |
122
|
|
|
} |
123
|
|
|
|
124
|
7 |
|
$name = ucfirst($name); |
125
|
7 |
|
$objectToReturn->$name = $this->propertyToXml($name, $property); |
126
|
|
|
} |
127
|
|
|
|
128
|
7 |
|
return $objectToReturn; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param $name |
133
|
|
|
* @param $property |
134
|
|
|
* @return array|Type|null |
135
|
|
|
*/ |
136
|
7 |
|
protected function propertyToXml($name, $property) |
|
|
|
|
137
|
|
|
{ |
138
|
7 |
|
if ($property instanceof \DateTime) { |
139
|
|
|
$property = $property->format("c"); |
140
|
|
|
} |
141
|
|
|
|
142
|
7 |
|
if ($property instanceof Type) { |
143
|
6 |
|
return $property->toXmlObject(); |
|
|
|
|
144
|
|
|
} |
145
|
|
|
|
146
|
7 |
|
if (is_array($property) && $this->arrayIsAssoc($property)) { |
147
|
|
|
return $this->buildFromArray($property); |
|
|
|
|
148
|
|
|
} |
149
|
|
|
|
150
|
7 |
|
if (is_array($property)) { |
151
|
5 |
|
return array_map(function ($property) { |
152
|
5 |
|
if ($property instanceof Type) { |
153
|
5 |
|
return $property->toXmlObject(); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
return $property; |
157
|
5 |
|
}, $property); |
158
|
|
|
} |
159
|
|
|
|
160
|
7 |
|
return $property; |
161
|
|
|
} |
162
|
|
|
|
163
|
45 |
|
public static function arrayIsAssoc($array) |
164
|
|
|
{ |
165
|
45 |
|
return (bool)count(array_filter(array_keys($array), 'is_string')); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
/** |
169
|
|
|
* Clones any object properties on a type object when it is cloned. Allows |
170
|
|
|
* for a deep clone required when using object to represent data types when |
171
|
|
|
* making a SOAP call. |
172
|
|
|
*/ |
173
|
1 |
|
public function __clone() |
174
|
|
|
{ |
175
|
|
|
// Iterate over all properties on the current object. |
176
|
1 |
|
foreach (get_object_vars($this) as $property => $value) { |
177
|
1 |
|
$this->$property = \garethp\ews\Utilities\cloneValue($value); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
|
181
|
27 |
|
public function __toString() |
182
|
|
|
{ |
183
|
27 |
|
if (!is_string($this->_)) { |
|
|
|
|
184
|
6 |
|
return ''; |
185
|
|
|
} |
186
|
|
|
|
187
|
23 |
|
return $this->_; |
188
|
|
|
} |
189
|
|
|
} |
190
|
|
|
|