11<?php
22
3+ declare (strict_types=1 );
4+
35/*
46 * This file is a part of dflydev/dot-access-data.
57 *
@@ -45,25 +47,10 @@ public function __construct(array $data = [])
4547 public function append (string $ key , $ value = null ): void
4648 {
4749 $ currentValue =& $ this ->data ;
48- $ keyPath = $ this ->keyToPathArray ($ key );
49-
50- if (1 == count ($ keyPath )) {
51- if (!isset ($ currentValue [$ key ])) {
52- $ currentValue [$ key ] = [];
53- }
54- if (!is_array ($ currentValue [$ key ])) {
55- // Promote this key to an array.
56- // TODO: Is this really what we want to do?
57- $ currentValue [$ key ] = [$ currentValue [$ key ]];
58- }
59- $ currentValue [$ key ][] = $ value ;
60-
61- return ;
62- }
50+ $ keyPath = self ::keyToPathArray ($ key );
6351
6452 $ endKey = array_pop ($ keyPath );
65- for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
66- $ currentKey =& $ keyPath [$ i ];
53+ foreach ($ keyPath as $ currentKey ) {
6754 if (! isset ($ currentValue [$ currentKey ])) {
6855 $ currentValue [$ currentKey ] = [];
6956 }
@@ -73,11 +60,13 @@ public function append(string $key, $value = null): void
7360 if (!isset ($ currentValue [$ endKey ])) {
7461 $ currentValue [$ endKey ] = [];
7562 }
63+
7664 if (!is_array ($ currentValue [$ endKey ])) {
65+ // Promote this key to an array.
66+ // TODO: Is this really what we want to do?
7767 $ currentValue [$ endKey ] = [$ currentValue [$ endKey ]];
7868 }
79- // Promote this key to an array.
80- // TODO: Is this really what we want to do?
69+
8170 $ currentValue [$ endKey ][] = $ value ;
8271 }
8372
@@ -87,22 +76,15 @@ public function append(string $key, $value = null): void
8776 public function set (string $ key , $ value = null ): void
8877 {
8978 $ currentValue =& $ this ->data ;
90- $ keyPath = $ this ->keyToPathArray ($ key );
91-
92- if (1 == count ($ keyPath )) {
93- $ currentValue [$ key ] = $ value ;
94-
95- return ;
96- }
79+ $ keyPath = self ::keyToPathArray ($ key );
9780
9881 $ endKey = array_pop ($ keyPath );
99- for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
100- $ currentKey =& $ keyPath [$ i ];
82+ foreach ($ keyPath as $ currentKey ) {
10183 if (!isset ($ currentValue [$ currentKey ])) {
10284 $ currentValue [$ currentKey ] = [];
10385 }
10486 if (!is_array ($ currentValue [$ currentKey ])) {
105- throw new DataException (" Key path at $ currentKey of $ key cannot be indexed into (is not an array)" );
87+ throw new DataException (sprintf ( ' Key path "%s" within "%s" cannot be indexed into (is not an array) ' , $ currentKey , self :: formatPath ( $ key )) );
10688 }
10789 $ currentValue =& $ currentValue [$ currentKey ];
10890 }
@@ -115,17 +97,10 @@ public function set(string $key, $value = null): void
11597 public function remove (string $ key ): void
11698 {
11799 $ currentValue =& $ this ->data ;
118- $ keyPath = $ this ->keyToPathArray ($ key );
119-
120- if (1 == count ($ keyPath )) {
121- unset($ currentValue [$ key ]);
122-
123- return ;
124- }
100+ $ keyPath = self ::keyToPathArray ($ key );
125101
126102 $ endKey = array_pop ($ keyPath );
127- for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
128- $ currentKey =& $ keyPath [$ i ];
103+ foreach ($ keyPath as $ currentKey ) {
129104 if (!isset ($ currentValue [$ currentKey ])) {
130105 return ;
131106 }
@@ -142,10 +117,9 @@ public function remove(string $key): void
142117 public function get (string $ key , $ default = null )
143118 {
144119 $ currentValue = $ this ->data ;
145- $ keyPath = $ this -> keyToPathArray ($ key );
120+ $ keyPath = self :: keyToPathArray ($ key );
146121
147- for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
148- $ currentKey = $ keyPath [$ i ];
122+ foreach ($ keyPath as $ currentKey ) {
149123 if (!isset ($ currentValue [$ currentKey ])) {
150124 return $ default ;
151125 }
@@ -166,10 +140,8 @@ public function get(string $key, $default = null)
166140 public function has (string $ key ): bool
167141 {
168142 $ currentValue = &$ this ->data ;
169- $ keyPath = $ this ->keyToPathArray ($ key );
170143
171- for ($ i = 0 ; $ i < count ($ keyPath ); $ i ++) {
172- $ currentKey = $ keyPath [$ i ];
144+ foreach (self ::keyToPathArray ($ key ) as $ currentKey ) {
173145 if (
174146 !is_array ($ currentValue ) ||
175147 !array_key_exists ($ currentKey , $ currentValue )
@@ -194,7 +166,7 @@ public function getData(string $key): DataInterface
194166 return new Data ($ value );
195167 }
196168
197- throw new DataException (" Value at ' $ key ' could not be represented as a DataInterface" );
169+ throw new DataException (sprintf ( ' Value at "%s" could not be represented as a DataInterface ' , self :: formatPath ( $ key )) );
198170 }
199171
200172 /**
@@ -258,13 +230,15 @@ public function offsetUnset($key)
258230 }
259231
260232 /**
233+ * @param string $path
234+ *
261235 * @return string[]
262236 *
263237 * @psalm-return non-empty-list<string>
264238 *
265239 * @psalm-pure
266240 */
267- protected function keyToPathArray (string $ path ): array
241+ protected static function keyToPathArray (string $ path ): array
268242 {
269243 if (\strlen ($ path ) === 0 ) {
270244 throw new InvalidPathException ('Path cannot be an empty string ' );
@@ -274,4 +248,20 @@ protected function keyToPathArray(string $path): array
274248
275249 return \explode ('. ' , $ path );
276250 }
251+
252+ /**
253+ * @param string|string[] $path
254+ *
255+ * @return string
256+ *
257+ * @psalm-pure
258+ */
259+ protected static function formatPath ($ path ): string
260+ {
261+ if (is_string ($ path )) {
262+ $ path = self ::keyToPathArray ($ path );
263+ }
264+
265+ return implode (' » ' , $ path );
266+ }
277267}
0 commit comments