1: <?php
2: namespace Pharborist\Namespaces;
3:
4: use Pharborist\CommaListNode;
5: use Pharborist\NodeCollection;
6: use Pharborist\StatementNode;
7: use Pharborist\TokenNode;
8:
9: /**
10: * Use declaration statement, importing several classes, functions, or constants
11: * into a namespace.
12: *
13: * Example:
14: * ```
15: * use Jones, Gilliam, Cleese as Idle;
16: * ```
17: */
18: class UseDeclarationStatementNode extends StatementNode {
19: /**
20: * The function keyword.
21: *
22: * This property only has value if the use declaration statement is for
23: * importing a function. Eg. use function MyNamespace\my_func;
24: *
25: * @var TokenNode
26: */
27: protected $useFunction;
28:
29: /**
30: * The const keyword.
31: *
32: * This property only has value if the use declaration statement is for
33: * importing a const. Eg. use const MyNamespace\MY_CONST;
34: *
35: * @var TokenNode
36: */
37: protected $useConst;
38:
39: /**
40: * Test whether use declaration imports a class.
41: *
42: * @param string|NULL $class_name
43: * (Optional) Class name to check if being imported by use statement.
44: *
45: * @return bool
46: * TRUE if this use declaration imports class.
47: */
48: public function importsClass($class_name = NULL) {
49: if ($this->useFunction || $this->useConst) {
50: return FALSE;
51: }
52: if ($class_name) {
53: foreach ($this->getDeclarations() as $declaration) {
54: if ($declaration->getName()->getPath() === $class_name) {
55: return TRUE;
56: }
57: }
58: return FALSE;
59: }
60: else {
61: return TRUE;
62: }
63: }
64:
65: /**
66: * Test whether use declaration imports a function.
67: *
68: * @param string|NULL $function_name
69: * (Optional) Function name to check if being imported by use statement.
70: *
71: * @return bool
72: * TRUE if this use declaration imports function.
73: */
74: public function importsFunction($function_name = NULL) {
75: if (!$this->useFunction) {
76: return FALSE;
77: }
78: if ($function_name) {
79: foreach ($this->getDeclarations() as $declaration) {
80: if ($declaration->getName()->getPath() === $function_name) {
81: return TRUE;
82: }
83: }
84: return FALSE;
85: }
86: else {
87: return TRUE;
88: }
89: }
90:
91: /**
92: * Test whether use declaration imports a constant.
93: *
94: * @param string|NULL $const_name
95: * (Optional) Constant name to check if being imported by use statement.
96: *
97: * @return bool
98: * TRUE if this use declaration imports constant.
99: */
100: public function importsConst($const_name = NULL) {
101: if (!$this->useConst) {
102: return FALSE;
103: }
104: if ($const_name) {
105: foreach ($this->getDeclarations() as $declaration) {
106: if ($declaration->getName()->getPath() === $const_name) {
107: return TRUE;
108: }
109: }
110: return FALSE;
111: }
112: else {
113: return TRUE;
114: }
115: }
116:
117: /**
118: * @var CommaListNode
119: */
120: protected $declarations;
121:
122: /**
123: * @return CommaListNode
124: */
125: public function getDeclarationList() {
126: return $this->declarations;
127: }
128:
129: /**
130: * @return NodeCollection|UseDeclarationNode[]
131: */
132: public function getDeclarations() {
133: return $this->declarations->getItems();
134: }
135: }
136: