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