1: <?php
2: namespace Pharborist;
3:
4: use Pharborist\Namespaces\NamespaceNode;
5:
6: 7: 8:
9: class RootNode extends StatementBlockNode {
10: 11: 12: 13: 14: 15: 16: 17:
18: public static function create($ns = NULL) {
19: $node = new RootNode();
20: $node->addChild(Token::openTag());
21: if (is_string($ns) && $ns) {
22: NamespaceNode::create($ns)->appendTo($node)->after(Token::newline());
23: }
24: return $node;
25: }
26:
27: 28: 29: 30: 31: 32: 33: 34:
35: public function hasNamespace($ns) {
36: return in_array($ns, $this->getNamespaceNames());
37: }
38:
39: 40: 41: 42: 43:
44: public function getNamespaces() {
45: return $this->children(Filter::isInstanceOf('\Pharborist\Namespaces\NamespaceNode'));
46: }
47:
48: 49: 50: 51: 52: 53: 54: 55:
56: public function getNamespace($ns) {
57: $namespaces = $this
58: ->getNamespaces()
59: ->filter(function(NamespaceNode $node) use ($ns) {
60: return $node->getName()->getPath() === $ns;
61: });
62:
63: return $namespaces->isEmpty() ? NULL : $namespaces[0];
64: }
65:
66: 67: 68: 69: 70: 71: 72:
73: public function getNamespaceNames($absolute = FALSE) {
74: $iterator = function(NamespaceNode $ns) use ($absolute) {
75: $name = $ns->getName();
76: return $absolute ? $name->getAbsolutePath() : $name->getPath();
77: };
78: return array_map($iterator, $this->getNamespaces()->toArray());
79: }
80: }
81: