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
|
|
|
|