Completed
Push — master ( 059a7f...25fd12 )
by Oscar
02:16
created

Document   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Importance

Changes 6
Bugs 1 Features 1
Metric Value
wmc 16
c 6
b 1
f 1
lcom 0
cbo 0
dl 0
loc 106
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
A __get() 0 6 1
A __set() 0 4 1
A __isset() 0 4 1
A __unset() 0 4 1
A jsonSerialize() 0 4 1
A getArrayCopy() 0 4 1
B arrayToObject() 0 16 5
A objectToArray() 0 10 4
1
<?php
2
3
namespace FlyCrud;
4
5
use ArrayObject;
6
use JsonSerializable;
7
8
class Document extends ArrayObject implements JsonSerializable
9
{
10
    public function __construct(array $value)
11
    {
12
        parent::__construct(self::arrayToObject($value));
13
    }
14
15
    /**
16
     * @param string $name
17
     * 
18
     * @return mixed
19
     */
20
    public function &__get($name)
21
    {
22
        $value = $this->offsetGet($name);
23
24
        return $value;
25
    }
26
27
    /**
28
     * @param string $name
29
     * @param mixed  $value
30
     */
31
    public function __set($name, $value)
32
    {
33
        $this->offsetSet($name, $value);
34
    }
35
36
    /**
37
     * @param string $name
38
     */
39
    public function __isset($name)
40
    {
41
        return $this->offsetExists($name);
42
    }
43
44
    /**
45
     * @param string $name
46
     */
47
    public function __unset($name)
48
    {
49
        $this->offsetUnset($name);
50
    }
51
52
    /**
53
     * @see JsonSerializable
54
     *
55
     * @return array
56
     */
57
    public function jsonSerialize()
58
    {
59
        return $this->getArrayCopy();
60
    }
61
62
    /**
63
     * Returns the document data.
64
     * 
65
     * @return self
66
     */
67
    public function getArrayCopy()
68
    {
69
        return self::objectToArray(parent::getArrayCopy());
70
    }
71
72
    /**
73
     * Converts the associative arrays to stdClass object recursively.
74
     * 
75
     * @param array $array
76
     * 
77
     * @return array|stdClass
78
     */
79
    private static function arrayToObject(array $array)
80
    {
81
        $is_object = false;
82
83
        foreach ($array as $key => &$value) {
84
            if (is_array($value)) {
85
                $value = self::arrayToObject($value);
86
            }
87
88
            if (is_string($key)) {
89
                $is_object = true;
90
            }
91
        }
92
93
        return $is_object ? (object) $array : $array;
94
    }
95
96
    /**
97
     * Converts stdClass objects to arrays recursively.
98
     * 
99
     * @param array $array
100
     * 
101
     * @return array
102
     */
103
    private static function objectToArray(array $array)
104
    {
105
        foreach ($array as $key => &$value) {
106
            if (is_object($value) || is_array($value)) {
107
                $value = self::objectToArray((array) $value);
108
            }
109
        }
110
111
        return (array) $array;
112
    }
113
}
114