<?php
namespace phpDocumentor\Plugin\Core\Transformer\Writer;
class Pathfinder
{
public function find($object, $query)
{
if ($query) {
$node = $this->walkObjectTree($object, $query);
if (!is_array($node) && (!$node instanceof \Traversable)) {
$node = array($node);
}
return $node;
}
return array($object);
}
private function walkObjectTree($objectOrArray, $query)
{
$node = $objectOrArray;
$objectPath = explode('.', $query);
foreach ($objectPath as $pathNode) {
if (is_array($node)) {
if (isset($node[$pathNode])) {
$node = $node[$pathNode];
continue;
}
} elseif (is_object($node)) {
if (isset($node->$pathNode) || (method_exists($node, '__get') && $node->$pathNode)) {
$node = $node->$pathNode;
continue;
} elseif (method_exists($node, $pathNode)) {
$node = $node->$pathNode();
continue;
} elseif (method_exists($node, 'get' . $pathNode)) {
$pathNode = 'get' . $pathNode;
$node = $node->$pathNode();
continue;
} elseif (method_exists($node, 'is' . $pathNode)) {
$pathNode = 'is' . $pathNode;
$node = $node->$pathNode();
continue;
}
}
return null;
}
return $node;
}
}