Passed
Pull Request — master (#265)
by Gareth
02:07
created

BuildableTrait::arrayIsAssoc()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 1
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
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) {
0 ignored issues
show
introduced by
The condition static::class === garethp\ews\API\Type::class is always false.
Loading history...
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);
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\XmlObjec...ethp\ews\BuildableTrait.
Loading history...
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)]);
0 ignored issues
show
Bug introduced by
It seems like castToExchange() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

111
            return $this->/** @scrutinizer ignore-call */ castToExchange($property, $this->_typeMap[lcfirst($name)]);
Loading history...
112
        }
113
114 6
        if ($property instanceof Type) {
115 6
            return $property->toXmlObject();
0 ignored issues
show
Bug Best Practice introduced by
The expression return $property->toXmlObject() returns the type garethp\ews\API\XmlObject which is incompatible with the documented return type array|garethp\ews\API\Type|null.
Loading history...
116
        }
117
118 6
        if (is_array($property) && $this->arrayIsAssoc($property)) {
119
            return $this->buildFromArray($property);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->buildFromArray($property) returns the type garethp\ews\API\XmlObject which is incompatible with the documented return type array|garethp\ews\API\Type|null.
Loading history...
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->_)) {
0 ignored issues
show
introduced by
The condition is_string($this->_) is always true.
Loading history...
156 4
            return '';
157
        }
158
159 19
        return $this->_;
160
    }
161
}
162