| @@ -6,25 +6,176 @@ | ||
| 6 | 6 | |
| 7 | 7 | class JSONParser extends AbstractParser implements ParserInterface |
| 8 | 8 | { |
| 9 | 9 | /** |
| 10 | - * JSON Record | |
| 10 | + * Decoded JSON record. | |
| 11 | 11 | * |
| 12 | - * @var array | |
| 12 | + * @var array|null | |
| 13 | 13 | */ |
| 14 | 14 | private $json_record; |
| 15 | 15 | |
| 16 | - public function query($query) | |
| 16 | + /** | |
| 17 | + * Active sub-record when expanding a group base. | |
| 18 | + * | |
| 19 | + * @var array|null | |
| 20 | + */ | |
| 21 | + private $query_base_record; | |
| 22 | + | |
| 23 | + /** | |
| 24 | + * Query a value from the current JSON record. | |
| 25 | + * | |
| 26 | + * Paths are slash-delimited relative to the record root, e.g. "/id", "/author/name". | |
| 27 | + * Legacy flat keys without a leading slash are also supported. | |
| 28 | + * | |
| 29 | + * @param string $query | |
| 30 | + * @param bool $as_string | |
| 31 | + * @return mixed | |
| 32 | + */ | |
| 33 | + public function query($query, $as_string = true) | |
| 17 | 34 | { |
| 18 | - return isset($this->json_record[$query]) ? $this->json_record[$query] : ''; | |
| 35 | + $value = $this->resolvePath($query, $this->getActiveRecord()); | |
| 36 | + | |
| 37 | + if ($value === null || $value === false) { | |
| 38 | + return $as_string ? '' : false; | |
| 39 | + } | |
| 40 | + | |
| 41 | + if (!$as_string) { | |
| 42 | + return $value; | |
| 43 | + } | |
| 44 | + | |
| 45 | + if (is_array($value)) { | |
| 46 | + if ($this->isList($value)) { | |
| 47 | + $parts = []; | |
| 48 | + foreach ($value as $item) { | |
| 49 | + if (is_scalar($item) || $item === null) { | |
| 50 | + $parts[] = (string) $item; | |
| 51 | + } else { | |
| 52 | + $parts[] = wp_json_encode($item); | |
| 53 | + } | |
| 54 | + } | |
| 55 | + return implode(',', $parts); | |
| 56 | + } | |
| 57 | + | |
| 58 | + return wp_json_encode($value); | |
| 59 | + } | |
| 60 | + | |
| 61 | + return (string) $value; | |
| 19 | 62 | } |
| 20 | 63 | |
| 64 | + /** | |
| 65 | + * Query a group of JSON data, optionally expanding a nested array via base. | |
| 66 | + * | |
| 67 | + * @param array $group | |
| 68 | + * @return array | |
| 69 | + */ | |
| 70 | + public function queryGroup($group) | |
| 71 | + { | |
| 72 | + if (isset($group['base']) && $group['base'] !== '') { | |
| 73 | + $output = []; | |
| 74 | + $sub_records = $this->query($group['base'], false); | |
| 75 | + | |
| 76 | + if (!is_array($sub_records)) { | |
| 77 | + return []; | |
| 78 | + } | |
| 79 | + | |
| 80 | + if (!$this->isList($sub_records)) { | |
| 81 | + $sub_records = [$sub_records]; | |
| 82 | + } | |
| 83 | + | |
| 84 | + foreach ($sub_records as $record) { | |
| 85 | + if (!is_array($record)) { | |
| 86 | + continue; | |
| 87 | + } | |
| 88 | + $this->query_base_record = $record; | |
| 89 | + $output[] = parent::queryGroup($group); | |
| 90 | + } | |
| 91 | + $this->query_base_record = null; | |
| 92 | + | |
| 93 | + return $output; | |
| 94 | + } | |
| 95 | + | |
| 96 | + return parent::queryGroup($group); | |
| 97 | + } | |
| 98 | + | |
| 21 | 99 | protected function onRecordLoaded() |
| 22 | 100 | { |
| 23 | 101 | $this->json_record = json_decode($this->record, true); |
| 102 | + $this->query_base_record = null; | |
| 103 | + | |
| 104 | + if (!is_array($this->json_record)) { | |
| 105 | + $this->json_record = []; | |
| 106 | + } | |
| 24 | 107 | } |
| 25 | 108 | |
| 26 | 109 | public function record() |
| 27 | 110 | { |
| 28 | 111 | return $this->json_record; |
| 112 | + } | |
| 113 | + | |
| 114 | + /** | |
| 115 | + * @return array|null | |
| 116 | + */ | |
| 117 | + private function getActiveRecord() | |
| 118 | + { | |
| 119 | + if (is_array($this->query_base_record)) { | |
| 120 | + return $this->query_base_record; | |
| 121 | + } | |
| 122 | + | |
| 123 | + return $this->json_record; | |
| 124 | + } | |
| 125 | + | |
| 126 | + /** | |
| 127 | + * Resolve a slash path against a record array. | |
| 128 | + * | |
| 129 | + * @param string $query | |
| 130 | + * @param array|null $record | |
| 131 | + * @return mixed | |
| 132 | + */ | |
| 133 | + private function resolvePath($query, $record) | |
| 134 | + { | |
| 135 | + if (!is_array($record)) { | |
| 136 | + return null; | |
| 137 | + } | |
| 138 | + | |
| 139 | + $query = (string) $query; | |
| 140 | + if ($query === '' || $query === '/') { | |
| 141 | + return $record; | |
| 142 | + } | |
| 143 | + | |
| 144 | + if (strpos($query, '/') === 0) { | |
| 145 | + $query = substr($query, 1); | |
| 146 | + } | |
| 147 | + | |
| 148 | + // Legacy flat key: no slashes | |
| 149 | + if (strpos($query, '/') === false) { | |
| 150 | + return array_key_exists($query, $record) ? $record[$query] : null; | |
| 151 | + } | |
| 152 | + | |
| 153 | + $segments = explode('/', $query); | |
| 154 | + $current = $record; | |
| 155 | + | |
| 156 | + foreach ($segments as $segment) { | |
| 157 | + if ($segment === '') { | |
| 158 | + continue; | |
| 159 | + } | |
| 160 | + if (!is_array($current) || !array_key_exists($segment, $current)) { | |
| 161 | + return null; | |
| 162 | + } | |
| 163 | + $current = $current[$segment]; | |
| 164 | + } | |
| 165 | + | |
| 166 | + return $current; | |
| 167 | + } | |
| 168 | + | |
| 169 | + /** | |
| 170 | + * @param array $data | |
| 171 | + * @return bool | |
| 172 | + */ | |
| 173 | + private function isList(array $data) | |
| 174 | + { | |
| 175 | + if ($data === []) { | |
| 176 | + return true; | |
| 177 | + } | |
| 178 | + | |
| 179 | + return array_keys($data) === range(0, count($data) - 1); | |
| 29 | 180 | } |
| 30 | 181 | } |