1 | <?php |
||
32 | final class NamespaceStmtCollection implements IteratorAggregate, Countable |
||
33 | { |
||
34 | /** |
||
35 | * @var Namespace_[] |
||
36 | */ |
||
37 | private $nodes = []; |
||
38 | |||
39 | /** |
||
40 | * @var (Name|null)[] Associative array with the potentially prefixed namespace names as keys and their original name |
||
41 | * as value. |
||
42 | */ |
||
43 | private $mapping = []; |
||
44 | |||
45 | /** |
||
46 | * @param Namespace_ $node New namespace, may have been prefixed. |
||
47 | * @param Namespace_ $originalName Original unchanged namespace. |
||
48 | */ |
||
49 | 10 | public function add(Namespace_ $node, Namespace_ $originalName): void |
|
55 | |||
56 | 7 | public function findNamespaceForNode(Node $node): ?Name |
|
57 | { |
||
58 | 7 | if (0 === count($this->nodes)) { |
|
59 | return null; |
||
60 | } |
||
61 | |||
62 | // Shortcut if there is only one namespace |
||
63 | 7 | if (1 === count($this->nodes)) { |
|
64 | 7 | return $this->nodes[0]->name; |
|
65 | } |
||
66 | |||
67 | return $this->getNodeNamespace($node); |
||
68 | } |
||
69 | |||
70 | public function findNamespaceByName(string $name): ?Name |
||
71 | { |
||
72 | foreach ($this->nodes as $node) { |
||
73 | if ((string) $node->name === $name) { |
||
74 | return $node->name; |
||
75 | } |
||
76 | } |
||
77 | |||
78 | return null; |
||
79 | } |
||
80 | |||
81 | public function getCurrentNamespaceName(): ?Name |
||
82 | { |
||
83 | $lastNode = end($this->nodes); |
||
84 | |||
85 | return false === $lastNode ? null : $lastNode->name; |
||
86 | } |
||
87 | |||
88 | /** |
||
89 | * @inheritdoc |
||
90 | */ |
||
91 | public function count(): int |
||
95 | |||
96 | private function getNodeNamespace(Node $node): ?Name |
||
97 | { |
||
98 | if (false === ParentNodeAppender::hasParent($node)) { |
||
99 | return null; |
||
100 | } |
||
101 | |||
102 | $parentNode = ParentNodeAppender::getParent($node); |
||
103 | |||
104 | if ($parentNode instanceof Namespace_) { |
||
105 | return $this->mapping[(string) $parentNode->name]; |
||
106 | } |
||
107 | |||
108 | return $this->getNodeNamespace($parentNode); |
||
109 | } |
||
110 | |||
111 | /** |
||
112 | * @inheritdoc |
||
113 | */ |
||
114 | public function getIterator(): iterable |
||
118 | } |
||
119 |