1: <?php
2:
3: namespace webfilesframework\codegeneration\php;
4:
5: use webfilesframework\codegeneration\general\MAbstractClass;
6: use webfilesframework\MWebfilesFrameworkException;
7:
8: 9: 10: 11: 12: 13:
14: class MPhpWebfileClass extends MAbstractClass
15: {
16:
17: public function generateCode()
18: {
19:
20: $this->generateSetterAndGetter();
21:
22: $code = parent::generateCode();
23: return $code;
24: }
25:
26: protected function generatePreambleCode()
27: {
28: return "<?php \n";
29: }
30:
31: protected function generateHeaderCode()
32: {
33:
34:
35: return "class " . $this->className . " extends MWebfile { \n\n";
36: }
37:
38: protected function generateAttributes()
39: {
40:
41: $code = "";
42:
43:
44: foreach ($this->attributes as $attribute) {
45:
46: if (!$attribute instanceof MPhpClassAttribute) {
47: throw new MWebfilesFrameworkException("Cannot generate code for attribute which is not of type 'MPhpClassAttribute'.");
48: }
49:
50:
51: $modifiedAttribute = new MPhpClassAttribute($attribute->getVisibility(), "m_" . $attribute->getName(), $attribute->getType());
52:
53: $code .= $modifiedAttribute->generateCode() . "\n";
54: }
55:
56: return $code;
57: }
58:
59: protected function generateFooterCode()
60: {
61: return "}";
62: }
63:
64: public function addAttribute(MPhpClassAttribute $attribute)
65: {
66: $this->attributes[] = $attribute;
67: }
68:
69: public function addMethod(MPhpClassMethod $method)
70: {
71: $this->methods[] = $method;
72: }
73:
74: private function generateSetterAndGetter()
75: {
76: foreach ($this->attributes as $attribute) {
77:
78: $firstLetterUppercaseAttribute = ucfirst($attribute);
79:
80:
81: $getterMethodName = "get" . $firstLetterUppercaseAttribute;
82: $getterContent = "return \$this->m_" . $attribute . ";";
83: $getterMethod = new MPhpClassMethod("public", $getterMethodName, $getterContent);
84: $this->addMethod($getterMethod);
85:
86:
87: $setterMethodName = "set" . $firstLetterUppercaseAttribute;
88: $setterContent = "\$this->m_" . $attribute . " = $" . $attribute . ";";
89: $setterMethod = new MPhpClassMethod("public", $setterMethodName, $setterContent);
90: $setterMethod->addParameter(new MPhpClassMethodParameter($attribute, ""));
91: $this->addMethod($setterMethod);
92:
93: }
94: }
95: }