Completed
Push — master ( 8ff8a5...9a5f44 )
by Luka
05:09
created

ObjectArray   A

Complexity

Total Complexity 24

Size/Duplication

Total Lines 141
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 24
c 0
b 0
f 0
lcom 1
cbo 1
dl 0
loc 141
ccs 0
cts 77
cp 0
rs 10

11 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 2
A fromArray() 0 13 4
A toArray() 0 9 2
C toArrayRec() 0 22 7
A getObjects() 0 4 1
A count() 0 4 1
A offsetExists() 0 4 1
A offsetGet() 0 8 2
A offsetSet() 0 8 2
A offsetUnset() 0 4 1
A getIterator() 0 4 1
1
<?php
2
3
namespace Teto\Object;
4
5
/**
6
 * Interface for array compatible object
7
 *
8
 * @author    USAMI Kenta <[email protected]>
9
 * @copyright 2016 Baguette HQ
10
 * @license   http://www.apache.org/licenses/LICENSE-2.0
11
 */
12
class ObjectArray implements \ArrayAccess, \Countable, \IteratorAggregate, ToArrayInterface
13
{
14
    private $objects = [];
15
16
    /**
17
     * @param object
18
     */
19
    public function __construct()
20
    {
21
        $this->objects = func_get_args() ?: [];
22
    }
23
24
    /**
25
     * @param  ObjectArray|object[] $objects
26
     * @return ObjectArray
27
     * @throws InvalidArgumentException
28
     */
29
    public static function fromArray($objects)
30
    {
31
        if ($objects instanceof ObjectArray) {
32
            return $objects;
33
        } elseif (!is_array($objects) && !$objects instanceof ToArrayInterface) {
34
            throw new \InvalidArgumentException;
35
        }
36
37
        $model_array = new ObjectArray;
38
        $model_array->objects = array_values($objects);
39
40
        return $model_array;
41
    }
42
43
    /**
44
     * @return array
45
     */
46
    public function toArray()
47
    {
48
        $arrays = [];
49
        foreach ($this->objects as $k => $object) {
50
            $arrays[$k] = self::toArrayRec($object);
51
        }
52
53
        return $arrays;
54
    }
55
56
    /**
57
     * Convert elements to recursive array
58
     *
59
     * @param mixed $object
60
     * @param int   $rec    ネストの深さ
61
     */
62
    public static function toArrayRec($object, $rec = 5)
63
    {
64
        if ($rec < 1
65
            || empty($object)
66
            || is_scalar($object)
67
        ) {
68
            return $object;
69
        }
70
71
        if ($object instanceof ToArrayInterface) {
72
            return $object->toArray();
73
        } elseif (!is_array($object)) {
74
            return $object;
75
        }
76
77
        $retval = [];
78
        foreach ($object as $idx => $obj) {
79
            $retval[$idx] = self::toArrayRec($obj, $rec - 1);
80
        }
81
82
        return $retval;
83
    }
84
85
    public function getObjects()
86
    {
87
        require $this->objects;
88
    }
89
90
    /**
91
     * @return int
92
     */
93
    public function count()
94
    {
95
        return count($this->objects);
96
    }
97
98
    /**
99
     * @param  mixed offset
100
     * @return bool
101
     */
102
    public function offsetExists($offset)
103
    {
104
        return !empty($this->objects[$offset]);
105
    }
106
107
    /**
108
     * @param  mixed offset
109
     * @return mixed
110
     * @throws OutOfBoundsException
111
     */
112
    public function offsetGet($offset)
113
    {
114
        if (!array_key_exists($offset, $this->objects)) {
115
            throw new \OutOfBoundsException("Undefined offset: {$offset}");
116
        }
117
118
        return $this->objects[$offset];
119
    }
120
121
    /**
122
     * @param  mixed offset
123
     * @param  mixed value
124
     * @return void
125
     * @throws OutOfBoundsException
126
     */
127
    public function offsetSet($offset, $value)
128
    {
129
        if ($offset === null) {
130
            $this->objects[] = $value;
131
        } else {
132
            $this->objects[$offset] = $value;
133
        }
134
    }
135
136
    /**
137
     * @param  mixed offset
138
     * @return void
139
     */
140
    public function offsetUnset($offset)
141
    {
142
        unset($this->objects[$offset]);
143
    }
144
145
    /**
146
     * @return \ArrayIterator
147
     */
148
    public function getIterator()
149
    {
150
        return new \ArrayIterator($this->objects);
151
    }
152
}
153