| Total Complexity | 59 |
| Total Lines | 303 |
| Duplicated Lines | 0 % |
| Changes | 7 | ||
| Bugs | 0 | Features | 0 |
Complex classes like IteratorDot often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use IteratorDot, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 22 | class IteratorDot extends ValidateDot |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * The stored items |
||
| 26 | * |
||
| 27 | * @var array |
||
| 28 | */ |
||
| 29 | protected $items = []; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * elements |
||
| 33 | * |
||
| 34 | * @var array |
||
| 35 | */ |
||
| 36 | protected $elements; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Create a new Dot instance |
||
| 40 | * |
||
| 41 | * @param mixed $items |
||
| 42 | */ |
||
| 43 | public function __construct($items = []) |
||
| 44 | { |
||
| 45 | $this->elements = $this->getArrayItems($items); |
||
| 46 | } |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Set a given key / value pair or pairs |
||
| 50 | * if the key doesn't exist already |
||
| 51 | * |
||
| 52 | * @param array|int|string $keys |
||
| 53 | * @param mixed $value |
||
| 54 | */ |
||
| 55 | public function add($keys, $value = null) |
||
| 63 | } |
||
| 64 | } |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Delete the contents of a given key or keys |
||
| 68 | * |
||
| 69 | * @param array|int|string|null $keys |
||
| 70 | */ |
||
| 71 | public function clear($keys = null) |
||
| 72 | { |
||
| 73 | if (is_null($keys)) { |
||
| 74 | $this->elements = []; |
||
| 75 | return; |
||
| 76 | } |
||
| 77 | $keys = (array)$keys; |
||
| 78 | foreach ($keys as $key) { |
||
| 79 | $this->set($key, []); |
||
| 80 | } |
||
| 81 | } |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Delete the given key or keys |
||
| 85 | * |
||
| 86 | * @param array|int|string $keys |
||
| 87 | */ |
||
| 88 | public function delete($keys) |
||
| 89 | { |
||
| 90 | $keys = (array)$keys; |
||
| 91 | foreach ($keys as $key) { |
||
| 92 | if ($this->exists($this->elements, $key)) { |
||
| 93 | unset($this->elements[$key]); |
||
| 94 | continue; |
||
| 95 | } |
||
| 96 | $items = &$this->elements; |
||
| 97 | $segments = explode('.', $key); |
||
| 98 | $lastSegment = array_pop($segments); |
||
| 99 | foreach ($segments as $segment) { |
||
| 100 | if (!isset($items[$segment]) || !is_array($items[$segment])) { |
||
| 101 | continue 2; |
||
| 102 | } |
||
| 103 | $items = &$items[$segment]; |
||
| 104 | } |
||
| 105 | unset($items[$lastSegment]); |
||
| 106 | } |
||
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Flatten an array with the given character as a key delimiter |
||
| 111 | * |
||
| 112 | * @param string $delimiter |
||
| 113 | * @param array|null $items |
||
| 114 | * @param string $prepend |
||
| 115 | * @return array |
||
| 116 | */ |
||
| 117 | public function flatten($delimiter = '.', $items = null, $prepend = '') |
||
| 118 | { |
||
| 119 | $flatten = []; |
||
| 120 | if (is_null($items)) { |
||
| 121 | $items = $this->elements; |
||
| 122 | } |
||
| 123 | foreach ($items as $key => $value) { |
||
| 124 | if (is_array($value) && !empty($value)) { |
||
| 125 | $flatten = array_merge( |
||
| 126 | $flatten, |
||
| 127 | $this->flatten($delimiter, $value, $prepend . $key . $delimiter) |
||
| 128 | ); |
||
| 129 | } else { |
||
| 130 | $flatten[$prepend . $key] = $value; |
||
| 131 | } |
||
| 132 | } |
||
| 133 | return $flatten; |
||
| 134 | } |
||
| 135 | |||
| 136 | /** |
||
| 137 | * Return the value of a given key |
||
| 138 | * |
||
| 139 | * @param int|string|null $key |
||
| 140 | * @param mixed $default |
||
| 141 | * @return mixed |
||
| 142 | */ |
||
| 143 | public function get($key = null, $default = null) |
||
| 144 | { |
||
| 145 | if (is_null($key)) { |
||
| 146 | return $this->elements; |
||
| 147 | } |
||
| 148 | if ($this->exists($this->elements, $key)) { |
||
| 149 | return $this->elements[$key]; |
||
| 150 | } |
||
| 151 | if (false === strpos($key, '.')) { |
||
| 152 | return $default; |
||
| 153 | } |
||
| 154 | $items = $this->elements; |
||
| 155 | foreach (explode('.', $key) as $segment) { |
||
| 156 | if (!is_array($items) || !$this->exists($items, $segment)) { |
||
| 157 | return $default; |
||
| 158 | } |
||
| 159 | $items = &$items[$segment]; |
||
| 160 | } |
||
| 161 | return $items; |
||
| 162 | } |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Return the given items as an array |
||
| 166 | * |
||
| 167 | * @param mixed $items |
||
| 168 | * @return array |
||
| 169 | */ |
||
| 170 | protected function getArrayItems($items) |
||
| 171 | { |
||
| 172 | if (is_array($items)) { |
||
| 173 | return $items; |
||
| 174 | } elseif ($items instanceof self) { |
||
| 175 | return $items->all(); |
||
| 176 | } |
||
| 177 | return (array)$items; |
||
| 178 | } |
||
| 179 | |||
| 180 | /** |
||
| 181 | * Check if a given key or keys exists |
||
| 182 | * |
||
| 183 | * @param array|int|string $keys |
||
| 184 | * @return bool |
||
| 185 | */ |
||
| 186 | public function has($keys) |
||
| 187 | { |
||
| 188 | $keys = (array)$keys; |
||
| 189 | if ( empty($this->elements) || $keys === []) { |
||
| 190 | return false; |
||
| 191 | } |
||
| 192 | foreach ($keys as $key) { |
||
| 193 | $items = $this->elements; |
||
| 194 | if ($this->exists($items, $key)) { |
||
| 195 | continue; |
||
| 196 | } |
||
| 197 | foreach (explode('.', $key) as $segment) { |
||
| 198 | if (!is_array($items) || !$this->exists($items, $segment)) { |
||
| 199 | return false; |
||
| 200 | } |
||
| 201 | $items = $items[$segment]; |
||
| 202 | } |
||
| 203 | } |
||
| 204 | return true; |
||
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check if a given key or keys are empty |
||
| 209 | * |
||
| 210 | * @param array|int|string|null $keys |
||
| 211 | * @return bool |
||
| 212 | */ |
||
| 213 | public function isEmpty($keys = null) |
||
| 214 | { |
||
| 215 | if (is_null($keys)) { |
||
| 216 | return empty($this->elements); |
||
| 217 | } |
||
| 218 | $keys = (array)$keys; |
||
| 219 | foreach ($keys as $key) { |
||
| 220 | if (!empty($this->get($key))) { |
||
| 221 | return false; |
||
| 222 | } |
||
| 223 | } |
||
| 224 | return true; |
||
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * Merge a given array or a Dot object with the given key |
||
| 229 | * or with the whole Dot object |
||
| 230 | * |
||
| 231 | * @param array|string|self $key |
||
| 232 | * @param array $value |
||
| 233 | */ |
||
| 234 | public function merge($key, $value = null) |
||
| 235 | { |
||
| 236 | if (is_array($key)) { |
||
| 237 | $this->elements = array_merge($this->elements, $key); |
||
| 238 | } elseif (is_string($key)) { |
||
| 239 | $items = (array)$this->get($key); |
||
| 240 | $value = array_merge($items, $this->getArrayItems($value)); |
||
| 241 | $this->set($key, $value); |
||
| 242 | } |
||
| 243 | $this->elements = array_merge($this->elements, $key->all()); |
||
| 244 | |||
| 245 | } |
||
| 246 | |||
| 247 | /** |
||
| 248 | * Return the value of a given key and |
||
| 249 | * delete the key |
||
| 250 | * |
||
| 251 | * @param int|string|null $key |
||
| 252 | * @param mixed $default |
||
| 253 | * @return mixed |
||
| 254 | */ |
||
| 255 | public function pull($key = null, $default = null) |
||
| 256 | { |
||
| 257 | if (is_null($key)) { |
||
| 258 | $value = $this->all(); |
||
| 259 | $this->clear(); |
||
| 260 | return $value; |
||
| 261 | } |
||
| 262 | $value = $this->get($key, $default); |
||
| 263 | $this->delete($key); |
||
| 264 | return $value; |
||
| 265 | } |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Push a given value to the end of the array |
||
| 269 | * in a given key |
||
| 270 | * |
||
| 271 | * @param mixed $key |
||
| 272 | * @param mixed $value |
||
| 273 | */ |
||
| 274 | public function push($key, $value = null) |
||
| 275 | { |
||
| 276 | if (is_null($value)) { |
||
| 277 | $this->elements[] = $key; |
||
| 278 | return; |
||
| 279 | } |
||
| 280 | $items = $this->get($key); |
||
| 281 | if (is_array($items) || is_null($items)) { |
||
| 282 | $items[] = $value; |
||
| 283 | $this->set($key, $items); |
||
| 284 | } |
||
| 285 | } |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Set a given key / value pair or pairs |
||
| 289 | * |
||
| 290 | * @param array|int|string $keys |
||
| 291 | * @param mixed $value |
||
| 292 | */ |
||
| 293 | public function set($keys, $value = null) |
||
| 294 | { |
||
| 295 | if (is_array($keys)) { |
||
| 296 | foreach ($keys as $key => $value) { |
||
| 297 | $this->set($key, $value); |
||
| 298 | } |
||
| 299 | return; |
||
| 300 | } |
||
| 301 | $items = &$this->elements; |
||
| 302 | foreach (explode('.', $keys) as $key) { |
||
| 303 | if (!isset($items[$key]) || !is_array($items[$key])) { |
||
| 304 | $items[$key] = []; |
||
| 305 | } |
||
| 306 | $items = &$items[$key]; |
||
| 307 | } |
||
| 308 | $items = $value; |
||
| 309 | } |
||
| 310 | |||
| 311 | /** |
||
| 312 | * Return the value of a given key or all the values as JSON |
||
| 313 | * |
||
| 314 | * @param mixed $key |
||
| 315 | * @param int $options |
||
| 316 | * @return string |
||
| 317 | */ |
||
| 318 | public function toJson($key = null, $options = 0) |
||
| 325 | } |
||
| 326 | |||
| 327 | } |