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 a class element.
12: */
13: class ClassElement
14: {
15:
16: /**
17: * Holds the name of the class
18: * @var string
19: */
20: public $name;
21:
22: /**
23: * List of platforms where the element is supported.
24: * @var array
25: */
26: public $platforms;
27:
28: /**
29: * Flag that indicates if this class should be treated as a struct.
30: * @var bool
31: */
32: public $struct;
33:
34: /**
35: * Flag that indicates if this class should be treated as a forward
36: * declaration.
37: * @var bool
38: */
39: public $forward_declaration;
40:
41: /**
42: * Flag that indicates if this class has parents.
43: * @var bool
44: */
45: public $has_parent;
46:
47: /**
48: * List of parents.
49: * @var string[]
50: */
51: public $parents;
52:
53: /**
54: * List of member variables.
55: * @var \Peg\Lib\Definitions\Element\ClassVariable[]
56: */
57: public $variables;
58:
59: /**
60: * List of enumerations.
61: * @var \Peg\Lib\Definitions\Element\Enumeration[]
62: */
63: public $enumerations;
64:
65: /**
66: * List of methods.
67: * @var \Peg\Lib\Definitions\Element\FunctionElement[]
68: */
69: public $methods;
70:
71: /**
72: * Description of the element.
73: * @var string
74: */
75: public $description;
76:
77: /**
78: * Reference to the header containing this element.
79: * @var \Peg\Lib\Definitions\Element\Header
80: */
81: public $header;
82:
83: /**
84: * Reference to the namespace containing this element.
85: * @var \Peg\Lib\Definitions\Element\NamespaceElement
86: */
87: public $namespace;
88:
89: /**
90: * Creates a class element.
91: * @param string $name
92: */
93: public function __construct($name, $description="")
94: {
95: $this->name = $name;
96: $this->description = $description;
97:
98: $this->parents = array();
99: $this->variables = array();
100: $this->enumerations = array();
101: $this->methods = array();
102: $this->platforms = array();
103: }
104:
105: /**
106: * Add a parent class.
107: * @param string $parent Name of parent class which may also include its namespace.
108: * @return \Peg\Lib\Definitions\Element\ClassElement
109: */
110: public function AddParent($parent)
111: {
112: $parent = str_replace("::", "\\", $parent);
113:
114: $this->parents[$parent] = $parent;
115: $this->has_parent = true;
116:
117: return $this;
118: }
119:
120: /**
121: * Adds an array of parent classes.
122: * @param array $parents Name of parent classes which may also include its namespace.
123: * @return \Peg\Lib\Definitions\Element\ClassElement
124: */
125: public function AddParents(array $parents)
126: {
127: foreach($parents as $parent)
128: $this->AddParent($parent);
129:
130: return $this;
131: }
132:
133: /**
134: * Adds a variable to the class.
135: * @param \Peg\Lib\Definitions\Element\ClassVariable $variable
136: * @return \Peg\Lib\Definitions\Element\ClassElement
137: */
138: public function AddVariable(\Peg\Lib\Definitions\Element\ClassVariable $variable)
139: {
140: $variable->parent_class =& $this;
141: $this->variables[$variable->name] = $variable;
142:
143: return $this;
144: }
145:
146: /**
147: * Adds an enumeration to the class.
148: * @param \Peg\Lib\Definitions\Element\Enumeration $enumeration
149: * @return \Peg\Lib\Definitions\Element\ClassElement
150: */
151: public function AddEnumeration(\Peg\Lib\Definitions\Element\Enumeration $enumeration)
152: {
153: $enumeration->parent_class =& $this;
154: $this->enumerations[$enumeration->name] = $enumeration;
155:
156: return $this;
157: }
158:
159: /**
160: * Adds a new method/function to the class.
161: * @param \Peg\Lib\Definitions\Element\FunctionElement $method
162: * @return \Peg\Lib\Definitions\Element\ClassElement
163: */
164: public function AddMethod(\Peg\Lib\Definitions\Element\FunctionElement $method)
165: {
166: $method->parent_class =& $this;
167: $this->methods[$method->name] = $method;
168:
169: return $this;
170: }
171:
172: /**
173: * Check of the class as any properties.
174: * @return boolean
175: */
176: public function HasProperties()
177: {
178: if(count($this->variables) > 0)
179: return true;
180:
181: return false;
182: }
183:
184: }