1 | <?php |
||
8 | class JsonIterator extends GenericIterator |
||
9 | { |
||
10 | |||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | private $jsonObject; |
||
15 | |||
16 | /** |
||
17 | * Enter description here... |
||
18 | * |
||
19 | * @var int |
||
20 | */ |
||
21 | private $current = 0; |
||
22 | |||
23 | /** |
||
24 | * JsonIterator constructor. |
||
25 | * |
||
26 | * @param $jsonObject |
||
27 | * @param string $path |
||
28 | * @param bool $throwErr |
||
29 | * @throws \ByJG\AnyDataset\Exception\IteratorException |
||
30 | */ |
||
31 | 7 | public function __construct($jsonObject, $path = "", $throwErr = false) |
|
32 | { |
||
33 | 7 | if (!is_array($jsonObject)) { |
|
34 | throw new InvalidArgumentException("Invalid JSON object"); |
||
35 | } |
||
36 | |||
37 | 7 | if ($path != "") { |
|
38 | 4 | if ($path[0] == "/") { |
|
39 | 3 | $path = substr($path, 1); |
|
40 | } |
||
41 | |||
42 | 4 | $pathAr = explode("/", $path); |
|
43 | |||
44 | 4 | $newjsonObject = $jsonObject; |
|
45 | |||
46 | 4 | foreach ($pathAr as $key) { |
|
47 | 4 | if (array_key_exists($key, $newjsonObject)) { |
|
48 | 4 | $newjsonObject = $newjsonObject[$key]; |
|
49 | 2 | } elseif ($throwErr) { |
|
50 | 1 | throw new IteratorException("Invalid path '$path' in JSON Object"); |
|
51 | } else { |
||
52 | 1 | $newjsonObject = array(); |
|
53 | 4 | break; |
|
54 | } |
||
55 | } |
||
56 | 3 | $this->jsonObject = $newjsonObject; |
|
57 | } else { |
||
58 | 3 | $this->jsonObject = $jsonObject; |
|
59 | } |
||
60 | |||
61 | 6 | $this->current = 0; |
|
62 | 6 | } |
|
63 | |||
64 | 6 | public function count() |
|
68 | |||
69 | /** |
||
70 | * @access public |
||
71 | * @return bool |
||
72 | */ |
||
73 | 5 | public function hasNext() |
|
81 | |||
82 | /** |
||
83 | * @access public |
||
84 | * @return Row |
||
85 | * @throws \ByJG\AnyDataset\Exception\IteratorException |
||
86 | * @throws \ByJG\Serializer\Exception\InvalidArgumentException |
||
87 | */ |
||
88 | 4 | public function moveNext() |
|
96 | |||
97 | public function key() |
||
101 | } |
||
102 |