Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
9 / 9
CRAP
100.00% covered (success)
100.00%
1 / 1
ArrayObject
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
9 / 9
19
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
 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%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3/**
4 * Pop PHP Framework (https://www.popphp.org/)
5 *
6 * @link       https://github.com/popphp/popphp-framework
7 * @author     Nick Sagona, III <nick@popphp.org>
8 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
9 * @license    https://www.popphp.org/license     New BSD License
10 */
11
12/**
13 * @namespace
14 */
15namespace Pop\Utils;
16
17/**
18 * Pop utils array object class
19 *
20 * @category   Pop
21 * @package    Pop\Utils
22 * @author     Nick Sagona, III <nick@popphp.org>
23 * @copyright  Copyright (c) 2009-2026 Nick Sagona, III
24 * @license    https://www.popphp.org/license     New BSD License
25 * @version    3.0.0
26 */
27class ArrayObject extends AbstractArray implements SerializableInterface, JsonableInterface
28{
29
30    /**
31     * Constructor
32     *
33     * Instantiate the array object
34     *
35     * @param mixed|null $data
36     * @throws Exception
37     */
38    public function __construct(mixed $data = null)
39    {
40        if (($data !== null) && !is_array($data) && !($data instanceof self) && !($data instanceof \ArrayObject) &&
41            !($data instanceof \ArrayAccess) && !($data instanceof \Countable) && !($data instanceof \IteratorAggregate)) {
42            throw new Exception('Error: The data passed must be an array or an array-like object.');
43        }
44        $this->data = $data;
45    }
46
47    /**
48     * Create array object from JSON string
49     *
50     * @param  string $jsonString
51     * @param  int    $depth
52     * @param  int    $options
53     * @return ArrayObject
54     */
55    public static function createFromJson(string $jsonString, int $depth = 512, int $options = 0): ArrayObject
56    {
57        return (new self())->jsonUnserialize($jsonString, $depth, $options);
58    }
59
60    /**
61     * Create array object from serialized string
62     *
63     * @param string $string
64     * @return ArrayObject
65     * @throws Exception
66     */
67    public static function createFromSerialized(string $string): ArrayObject
68    {
69        return (new self())->unserialize($string);
70    }
71
72    /**
73     * JSON serialize the array object
74     *
75     * @param  int $options
76     * @param  int $depth
77     * @return string
78     */
79    public function jsonSerialize(int $options = 0, int $depth = 512): string
80    {
81        return json_encode($this->toArray(), $options, $depth);
82    }
83
84    /**
85     * Unserialize a JSON string
86     *
87     * @param  string $jsonString
88     * @param  int    $depth
89     * @param  int    $options
90     * @return ArrayObject
91     */
92    public function jsonUnserialize(string $jsonString, int $depth = 512, int $options = 0): ArrayObject
93    {
94        $this->data = json_decode($jsonString, true, $depth, $options);
95        return $this;
96    }
97
98    /**
99     * Serialize the array object
100     *
101     * @param  bool $self
102     * @return string
103     */
104    public function serialize(bool $self = false): string
105    {
106        return ($self)? serialize($this) : serialize($this->toArray());
107    }
108
109    /**
110     * Unserialize a string
111     *
112     * @param  string $string
113     * @throws Exception
114     * @return ArrayObject
115     */
116    public function unserialize(string $string): ArrayObject
117    {
118        $data = @unserialize($string);
119        if ($data instanceof ArrayObject) {
120            return $data;
121        } else if (is_array($data)) {
122            $this->data = $data;
123            return $this;
124        } else {
125            throw new Exception('Error: The string was not able to be correctly unserialized.');
126        }
127    }
128
129    /**
130     * Serialize magic method
131     *
132     * @return mixed
133     */
134    public function __serialize()
135    {
136        return $this->data;
137    }
138
139    /**
140     * Unserialize magic method
141     *
142     * @param  array $data
143     * @return void
144     */
145    public function __unserialize(array $data): void
146    {
147        $this->data = $data;
148    }
149
150}