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