ObjectUtils::getAllObjectVarsOrArrayProperties()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 1
1
<?php
2
3
namespace JimmyOak\Utility;
4
5
class ObjectUtils extends UtilsBase
6
{
7
    const SHALLOW = false;
8
    const DEEP = true;
9
10
    /** @var ArrayUtils */
11
    private $arrayUtils;
12
13
    protected function __construct()
14
    {
15
        $this->arrayUtils = ArrayUtils::instance();
16
    }
17
18
    /**
19
     * @param object $objectToParse
20
     * @param bool $deep
21
     *
22
     * @return \SimpleXMLElement
23
     */
24
    public function toXml($objectToParse, $deep = self::SHALLOW)
25
    {
26
        return simplexml_load_string($this->toXmlString($objectToParse, $deep));
27
    }
28
29
    /**
30
     * @param object $object
31
     * @param bool $deep
32
     *
33
     * @return string
34
     */
35
    public function toXmlString($object, $deep = self::SHALLOW)
36
    {
37
        return $this->arrayUtils->toXmlString($this->toArray($object, $deep));
38
    }
39
40
    /**
41
     * @param object $object
42
     * @param bool $deep
43
     *
44
     * @return array
45
     */
46
    public function toArray($object, $deep = self::SHALLOW)
47
    {
48
        return $deep ? $this->toDeepArray($object) : $this->toShallowArray($object);
49
    }
50
51 View Code Duplication
    private function toDeepArray($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
52
    {
53
        $array = [];
54
55
        if ($this->isDateTimeExtensionObject($object)) {
56
            return $this->parseDateTimeExtensionObject($object);
57
        }
58
59
        $vars = $this->getAllObjectVarsOrArrayProperties($object);
60
61
        foreach ($vars as $property => $value) {
62
            if (is_object($value) || is_array($value)) {
63
                $array[$property] = $this->toDeepArray($value);
64
            } elseif (is_resource($value)) {
65
                $array[$property] = (string)$value;
66
            } else {
67
                $array[$property] = $value;
68
            }
69
        }
70
71
        return $array;
72
    }
73
74
    private function isDateTimeExtensionObject($object)
75
    {
76
        return $object instanceof \DateTimeInterface
77
            || $object instanceof \DateInterval
78
            || $object instanceof \DateTimeZone;
79
    }
80
81
    private function parseDateTimeExtensionObject($object)
82
    {
83
        if ($object instanceof \DateTimeInterface) {
84
            return $object->format(\DateTime::ISO8601);
85
        } elseif ($object instanceof \DateInterval) {
86
            return $object->format('P%yY%mM%dDT%hH%iM%sS');
87
        } elseif ($object instanceof \DateTimeZone) {
88
            return $object->getName();
89
        }
90
91
        return (string) $object;
92
    }
93
94
    private function getAllObjectVarsOrArrayProperties($object)
95
    {
96
        if (is_object($object)) {
97
            $vars = $this->getAllObjectVars($object);
98
        } else {
99
            $vars = $object;
100
        }
101
        return $vars;
102
    }
103
104
    private function getAllObjectVars($object)
105
    {
106
        $getObjectVarsClosure = function () {
107
            return get_object_vars($this);
108
        };
109
110
        $vars = [];
111
        $class = get_class($object);
112
        do {
113
            $bindedGetObjectVarsClosure = \Closure::bind($getObjectVarsClosure, $object, $class);
114
            $vars = array_merge($vars, $bindedGetObjectVarsClosure());
115
        } while ($class = get_parent_class($class));
116
117
        return $vars;
118
    }
119
120 View Code Duplication
    private function toShallowArray($object)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
121
    {
122
        $array = [];
123
124
        if ($this->isDateTimeExtensionObject($object)) {
125
            return $this->parseDateTimeExtensionObject($object);
126
        }
127
128
        foreach ($object as $property => $value) {
129
            if (is_object($value) || is_array($value)) {
130
                $array[$property] = $this->toShallowArray($value);
131
            } elseif (is_resource($value)) {
132
                $array[$property] = (string)$value;
133
            } else {
134
                $array[$property] = $value;
135
            }
136
        }
137
138
        return $array;
139
    }
140
}
141