|
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
|
32 |
|
public function getNonNullItems($includeHiddenValue = false) |
|
18
|
|
|
{ |
|
19
|
32 |
|
$items = get_object_vars($this); |
|
20
|
|
|
|
|
21
|
32 |
|
foreach ($items as $key => $item) { |
|
22
|
32 |
|
if (substr($key, 0, 1) == "_" || $item === null) { |
|
23
|
32 |
|
unset($items[$key]); |
|
24
|
|
|
} |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
32 |
|
if ($includeHiddenValue && $this->_value !== null) { |
|
28
|
1 |
|
$items['_value'] = $this->_value; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
32 |
|
return $items; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @param $array |
|
36
|
|
|
* @return static|XmlObject |
|
37
|
|
|
*/ |
|
38
|
33 |
|
public static function buildFromArray($array) |
|
39
|
|
|
{ |
|
40
|
33 |
|
if (static::class === Type::class) { |
|
|
|
|
|
|
41
|
33 |
|
return XmlObject::buildFromArray($array); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
33 |
|
if (!is_array($array)) { |
|
45
|
17 |
|
return $array; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
33 |
|
if (!self::arrayIsAssoc($array)) { |
|
49
|
18 |
|
return self::buildArrayFromArray($array); |
|
|
|
|
|
|
50
|
|
|
} else { |
|
51
|
33 |
|
return self::buildObjectFromArray($array); |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
33 |
|
protected static function buildObjectFromArray($array) |
|
56
|
|
|
{ |
|
57
|
33 |
|
$object = new static(); |
|
58
|
33 |
|
foreach ($array as $key => $value) { |
|
59
|
33 |
|
if (is_array($value)) { |
|
60
|
32 |
|
$value = self::buildFromArray($value); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
//I think _value is a more expressive way to set string value, but Soap needs _ |
|
64
|
33 |
|
if ($key === "_value") { |
|
65
|
5 |
|
$key = "_"; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
33 |
|
$object->$key = $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
33 |
|
return $object; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
18 |
|
public static function buildArrayFromArray($array) |
|
75
|
|
|
{ |
|
76
|
18 |
|
foreach ($array as $key => $value) { |
|
77
|
18 |
|
$array[$key] = self::buildFromArray($value); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
18 |
|
return $array; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
6 |
|
public function toXmlObject() |
|
84
|
|
|
{ |
|
85
|
6 |
|
$objectToReturn = new XmlObject(); |
|
86
|
6 |
|
$objectToReturn->_ = (string) $this; |
|
87
|
|
|
|
|
88
|
6 |
|
$properties = $this->getNonNullItems(true); |
|
89
|
|
|
|
|
90
|
6 |
|
foreach ($properties as $name => $property) { |
|
91
|
|
|
//I think _value is a more expressive way to set string value, but Soap needs _ |
|
92
|
6 |
|
if ($name == "_value") { |
|
93
|
|
|
$name = "_"; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
6 |
|
$name = ucfirst($name); |
|
97
|
6 |
|
$objectToReturn->$name = $this->propertyToXml($name, $property); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
6 |
|
return $objectToReturn; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param $name |
|
105
|
|
|
* @param $property |
|
106
|
|
|
* @return array|Type|null |
|
107
|
|
|
*/ |
|
108
|
6 |
|
protected function propertyToXml($name, $property) |
|
109
|
|
|
{ |
|
110
|
6 |
|
if (isset($this->_typeMap[lcfirst($name)])) { |
|
111
|
|
|
return $this->castToExchange($property, $this->_typeMap[lcfirst($name)]); |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
6 |
|
if ($property instanceof Type) { |
|
115
|
6 |
|
return $property->toXmlObject(); |
|
|
|
|
|
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
6 |
|
if (is_array($property) && $this->arrayIsAssoc($property)) { |
|
119
|
|
|
return $this->buildFromArray($property); |
|
|
|
|
|
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
6 |
|
if (is_array($property)) { |
|
123
|
3 |
|
return array_map(function ($property) { |
|
124
|
3 |
|
if ($property instanceof Type) { |
|
125
|
3 |
|
return $property->toXmlObject(); |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $property; |
|
129
|
3 |
|
}, $property); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
6 |
|
return $property; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
39 |
|
public static function arrayIsAssoc($array) |
|
136
|
|
|
{ |
|
137
|
39 |
|
return (bool) count(array_filter(array_keys($array), 'is_string')); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
/** |
|
141
|
|
|
* Clones any object properties on a type object when it is cloned. Allows |
|
142
|
|
|
* for a deep clone required when using object to represent data types when |
|
143
|
|
|
* making a SOAP call. |
|
144
|
|
|
*/ |
|
145
|
1 |
|
public function __clone() |
|
146
|
|
|
{ |
|
147
|
|
|
// Iterate over all properties on the current object. |
|
148
|
1 |
|
foreach (get_object_vars($this) as $property => $value) { |
|
149
|
1 |
|
$this->$property = \garethp\ews\Utilities\cloneValue($value); |
|
150
|
|
|
} |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
23 |
|
public function __toString() |
|
154
|
|
|
{ |
|
155
|
23 |
|
if (!is_string($this->_)) { |
|
|
|
|
|
|
156
|
4 |
|
return ''; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
19 |
|
return $this->_; |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|