1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* This file is part of the O2System Framework package.
|
4
|
|
|
*
|
5
|
|
|
* For the full copyright and license information, please view the LICENSE
|
6
|
|
|
* file that was distributed with this source code.
|
7
|
|
|
*
|
8
|
|
|
* @author Steeve Andrian Salim
|
9
|
|
|
* @copyright Copyright (c) Steeve Andrian Salim
|
10
|
|
|
*/
|
11
|
|
|
|
12
|
|
|
// ------------------------------------------------------------------------
|
13
|
|
|
|
14
|
|
|
namespace O2System\Spl\DataStructures\Traits;
|
15
|
|
|
|
16
|
|
|
// ------------------------------------------------------------------------
|
17
|
|
|
|
18
|
|
|
use O2System\Spl\DataStructures\SplArrayObject;
|
19
|
|
|
|
20
|
|
|
/**
|
21
|
|
|
* Trait ArrayConversionTrait
|
22
|
|
|
*
|
23
|
|
|
* Add re-usable ArrayObject conversion methods
|
24
|
|
|
*
|
25
|
|
|
* @package O2System\Spl\Traits
|
26
|
|
|
*/
|
27
|
|
|
trait ArrayConversionTrait
|
28
|
|
|
{
|
29
|
|
|
/**
|
30
|
|
|
* ArrayConversionTrait::__toObject
|
31
|
|
|
*
|
32
|
|
|
* Convert storage array into ArrayObject
|
33
|
|
|
*
|
34
|
|
|
* @param int $depth The depth of the conversion
|
35
|
|
|
*
|
36
|
|
|
* @return SplArrayObject
|
37
|
|
|
*/
|
38
|
|
|
public function __toObject($depth = 0)
|
39
|
|
|
{
|
40
|
|
|
return $this->___toObjectIterator($this->getArrayCopy(), ($depth == 0 ? 'ALL' : $depth));
|
|
|
|
|
41
|
|
|
}
|
42
|
|
|
|
43
|
|
|
// --------------------------------------------------------------------
|
44
|
|
|
|
45
|
|
|
/**
|
46
|
|
|
* ArrayConversionTrait::__toObjectIterator
|
47
|
|
|
*
|
48
|
|
|
* Iterate storage array into object
|
49
|
|
|
*
|
50
|
|
|
* @param array $array The array copy
|
51
|
|
|
* @param string $depth The depth of the conversion
|
52
|
|
|
* @param int $counter Internal iterator counter
|
53
|
|
|
*
|
54
|
|
|
* @return SplArrayObject
|
55
|
|
|
*/
|
56
|
|
|
private function ___toObjectIterator($array, $depth = 'ALL', $counter = 0)
|
57
|
|
|
{
|
58
|
|
|
$object = new SplArrayObject();
|
59
|
|
|
|
60
|
|
|
if ($this->count() > 0) {
|
|
|
|
|
61
|
|
|
foreach ($array as $key => $value) {
|
62
|
|
|
if (strlen($key)) {
|
63
|
|
|
if (is_array($value)) {
|
64
|
|
|
if ($depth == 'ALL') {
|
65
|
|
|
$object->offsetSet($key, $this->___toObjectIterator($value, $depth));
|
66
|
|
|
} elseif (is_numeric($depth)) {
|
67
|
|
|
if ($counter != $depth) {
|
68
|
|
|
$object->offsetSet($key, $this->___toObjectIterator($value, $depth, $counter));
|
69
|
|
|
} else {
|
70
|
|
|
$object->offsetSet($key, $value);
|
71
|
|
|
}
|
72
|
|
|
} elseif (is_string($depth) && $key == $depth) {
|
73
|
|
|
$object->offsetSet($key, $value);
|
74
|
|
|
} elseif (is_array($depth) && in_array($key, $depth)) {
|
75
|
|
|
$object->offsetSet($key, $value);
|
76
|
|
|
} else {
|
77
|
|
|
$object->offsetSet($key, $this->___toObjectIterator($value, $depth));
|
78
|
|
|
}
|
79
|
|
|
} else {
|
80
|
|
|
$object->offsetSet($key, $value);
|
81
|
|
|
}
|
82
|
|
|
}
|
83
|
|
|
}
|
84
|
|
|
}
|
85
|
|
|
|
86
|
|
|
return $object;
|
87
|
|
|
}
|
88
|
|
|
|
89
|
|
|
// --------------------------------------------------------------------
|
90
|
|
|
|
91
|
|
|
/**
|
92
|
|
|
* ArrayConversionTrait::__toString
|
93
|
|
|
*
|
94
|
|
|
* Returning JSON Encode array copy of the storage
|
95
|
|
|
*
|
96
|
|
|
* @return string
|
97
|
|
|
*/
|
98
|
|
|
public function __toString()
|
99
|
|
|
{
|
100
|
|
|
if (method_exists($this, 'render')) {
|
101
|
|
|
return $this->render();
|
102
|
|
|
}
|
103
|
|
|
|
104
|
|
|
return $this->__toJSON();
|
105
|
|
|
}
|
106
|
|
|
|
107
|
|
|
// ------------------------------------------------------------------------
|
108
|
|
|
|
109
|
|
|
/**
|
110
|
|
|
* ArrayConversionTrait::__toJSON
|
111
|
|
|
*
|
112
|
|
|
* @see http://php.net/manual/en/function.json-encode.php
|
113
|
|
|
*
|
114
|
|
|
* @param int $options JSON encode options, default JSON_PRETTY_PRINT
|
115
|
|
|
* @param int $depth Maximum depth of JSON encode. Must be greater than zero.
|
116
|
|
|
*
|
117
|
|
|
* @return string
|
118
|
|
|
*/
|
119
|
|
|
public function __toJson($options = JSON_PRETTY_PRINT, $depth = 512)
|
120
|
|
|
{
|
121
|
|
|
$depth = $depth == 0 ? 512 : $depth;
|
122
|
|
|
|
123
|
|
|
return call_user_func_array('json_encode', [$this->getArrayCopy(), $options, $depth]);
|
124
|
|
|
}
|
125
|
|
|
|
126
|
|
|
// --------------------------------------------------------------------
|
127
|
|
|
|
128
|
|
|
/**
|
129
|
|
|
* ArrayConversionTrait::__toSerialize
|
130
|
|
|
*
|
131
|
|
|
* Convert rows into PHP serialize array
|
132
|
|
|
*
|
133
|
|
|
* @see http://php.net/manual/en/function.serialize.php
|
134
|
|
|
*
|
135
|
|
|
* @return string
|
136
|
|
|
*/
|
137
|
|
|
public function __toSerialize()
|
138
|
|
|
{
|
139
|
|
|
return serialize($this->__toArray());
|
140
|
|
|
}
|
141
|
|
|
|
142
|
|
|
// --------------------------------------------------------------------
|
143
|
|
|
|
144
|
|
|
/**
|
145
|
|
|
* ArrayConversionTrait::__toArray
|
146
|
|
|
*
|
147
|
|
|
* Returning array copy of the storage
|
148
|
|
|
*
|
149
|
|
|
* @return string
|
150
|
|
|
*/
|
151
|
|
|
public function __toArray()
|
152
|
|
|
{
|
153
|
|
|
return $this->getArrayCopy();
|
154
|
|
|
}
|
155
|
|
|
|
156
|
|
|
// --------------------------------------------------------------------
|
157
|
|
|
|
158
|
|
|
/**
|
159
|
|
|
* ArrayConversionTrait::implode
|
160
|
|
|
*
|
161
|
|
|
* Join the storage elements with a string
|
162
|
|
|
*
|
163
|
|
|
* @param string $glue Defaults to an empty string.
|
164
|
|
|
*
|
165
|
|
|
* @return string
|
166
|
|
|
*/
|
167
|
|
|
public function implode($glue = '')
|
168
|
|
|
{
|
169
|
|
|
return implode($glue, $this->getArrayCopy());
|
170
|
|
|
}
|
171
|
|
|
|
172
|
|
|
// --------------------------------------------------------------------
|
173
|
|
|
|
174
|
|
|
/**
|
175
|
|
|
* ArrayConversionTrait::join
|
176
|
|
|
*
|
177
|
|
|
* Join the storage elements with a string
|
178
|
|
|
*
|
179
|
|
|
* @param string $glue Defaults to an empty string.
|
180
|
|
|
*
|
181
|
|
|
* @return string
|
182
|
|
|
*/
|
183
|
|
|
public function join($glue = '')
|
184
|
|
|
{
|
185
|
|
|
return join($glue, $this->getArrayCopy());
|
186
|
|
|
}
|
187
|
|
|
} |