1: <?php
2: /**
3: * @author Jefferson González
4: * @license MIT
5: * @link http://github.com/peg-org/peg-src Source code.
6: */
7:
8: namespace Peg\Lib\Definitions\Element;
9:
10: /**
11: * Represents an include file with all its elements.
12: */
13: class Header
14: {
15:
16: /**
17: * Holds the name of the file
18: * @var string
19: */
20: public $name;
21:
22: /**
23: * Flag to exclude/include this file when generating the source code.
24: * @var bool
25: */
26: public $enabled;
27:
28: /**
29: * List of namespaces declared on the file.
30: * @var \Peg\Lib\Definitions\Element\NamespaceElement[]
31: */
32: public $namespaces;
33:
34: /**
35: * Initializes a header element.
36: * @param string $name
37: * @param bool $enabled
38: */
39: public function __construct($name, $enabled=true)
40: {
41: $this->name = $name;
42:
43: $this->enabled = $enabled;
44:
45: $this->namespaces = array();
46: }
47:
48: /**
49: * Adds a new constant.
50: * @param \Peg\Lib\Definitions\Element\Constant $constant
51: * @param string $namespace
52: */
53: public function AddConstant(
54: \Peg\Lib\Definitions\Element\Constant $constant,
55: $namespace = "\\"
56: )
57: {
58: $this->CreateNamespace($namespace);
59:
60: $constant->header =& $this;
61: $constant->namespace =& $this->namespaces[$namespace];
62:
63: $this->namespaces[$namespace]->constants[$constant->name] = $constant;
64: }
65:
66: /**
67: * Adds a new enumeration.
68: * @param \Peg\Lib\Definitions\Element\Enumeration $enumeration
69: * @param string $namespace
70: */
71: public function AddEnumeration(
72: \Peg\Lib\Definitions\Element\Enumeration $enumeration,
73: $namespace = "\\"
74: )
75: {
76: $this->CreateNamespace($namespace);
77:
78: $enumeration->header =& $this;
79: $enumeration->namespace =& $this->namespaces[$namespace];
80:
81: $this->namespaces[$namespace]
82: ->enumerations[$enumeration->name] = $enumeration
83: ;
84: }
85:
86: /**
87: * Adds a new enumeration.
88: * @param \Peg\Lib\Definitions\Element\Enumeration $typedef
89: * @param string $namespace
90: */
91: public function AddTypeDef(
92: \Peg\Lib\Definitions\Element\TypeDef $typedef,
93: $namespace = "\\"
94: )
95: {
96: $this->CreateNamespace($namespace);
97:
98: $typedef->header =& $this;
99: $typedef->namespace =& $this->namespaces[$namespace];
100:
101: $this->namespaces[$namespace]
102: ->type_definitions[$typedef->name] = $typedef
103: ;
104: }
105:
106: /**
107: * Adds a new global variable.
108: * @param \Peg\Lib\Definitions\Element\GlobalVariable $global_variable
109: * @param string $namespace
110: */
111: public function AddGlobalVariable(
112: \Peg\Lib\Definitions\Element\GlobalVariable $global_variable,
113: $namespace = "\\"
114: )
115: {
116: $this->CreateNamespace($namespace);
117:
118: $global_variable->header =& $this;
119: $global_variable->namespace =& $this->namespaces[$namespace];
120:
121: $this->namespaces[$namespace]
122: ->global_variables[$global_variable->name] = $global_variable
123: ;
124: }
125:
126: /**
127: * Adds a new function.
128: * @param \Peg\Lib\Definitions\Element\FunctionElement $function
129: * @param string $namespace
130: */
131: public function AddFunction(
132: \Peg\Lib\Definitions\Element\FunctionElement $function,
133: $namespace = "\\"
134: )
135: {
136: $this->CreateNamespace($namespace);
137:
138: $function->header =& $this;
139: $function->namespace =& $this->namespaces[$namespace];
140:
141: $this->namespaces[$namespace]
142: ->functions[$function->name] = $function
143: ;
144: }
145:
146: /**
147: * Adds a new class.
148: * @param \Peg\Lib\Definitions\Element\ClassElement $class
149: * @param string $namespace
150: */
151: public function AddClass(
152: \Peg\Lib\Definitions\Element\ClassElement $class,
153: $namespace = "\\"
154: )
155: {
156: $this->CreateNamespace($namespace);
157:
158: $class->header =& $this;
159: $class->namespace =& $this->namespaces[$namespace];
160:
161: $this->namespaces[$namespace]
162: ->classes[$class->name] = $class
163: ;
164: }
165:
166: /**
167: * Adds a namespace to the namespaces array if not already listed.
168: * @param string $name
169: */
170: private function CreateNamespace(&$name)
171: {
172: if($name == "")
173: $name = "\\";
174:
175: if(!isset($this->namespaces[$name]))
176: $this->namespaces[$name] = new NamespaceElement($name);
177: }
178:
179: /**
180: * Check if the header has constants.
181: * @return bool
182: */
183: public function HasConstants()
184: {
185: foreach($this->namespaces as $namespace)
186: {
187: if($namespace->HasConstants())
188: return true;
189: }
190:
191: return false;
192: }
193:
194: /**
195: * Check if the header has enumerations.
196: * @return bool
197: */
198: public function HasEnumerations()
199: {
200: foreach($this->namespaces as $namespace)
201: {
202: if($namespace->HasEnumerations())
203: return true;
204: }
205:
206: return false;
207: }
208:
209: /**
210: * Check if the header has type definitions.
211: * @return bool
212: */
213: public function HasTypeDefs()
214: {
215: foreach($this->namespaces as $namespace)
216: {
217: if($namespace->HasTypeDefs())
218: return true;
219: }
220:
221: return false;
222: }
223:
224: /**
225: * Check if the header has global variables.
226: * @return bool
227: */
228: public function HasGlobalVariables()
229: {
230: foreach($this->namespaces as $namespace)
231: {
232: if($namespace->HasGlobalVariables())
233: return true;
234: }
235:
236: return false;
237: }
238:
239: /**
240: * Check if the header has functions.
241: * @return bool
242: */
243: public function HasFunctions()
244: {
245: foreach($this->namespaces as $namespace)
246: {
247: if($namespace->HasFunctions())
248: return true;
249: }
250:
251: return false;
252: }
253:
254: /**
255: * Check if the header has classes.
256: * @return bool
257: */
258: public function HasClasses()
259: {
260: foreach($this->namespaces as $namespace)
261: {
262: if($namespace->HasClasses())
263: return true;
264: }
265:
266: return false;
267: }
268:
269: }