Skip to content

Commit

Permalink
Fixing issue with KVTree
Browse files Browse the repository at this point in the history
  • Loading branch information
dcarbone committed Jul 18, 2016
1 parent 6d784a5 commit 98d472e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 36 deletions.
33 changes: 30 additions & 3 deletions src/KV/KVClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,41 @@ public function tree($prefix = null, QueryOptions $queryOptions = null)
if (!isset($treeHierarchy[$root]))
$treeHierarchy[$root] = new KVTree($root);

// We're still in the path definition...
if ('/' === substr($path, -1))
{
$treeHierarchy[$root][$path] = new KVTree($path);
$_path = '';
foreach(explode('/', $prefix) as $part)
{
if ('' === $part)
continue;

$_path .= "{$part}/";

if ($root === $_path)
continue;

if (!isset($treeHierarchy[$root][$_path]))
$treeHierarchy[$root][$_path] = new KVTree($_path);
}
}
// We've arrived at an actual key
else
{
$kvPrefix = substr($path, 0, strrpos($path, '/') + 1);
$_path = '';
foreach(explode('/', $kvPrefix) as $part)
{
if ('' === $part)
continue;

$_path .= "{$part}/";

if ($root === $_path)
continue;

if (!isset($treeHierarchy[$root][$_path]))
$treeHierarchy[$root][$_path] = new KVTree($_path);
}

$treeHierarchy[$root][$path] = $kvp;
}
}
Expand Down
85 changes: 52 additions & 33 deletions src/KV/KVTree.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,15 +136,12 @@ public function offsetExists($offset)
{
if (is_string($offset))
{
$subPath = trim(str_replace($this->_prefix, '', $offset), "/");
$slashPos = strpos($subPath, '/');
if (false === $slashPos)
{
$offset = $subPath;
}
else
$subPath = str_replace($this->_prefix, '', $offset);
$cnt = substr_count($subPath, '/');

if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
{
$childKey = substr($offset, 0, $slashPos);
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
if (isset($this->_children[$childKey]))
return isset($this->_children[$childKey][$offset]);
}
Expand All @@ -163,15 +160,11 @@ public function offsetGet($offset)
{
if (is_string($offset))
{
$subPath = trim(str_replace($this->_prefix, '', $offset), "/");
$slashPos = strpos($subPath, '/');
if (false === $slashPos)
$subPath = str_replace($this->_prefix, '', $offset);
$cnt = substr_count($subPath, '/');
if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
{
$offset = $subPath;
}
else
{
$childKey = substr($subPath, 0, $slashPos);
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
if (isset($this[$childKey]))
return $this->_children[$childKey][$offset];
}
Expand Down Expand Up @@ -199,23 +192,28 @@ public function offsetGet($offset)
*/
public function offsetSet($offset, $value)
{
switch(gettype($offset))
if ('string' === gettype($offset))
{
case 'NULL':
$this->_children[] = $value;
break;
case 'integer':
case 'double':
$this->_children[$offset] = $value;
break;
case 'string':
$childPath = trim(str_replace($this->_prefix, '', $offset), "/");
$slashPos = strpos($childPath, '/');
$subPath = str_replace($this->_prefix, '', $offset);
$cnt = substr_count($subPath, '/');

if (false === $slashPos)
$this->_children[$childPath] = $value;
else
$this->_children[substr($childPath, 0, $slashPos)][$offset] = $value;
if (1 < $cnt || (1 === $cnt && '/' !== substr($subPath, -1)))
{
$childKey = $this->_prefix.substr($subPath, 0, strpos($subPath, '/') + 1);
$this->_children[$childKey][$offset] = $value;
}
else
{
$this->_children[$offset] = $value;
}
}
else if (null === $offset)
{
$this->_children[] = $value;
}
else
{
$this->_children[$offset] = $value;
}
}

Expand Down Expand Up @@ -267,9 +265,30 @@ public function jsonSerialize()
$json[$this->_prefix] = $child;
else if ($child instanceof KVPair)
$json[$this->_prefix][$child->Key] = $child;
else
$json[$this->_prefix][$k] = $child;
}
return $json;
}

/**
* @return string
*/
public function __toString()
{
return $this->_prefix;
}

/**
* This method is called by var_dump() when dumping an object to get the properties that should be shown.
* If the method isn't defined on an object, then all public, protected and private properties will be shown.
*
* @return array
* @link http://php.net/manual/en/language.oop5.magic.php#language.oop5.magic.debuginfo
*/
public function __debugInfo()
{
return array(
'prefix' => $this->_prefix,
'children' => $this->_children
);
}
}

0 comments on commit 98d472e

Please sign in to comment.