Skip to content

Commit 98d472e

Browse files
committed
Fixing issue with KVTree
1 parent 6d784a5 commit 98d472e

File tree

2 files changed

+82
-36
lines changed

2 files changed

+82
-36
lines changed

src/KV/KVClient.php

+30-3
Original file line numberDiff line numberDiff line change
@@ -224,14 +224,41 @@ public function tree($prefix = null, QueryOptions $queryOptions = null)
224224
if (!isset($treeHierarchy[$root]))
225225
$treeHierarchy[$root] = new KVTree($root);
226226

227-
// We're still in the path definition...
228227
if ('/' === substr($path, -1))
229228
{
230-
$treeHierarchy[$root][$path] = new KVTree($path);
229+
$_path = '';
230+
foreach(explode('/', $prefix) as $part)
231+
{
232+
if ('' === $part)
233+
continue;
234+
235+
$_path .= "{$part}/";
236+
237+
if ($root === $_path)
238+
continue;
239+
240+
if (!isset($treeHierarchy[$root][$_path]))
241+
$treeHierarchy[$root][$_path] = new KVTree($_path);
242+
}
231243
}
232-
// We've arrived at an actual key
233244
else
234245
{
246+
$kvPrefix = substr($path, 0, strrpos($path, '/') + 1);
247+
$_path = '';
248+
foreach(explode('/', $kvPrefix) as $part)
249+
{
250+
if ('' === $part)
251+
continue;
252+
253+
$_path .= "{$part}/";
254+
255+
if ($root === $_path)
256+
continue;
257+
258+
if (!isset($treeHierarchy[$root][$_path]))
259+
$treeHierarchy[$root][$_path] = new KVTree($_path);
260+
}
261+
235262
$treeHierarchy[$root][$path] = $kvp;
236263
}
237264
}

src/KV/KVTree.php

+52-33
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,12 @@ public function offsetExists($offset)
136136
{
137137
if (is_string($offset))
138138
{
139-
$subPath = trim(str_replace($this->_prefix, '', $offset), "/");
140-
$slashPos = strpos($subPath, '/');
141-
if (false === $slashPos)
142-
{
143-
$offset = $subPath;
144-
}
145-
else
139+
$subPath = str_replace($this->_prefix, '', $offset);
140+
$cnt = substr_count($subPath, '/');
141+
142+
if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
146143
{
147-
$childKey = substr($offset, 0, $slashPos);
144+
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
148145
if (isset($this->_children[$childKey]))
149146
return isset($this->_children[$childKey][$offset]);
150147
}
@@ -163,15 +160,11 @@ public function offsetGet($offset)
163160
{
164161
if (is_string($offset))
165162
{
166-
$subPath = trim(str_replace($this->_prefix, '', $offset), "/");
167-
$slashPos = strpos($subPath, '/');
168-
if (false === $slashPos)
163+
$subPath = str_replace($this->_prefix, '', $offset);
164+
$cnt = substr_count($subPath, '/');
165+
if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
169166
{
170-
$offset = $subPath;
171-
}
172-
else
173-
{
174-
$childKey = substr($subPath, 0, $slashPos);
167+
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
175168
if (isset($this[$childKey]))
176169
return $this->_children[$childKey][$offset];
177170
}
@@ -199,23 +192,28 @@ public function offsetGet($offset)
199192
*/
200193
public function offsetSet($offset, $value)
201194
{
202-
switch(gettype($offset))
195+
if ('string' === gettype($offset))
203196
{
204-
case 'NULL':
205-
$this->_children[] = $value;
206-
break;
207-
case 'integer':
208-
case 'double':
209-
$this->_children[$offset] = $value;
210-
break;
211-
case 'string':
212-
$childPath = trim(str_replace($this->_prefix, '', $offset), "/");
213-
$slashPos = strpos($childPath, '/');
197+
$subPath = str_replace($this->_prefix, '', $offset);
198+
$cnt = substr_count($subPath, '/');
214199

215-
if (false === $slashPos)
216-
$this->_children[$childPath] = $value;
217-
else
218-
$this->_children[substr($childPath, 0, $slashPos)][$offset] = $value;
200+
if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
201+
{
202+
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
203+
$this->_children[$childKey][$offset] = $value;
204+
}
205+
else
206+
{
207+
$this->_children[$offset] = $value;
208+
}
209+
}
210+
else if (null === $offset)
211+
{
212+
$this->_children[] = $value;
213+
}
214+
else
215+
{
216+
$this->_children[$offset] = $value;
219217
}
220218
}
221219

@@ -267,9 +265,30 @@ public function jsonSerialize()
267265
$json[$this->_prefix] = $child;
268266
else if ($child instanceof KVPair)
269267
$json[$this->_prefix][$child->Key] = $child;
270-
else
271-
$json[$this->_prefix][$k] = $child;
272268
}
273269
return $json;
274270
}
271+
272+
/**
273+
* @return string
274+
*/
275+
public function __toString()
276+
{
277+
return $this->_prefix;
278+
}
279+
280+
/**
281+
* This method is called by var_dump() when dumping an object to get the properties that should be shown.
282+
* If the method isn't defined on an object, then all public, protected and private properties will be shown.
283+
*
284+
* @return array
285+
* @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
286+
*/
287+
public function __debugInfo()
288+
{
289+
return array(
290+
'prefix' => $this->_prefix,
291+
'children' => $this->_children
292+
);
293+
}
275294
}

0 commit comments

Comments
 (0)