|
5 | 5 | * |
6 | 6 | * @author Riku Särkinen <riku@adbar.io> |
7 | 7 | * @link https://github.com/adbario/php-dot-notation |
8 | | - * @license https://github.com/adbario/php-dot-notation/blob/2.x/LICENSE.md (MIT License) |
| 8 | + * @license https://github.com/adbario/php-dot-notation/blob/3.x/LICENSE.md (MIT License) |
9 | 9 | */ |
10 | 10 |
|
11 | 11 | namespace Adbar; |
@@ -36,19 +36,30 @@ class Dot implements ArrayAccess, Countable, IteratorAggregate, JsonSerializable |
36 | 36 | * |
37 | 37 | * @var array<TKey, TValue> |
38 | 38 | */ |
39 | | - protected $items = []; |
| 39 | + protected $items; |
| 40 | + |
| 41 | + /** |
| 42 | + * The character to use as a delimiter, defaults to dot (.) |
| 43 | + * |
| 44 | + * @var non-empty-string |
| 45 | + */ |
| 46 | + protected $delimiter = "."; |
40 | 47 |
|
41 | 48 | /** |
42 | 49 | * Create a new Dot instance |
43 | 50 | * |
44 | 51 | * @param mixed $items |
45 | 52 | * @param bool $parse |
| 53 | + * @param non-empty-string $delimiter |
46 | 54 | * @return void |
47 | 55 | */ |
48 | | - public function __construct($items = [], $parse = false) |
| 56 | + public function __construct($items = [], $parse = false, $delimiter = ".") |
49 | 57 | { |
50 | 58 | $items = $this->getArrayItems($items); |
51 | 59 |
|
| 60 | + // $this->delimiter = strlen($delimiter) ? $delimiter : '.'; |
| 61 | + $this->delimiter = $delimiter ?: "."; |
| 62 | + |
52 | 63 | if ($parse) { |
53 | 64 | $this->set($items); |
54 | 65 | } else { |
@@ -128,7 +139,7 @@ public function delete($keys) |
128 | 139 | } |
129 | 140 |
|
130 | 141 | $items = &$this->items; |
131 | | - $segments = explode('.', $key); |
| 142 | + $segments = explode($this->delimiter, $key); |
132 | 143 | $lastSegment = array_pop($segments); |
133 | 144 |
|
134 | 145 | foreach ($segments as $segment) { |
@@ -201,13 +212,13 @@ public function get($key = null, $default = null) |
201 | 212 | return $this->items[$key]; |
202 | 213 | } |
203 | 214 |
|
204 | | - if (!is_string($key) || strpos($key, '.') === false) { |
| 215 | + if (!is_string($key) || strpos($key, $this->delimiter) === false) { |
205 | 216 | return $default; |
206 | 217 | } |
207 | 218 |
|
208 | 219 | $items = $this->items; |
209 | 220 |
|
210 | | - foreach (explode('.', $key) as $segment) { |
| 221 | + foreach (explode($this->delimiter, $key) as $segment) { |
211 | 222 | if (!is_array($items) || !$this->exists($items, $segment)) { |
212 | 223 | return $default; |
213 | 224 | } |
@@ -258,7 +269,7 @@ public function has($keys) |
258 | 269 | continue; |
259 | 270 | } |
260 | 271 |
|
261 | | - foreach (explode('.', $key) as $segment) { |
| 272 | + foreach (explode($this->delimiter, $key) as $segment) { |
262 | 273 | if (!is_array($items) || !$this->exists($items, $segment)) { |
263 | 274 | return false; |
264 | 275 | } |
@@ -487,7 +498,7 @@ public function set($keys, $value = null) |
487 | 498 | $items = &$this->items; |
488 | 499 |
|
489 | 500 | if (is_string($keys)) { |
490 | | - foreach (explode('.', $keys) as $key) { |
| 501 | + foreach (explode($this->delimiter, $keys) as $key) { |
491 | 502 | if (!isset($items[$key]) || !is_array($items[$key])) { |
492 | 503 | $items[$key] = []; |
493 | 504 | } |
|
0 commit comments