DataType::__clone()   B
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 9

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 26
rs 8.439
cc 6
eloc 9
nc 6
nop 0
1
<?php
2
3
namespace PhpEws;
4
5
/**
6
 * Base class for Exchange Web Service Types
7
 */
8
abstract class DataType
9
{
10
    /**
11
     * Clones any object properties on a type object when it is cloned. Allows
12
     * for a deep clone required when using object to represent data types when
13
     * making a SOAP call.
14
     */
15
    public function __clone()
16
    {
17
        // Iterate over all properties on the current object.
18
        foreach (get_object_vars($this) as $property => $value) {
19
            // If the value of the property is an object then clone it.
20
            if (is_object($value)) {
21
                $this->$property = clone $value;
22
            }
23
            elseif (is_array($value)) {
24
                // The value is an array that may use objects as values. Iterate
25
                // over the array and clone any values that are objects into a
26
                // new array.
27
                // For some reason, if we try to set $this->$property to an
28
                // empty array then update it as we go it ends up being empty.
29
                // If we use a new array that we then set as the value of
30
                // $this->$property all is well.
31
                $new_value = array();
32
                foreach ($value as $index => $array_value) {
33
                    $new_value[$index] = (is_object($array_value) ? clone $array_value : $array_value);
34
                }
35
36
                // Set the property to the new array.
37
                $this->$property = $new_value;
38
            }
39
        }
40
    }
41
}
42