1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11:
12:
13:
14:
15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29:
30: class NPhpClassType extends NObject
31: {
32:
33: public $name;
34:
35:
36: public $type = 'class';
37:
38:
39: public $final;
40:
41:
42: public $abstract;
43:
44:
45: public $extends = array();
46:
47:
48: public $implements = array();
49:
50:
51: public $traits = array();
52:
53:
54: public $documents = array();
55:
56:
57: public $consts = array();
58:
59:
60: public $properties = array();
61:
62:
63: public $methods = array();
64:
65:
66: public function __construct($name = NULL)
67: {
68: $this->name = $name;
69: }
70:
71:
72:
73:
74: public function addConst($name, $value)
75: {
76: $this->consts[$name] = $value;
77: return $this;
78: }
79:
80:
81:
82:
83: public function addProperty($name, $value = NULL)
84: {
85: $property = new NPhpProperty;
86: return $this->properties[$name] = $property->setName($name)->setValue($value);
87: }
88:
89:
90:
91:
92: public function addMethod($name)
93: {
94: $method = new NPhpMethod;
95: if ($this->type === 'interface') {
96: $method->setVisibility('')->setBody(FALSE);
97: } else {
98: $method->setVisibility('public');
99: }
100: return $this->methods[$name] = $method->setName($name);
101: }
102:
103:
104:
105: public function __call($name, $args)
106: {
107: return NObjectMixin::callProperty($this, $name, $args);
108: }
109:
110:
111:
112:
113: public function __toString()
114: {
115: $consts = array();
116: foreach ($this->consts as $name => $value) {
117: $consts[] = "const $name = " . NPhpHelpers::dump($value) . ";\n";
118: }
119: $properties = array();
120: foreach ($this->properties as $property) {
121: $properties[] = ($property->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $property->documents)) . "\n */\n" : '')
122: . $property->visibility . ($property->static ? ' static' : '') . ' $' . $property->name
123: . ($property->value === NULL ? '' : ' = ' . NPhpHelpers::dump($property->value))
124: . ";\n";
125: }
126: return NStrings::normalize(
127: ($this->documents ? str_replace("\n", "\n * ", "/**\n" . implode("\n", (array) $this->documents)) . "\n */\n" : '')
128: . ($this->abstract ? 'abstract ' : '')
129: . ($this->final ? 'final ' : '')
130: . $this->type . ' '
131: . $this->name . ' '
132: . ($this->extends ? 'extends ' . implode(', ', (array) $this->extends) . ' ' : '')
133: . ($this->implements ? 'implements ' . implode(', ', (array) $this->implements) . ' ' : '')
134: . "\n{\n\n"
135: . NStrings::indent(
136: ($this->traits ? "use " . implode(', ', (array) $this->traits) . ";\n\n" : '')
137: . ($this->consts ? implode('', $consts) . "\n\n" : '')
138: . ($this->properties ? implode("\n", $properties) . "\n\n" : '')
139: . implode("\n\n\n", $this->methods), 1)
140: . "\n\n}") . "\n";
141: }
142:
143: }
144: