Passed
Branch master (6fbc1b)
by Gareth
10:31
created

Type::propertyToXml()   B

Complexity

Conditions 7
Paths 5

Size

Total Lines 25
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 7.4822

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 7
eloc 13
nc 5
nop 2
dl 0
loc 25
ccs 11
cts 14
cp 0.7856
crap 7.4822
rs 8.8333
c 2
b 0
f 0
1
<?php
2
/**
3
 * Contains \garethp\ews\API\Type.
4
 */
5
6
namespace garethp\ews\API;
7
8
/**
9
 * Base class for Exchange Web Service Types.
10
 *
11
 * @package php-ews\Type
12
 */
13
class Type
14
{
15
    use MagicMethodsTrait;
16
17
    /**
18
     * @var string
19
     */
20
    public $_ = '';
21
22
    public $_value = null;
23
24 32
    public function getNonNullItems($includeHiddenValue = false)
25
    {
26 32
        $items = get_object_vars($this);
27
28 32
        foreach ($items as $key => $item) {
29 32
            if (substr($key, 0, 1) == "_" || $item === null) {
30 32
                unset($items[$key]);
31 32
            }
32 32
        }
33
34 32
        if ($includeHiddenValue && $this->_value !== null) {
35 1
            $items['_value'] = $this->_value;
36 1
        }
37
38 32
        return $items;
39
    }
40
41
    /**
42
     * @param $array
43
     * @return static
44
     */
45 37
    public static function buildFromArray($array)
46
    {
47 37
        if (!is_array($array)) {
48 17
            return $array;
49
        }
50
51 37
        if (!self::arrayIsAssoc($array)) {
52 18
            return self::buildArrayFromArray($array);
0 ignored issues
show
Bug Best Practice introduced by
The expression return self::buildArrayFromArray($array) returns the type array which is incompatible with the documented return type garethp\ews\API\Type.
Loading history...
53
        } else {
54 37
            return self::buildObjectFromArray($array);
55
        }
56
    }
57
58 37
    protected static function buildObjectFromArray($array)
59
    {
60 37
        $object = new static();
61 37
        foreach ($array as $key => $value) {
62 37
            if (is_array($value)) {
63 32
                $value = self::buildFromArray($value);
64 32
            }
65
66
            //I think _value is a more expressive way to set string value, but Soap needs _
67 37
            if ($key === "_value") {
68 5
                $key = "_";
69 5
            }
70
71 37
            $object->$key = $value;
72 37
        }
73
74 37
        return $object;
75
    }
76
77 18
    public static function buildArrayFromArray($array)
78
    {
79 18
        foreach ($array as $key => $value) {
80 18
            $array[$key] = self::buildFromArray($value);
81 18
        }
82
83 18
        return $array;
84
    }
85
86 31
    public function toXmlObject()
87
    {
88 31
        $objectToReturn = new self();
89 31
        $objectToReturn->_ = (string) $this;
90
91 31
        $properties = $this->getNonNullItems(true);
92
93 31
        foreach ($properties as $name => $property) {
94
            //I think _value is a more expressive way to set string value, but Soap needs _
95 31
            if ($name == "_value") {
96
                $name = "_";
97
            }
98
99 31
            $name = ucfirst($name);
100 31
            $objectToReturn->$name = $this->propertyToXml($name, $property);
101 31
        }
102
103 31
        return $objectToReturn;
104
    }
105
106
    /**
107
     * @param $name
108
     * @param $property
109
     * @return array|Type|null
110
     */
111 31
    protected function propertyToXml($name, $property)
112
    {
113 31
        if (isset($this->_typeMap[lcfirst($name)])) {
114
            return $this->castToExchange($property, $this->_typeMap[lcfirst($name)]);
115
        }
116
117 31
        if ($property instanceof Type) {
118 31
            return $property->toXmlObject();
119
        }
120
121 31
        if (is_array($property) && $this->arrayIsAssoc($property)) {
122
            return $this->buildFromArray($property);
123
        }
124
125 31
        if (is_array($property)) {
126 16
            return array_map(function ($property) {
127 16
                if ($property instanceof Type) {
128 16
                    return $property->toXmlObject();
129
                }
130
131
                return $property;
132 16
            }, $property);
133
        }
134
135 31
        return $property;
136
    }
137
138 43
    public static function arrayIsAssoc($array)
139
    {
140 43
        return (bool) count(array_filter(array_keys($array), 'is_string'));
141
    }
142
143
    /**
144
     * Clones any object properties on a type object when it is cloned. Allows
145
     * for a deep clone required when using object to represent data types when
146
     * making a SOAP call.
147
     */
148 1
    public function __clone()
149
    {
150
        // Iterate over all properties on the current object.
151 1
        foreach (get_object_vars($this) as $property => $value) {
152 1
            $this->$property = \garethp\ews\Utilities\cloneValue($value);
153 1
        }
154 1
    }
155
156 36
    public function __toString()
157
    {
158 36
        if (!is_string($this->_)) {
0 ignored issues
show
introduced by
The condition is_string($this->_) is always true.
Loading history...
159 4
            return '';
160
        }
161
162 32
        return $this->_;
163
    }
164
}
165