| @@ 112-145 (lines=34) @@ | ||
| 109 | * |
|
| 110 | * @return mixed |
|
| 111 | */ |
|
| 112 | public function setValue($data, $keys, $value) |
|
| 113 | { |
|
| 114 | if ($this->isKeysEmpty($keys)) { |
|
| 115 | return $data; |
|
| 116 | } |
|
| 117 | if (!is_array($keys)) { |
|
| 118 | $keys = [$keys]; |
|
| 119 | } |
|
| 120 | ||
| 121 | $accessor = new Accessor($data); |
|
| 122 | $depth = 0; |
|
| 123 | $keyCount = count($keys); |
|
| 124 | foreach ($keys as $key) { |
|
| 125 | if ($depth + 1 === $keyCount) { |
|
| 126 | if ($accessor->set($key, $value) === false) { |
|
| 127 | throw new InvalidArgumentException(sprintf( |
|
| 128 | 'Did not set path %s in structure %s', |
|
| 129 | json_encode($keys), |
|
| 130 | json_encode($data) |
|
| 131 | )); |
|
| 132 | } |
|
| 133 | } elseif ($accessor->to($key) === false) { |
|
| 134 | throw new InvalidArgumentException(sprintf( |
|
| 135 | 'Did not find path %s in structure %s', |
|
| 136 | json_encode($keys), |
|
| 137 | json_encode($data) |
|
| 138 | )); |
|
| 139 | } |
|
| 140 | ||
| 141 | ++$depth; |
|
| 142 | } |
|
| 143 | ||
| 144 | return $accessor->getData(); |
|
| 145 | } |
|
| 146 | ||
| 147 | /** |
|
| 148 | * @param mixed $data |
|
| @@ 182-215 (lines=34) @@ | ||
| 179 | * |
|
| 180 | * @return mixed |
|
| 181 | */ |
|
| 182 | public function removeValue($data, $keys) |
|
| 183 | { |
|
| 184 | if ($this->isKeysEmpty($keys)) { |
|
| 185 | return; |
|
| 186 | } |
|
| 187 | if (!is_array($keys)) { |
|
| 188 | $keys = [$keys]; |
|
| 189 | } |
|
| 190 | ||
| 191 | $accessor = new Accessor($data); |
|
| 192 | $keyCount = count($keys); |
|
| 193 | $depth = 0; |
|
| 194 | foreach ($keys as $key) { |
|
| 195 | if ($depth + 1 === $keyCount) { |
|
| 196 | if ($accessor->remove($key) === false) { |
|
| 197 | throw new InvalidArgumentException(sprintf( |
|
| 198 | 'Did not remove path %s in structure %s', |
|
| 199 | json_encode($keys), |
|
| 200 | json_encode($data) |
|
| 201 | )); |
|
| 202 | } |
|
| 203 | } elseif ($accessor->to($key) === false) { |
|
| 204 | throw new InvalidArgumentException(sprintf( |
|
| 205 | 'Did not find path %s in structure %s', |
|
| 206 | json_encode($keys), |
|
| 207 | json_encode($data) |
|
| 208 | )); |
|
| 209 | } |
|
| 210 | ||
| 211 | ++$depth; |
|
| 212 | } |
|
| 213 | ||
| 214 | return $accessor->getData(); |
|
| 215 | } |
|
| 216 | ||
| 217 | /** |
|
| 218 | * @param string[]|string|null $keys |
|