Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
19 / 19
CRAP
100.00% covered (success)
100.00%
1 / 1
ArrayObject
100.00% covered (success)
100.00%
32 / 32
100.00% covered (success)
100.00%
19 / 19
31
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
8
 createFromJson
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 createFromSerialized
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 count
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getIterator
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonSerialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 jsonUnserialize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 serialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 unserialize
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
3
 __serialize
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unserialize
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __set
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 __get
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
 __isset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 __unset
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
 offsetSet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetGet
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetExists
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 offsetUnset
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Pop PHP Framework (http://www.popphp.org/)
4 *
5 * @link       https://github.com/popphp/popphp-framework
6 * @author     Nick Sagona, III <dev@nolainteractive.com>
7 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
8 * @license    http://www.popphp.org/license     New BSD License
9 */
10
11/**
12 * @namespace
13 */
14namespace Pop\Utils;
15
16use ArrayAccess;
17use Countable;
18use IteratorAggregate;
19
20/**
21 * Pop utils array object class
22 *
23 * @category   Pop
24 * @package    Pop\Utils
25 * @author     Nick Sagona, III <dev@nolainteractive.com>
26 * @copyright  Copyright (c) 2009-2024 NOLA Interactive, LLC. (http://www.nolainteractive.com)
27 * @license    http://www.popphp.org/license     New BSD License
28 * @version    2.0.0
29 */
30class ArrayObject extends AbstractArray implements ArrayAccess, Countable, IteratorAggregate, SerializableInterface, JsonableInterface
31{
32
33    /**
34     * Constructor
35     *
36     * Instantiate the array object
37     *
38     * @param mixed|null $data
39     * @throws Exception
40     */
41    public function __construct(mixed $data = null)
42    {
43        if (($data !== null) && !is_array($data) && !($data instanceof self) && !($data instanceof \ArrayObject) &&
44            !($data instanceof \ArrayAccess) && !($data instanceof \Countable) && !($data instanceof \IteratorAggregate)) {
45            throw new Exception('Error: The data passed must be an array or an array-like object.');
46        }
47        $this->data = $data;
48    }
49
50    /**
51     * Create array object from JSON string
52     *
53     * @param  string $jsonString
54     * @param  int    $depth
55     * @param  int    $options
56     * @return ArrayObject
57     */
58    public static function createFromJson(string $jsonString, int $depth = 512, int $options = 0): ArrayObject
59    {
60        return (new self())->jsonUnserialize($jsonString, $depth, $options);
61    }
62
63    /**
64     * Create array object from serialized string
65     *
66     * @param string $string
67     * @return ArrayObject
68     * @throws Exception
69     */
70    public static function createFromSerialized(string $string): ArrayObject
71    {
72        return (new self())->unserialize($string);
73    }
74
75    /**
76     * Method to get the count of the array object
77     *
78     * @return int
79     */
80    public function count(): int
81    {
82        return count($this->data);
83    }
84
85    /**
86     * Method to iterate over the array object
87     *
88     * @return \ArrayIterator
89     */
90    public function getIterator(): \ArrayIterator
91    {
92        return new \ArrayIterator($this->data);
93    }
94
95    /**
96     * JSON serialize the array object
97     *
98     * @param  int $options
99     * @param  int $depth
100     * @return string
101     */
102    public function jsonSerialize(int $options = 0, int $depth = 512): string
103    {
104        return json_encode($this->toArray(), $options, $depth);
105    }
106
107    /**
108     * Unserialize a JSON string
109     *
110     * @param  string $jsonString
111     * @param  int    $depth
112     * @param  int    $options
113     * @return ArrayObject
114     */
115    public function jsonUnserialize(string $jsonString, int $depth = 512, int $options = 0): ArrayObject
116    {
117        $this->data = json_decode($jsonString, true, $depth, $options);
118        return $this;
119    }
120
121    /**
122     * Serialize the array object
123     *
124     * @param  bool $self
125     * @return string
126     */
127    public function serialize(bool $self = false): string
128    {
129        return ($self)? serialize($this) : serialize($this->toArray());
130    }
131
132    /**
133     * Unserialize a string
134     *
135     * @param  string $string
136     * @throws Exception
137     * @return ArrayObject
138     */
139    public function unserialize(string $string): ArrayObject
140    {
141        $data = @unserialize($string);
142        if ($data instanceof ArrayObject) {
143            return $data;
144        } else if (is_array($data)) {
145            $this->data = $data;
146            return $this;
147        } else {
148            throw new Exception('Error: The string was not able to be correctly unserialized.');
149        }
150    }
151
152    /**
153     * Serialize magic method
154     *
155     * @return mixed
156     */
157    public function __serialize()
158    {
159        return $this->data;
160    }
161
162    /**
163     * Unserialize magic method
164     *
165     * @param  array $data
166     * @return ArrayObject
167     */
168    public function __unserialize(array $data)
169    {
170        $this->data = $data;
171        return $this;
172    }
173
174    /**
175     * Set a value
176     *
177     * @param  string $name
178     * @param  mixed $value
179     * @return ArrayObject
180     */
181    public function __set(string $name, mixed $value)
182    {
183        $this->data[$name] = $value;
184        return $this;
185    }
186
187    /**
188     * Get a value
189     *
190     * @param  string $name
191     * @return mixed
192     */
193    public function __get(string $name): mixed
194    {
195        return (array_key_exists($name, (array)$this->data)) ? $this->data[$name] : null;
196    }
197
198    /**
199     * Is value set
200     *
201     * @param  string $name
202     * @return bool
203     */
204    public function __isset(string $name): bool
205    {
206        return array_key_exists($name, (array)$this->data);
207    }
208
209    /**
210     * Unset a value
211     *
212     * @param  string $name
213     * @return void
214     */
215    public function __unset(string $name): void
216    {
217        if (array_key_exists($name, (array)$this->data)) {
218            unset($this->data[$name]);
219        }
220    }
221
222    /**
223     * ArrayAccess offsetSet
224     *
225     * @param  mixed $offset
226     * @param  mixed $value
227     * @return void
228     */
229    public function offsetSet(mixed $offset, mixed $value): void
230    {
231        $this->__set($offset, $value);
232    }
233
234    /**
235     * ArrayAccess offsetGet
236     *
237     * @param  mixed $offset
238     * @return mixed
239     */
240    public function offsetGet(mixed $offset): mixed
241    {
242        return $this->__get($offset);
243    }
244
245    /**
246     * ArrayAccess offsetExists
247     *
248     * @param  mixed $offset
249     * @return bool
250     */
251    public function offsetExists(mixed $offset): bool
252    {
253        return $this->__isset($offset);
254    }
255
256    /**
257     * ArrayAccess offsetUnset
258     *
259     * @param  mixed $offset
260     * @return void
261     */
262    public function offsetUnset(mixed $offset): void
263    {
264        $this->__unset($offset);
265    }
266
267}