1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* This file is part of phpDocumentor. |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
* @author Mike van Riel <[email protected]> |
11
|
|
|
* @copyright 2010-2018 Mike van Riel / Naenius (http://www.naenius.com) |
12
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT |
13
|
|
|
* @link http://phpdoc.org |
14
|
|
|
*/ |
15
|
|
|
|
16
|
|
|
namespace phpDocumentor\Transformer\Writer; |
17
|
|
|
|
18
|
|
|
class Pathfinder |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Combines the query and an object to retrieve a list of nodes that are to be used as node-point in a template. |
22
|
|
|
* |
23
|
|
|
* This method interprets the provided query string and walks through the given object to find the correct |
24
|
|
|
* element. This method will silently fail if an invalid query was provided; in such a case the given object |
25
|
|
|
* is returned. |
26
|
|
|
* |
27
|
|
|
* @param object $object |
28
|
|
|
* @param string $query |
29
|
|
|
* |
30
|
|
|
* @return \Traversable|array |
31
|
|
|
*/ |
32
|
|
|
public function find($object, $query) |
33
|
|
|
{ |
34
|
|
|
if ($query) { |
35
|
|
|
$node = $this->walkObjectTree($object, $query); |
36
|
|
|
|
37
|
|
|
if (!is_array($node) && (!$node instanceof \Traversable)) { |
38
|
|
|
$node = [$node]; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $node; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return [$object]; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Walks an object graph and/or array using a twig query string. |
49
|
|
|
* |
50
|
|
|
* @param \Traversable|mixed $objectOrArray |
51
|
|
|
* @param string $query A path to walk separated by dots, i.e. `namespace.namespaces`. |
52
|
|
|
* |
53
|
|
|
* @return mixed |
54
|
|
|
*/ |
55
|
|
|
private function walkObjectTree($objectOrArray, $query) |
56
|
|
|
{ |
57
|
|
|
$node = $objectOrArray; |
58
|
|
|
$objectPath = explode('.', $query); |
59
|
|
|
|
60
|
|
|
// walk through the tree |
61
|
|
|
foreach ($objectPath as $pathNode) { |
62
|
|
|
if (is_array($node)) { |
63
|
|
|
if (isset($node[$pathNode])) { |
64
|
|
|
$node = $node[$pathNode]; |
65
|
|
|
continue; |
66
|
|
|
} |
67
|
|
|
} elseif (is_object($node)) { |
68
|
|
|
if (isset($node->{$pathNode}) || (method_exists($node, '__get') && $node->{$pathNode})) { |
69
|
|
|
$node = $node->{$pathNode}; |
70
|
|
|
continue; |
71
|
|
|
} elseif (method_exists($node, $pathNode)) { |
72
|
|
|
$node = $node->{$pathNode}(); |
73
|
|
|
continue; |
74
|
|
|
} elseif (method_exists($node, 'get' . $pathNode)) { |
75
|
|
|
$pathNode = 'get' . $pathNode; |
76
|
|
|
$node = $node->{$pathNode}(); |
77
|
|
|
continue; |
78
|
|
|
} elseif (method_exists($node, 'is' . $pathNode)) { |
79
|
|
|
$pathNode = 'is' . $pathNode; |
80
|
|
|
$node = $node->{$pathNode}(); |
81
|
|
|
continue; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
return null; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $node; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|