22
33namespace Krlove \Generator \Model ;
44
5+ use Krlove \Generator \Exception \ValidationException ;
6+ use Krlove \Generator \Model \Traits \AccessModifierTrait ;
57use Krlove \Generator \Model \Traits \DocBlockTrait ;
6- use Krlove \Generator \Model \Traits \ModifierTrait ;
78use Krlove \Generator \RenderableModel ;
89
910/**
10- * TODO: add support for static and virtual methods
11+ * TODO: add support for virtual methods
1112 * Class PHPClassMethod
1213 * @package Krlove\Generator\Model
1314 */
1415class MethodModel extends RenderableModel
1516{
16- use ModifierTrait ;
17+ use AccessModifierTrait ;
1718 use DocBlockTrait;
1819
1920 /**
2021 * @var string
2122 */
2223 protected $ name ;
2324
25+ /**
26+ * @var boolean
27+ */
28+ protected $ static ;
29+
30+ /**
31+ * @var boolean
32+ */
33+ protected $ final ;
34+
35+ /**
36+ * @var boolean;
37+ */
38+ protected $ abstract ;
39+
2440 /**
2541 * @var ArgumentModel[]
2642 */
@@ -34,12 +50,12 @@ class MethodModel extends RenderableModel
3450 /**
3551 * MethodModel constructor.
3652 * @param string $name
37- * @param string $modifier
53+ * @param string $access
3854 */
39- public function __construct ($ name , $ modifier = 'public ' )
55+ public function __construct ($ name , $ access = 'public ' )
4056 {
4157 $ this ->setName ($ name )
42- ->setModifier ( $ modifier );
58+ ->setAccess ( $ access );
4359 }
4460
4561 /**
@@ -51,7 +67,20 @@ public function toLines()
5167 if ($ this ->docBlock !== null ) {
5268 $ lines [] = $ this ->docBlock ->render ();
5369 }
54- $ function = sprintf ('%s function %s( ' , $ this ->modifier , $ this ->name );
70+
71+ $ function = '' ;
72+ if ($ this ->final ) {
73+ $ function .= 'final ' ;
74+ }
75+ if ($ this ->abstract ) {
76+ $ function .= 'abstract ' ;
77+ }
78+ $ function .= $ this ->access . ' ' ;
79+ if ($ this ->static ) {
80+ $ function .= 'static ' ;
81+ }
82+ $ function .= 'function ' . $ this ->name . '( ' ;
83+
5584 if ($ this ->arguments ) {
5685 $ arguments = [];
5786 foreach ($ this ->arguments as $ argument ) {
@@ -62,16 +91,34 @@ public function toLines()
6291 }
6392 $ function .= ') ' ;
6493
94+ if ($ this ->abstract ) {
95+ $ function .= '; ' ;
96+ }
97+
6598 $ lines [] = $ function ;
66- $ lines [] = '{ ' ;
67- if ($ this ->body ) {
68- $ lines [] = $ this ->body ; // TODO: make body renderable
99+ if (!$ this ->abstract ) {
100+ $ lines [] = '{ ' ;
101+ if ($ this ->body ) {
102+ $ lines [] = $ this ->body ; // TODO: make body renderable
103+ }
104+ $ lines [] = '} ' ;
69105 }
70- $ lines [] = '} ' ;
71106
72107 return $ lines ;
73108 }
74109
110+ /**
111+ * {@inheritDoc}
112+ */
113+ protected function validate ()
114+ {
115+ if ($ this ->abstract and ($ this ->final or $ this ->static )) {
116+ throw new ValidationException ('Entity cannot be abstract and final or static at the same time ' );
117+ }
118+
119+ return parent ::validate ();
120+ }
121+
75122 /**
76123 * @return string
77124 */
@@ -93,41 +140,78 @@ public function setName($name)
93140 }
94141
95142 /**
96- * @return mixed
143+ * @return ArgumentModel[]
97144 */
98- public function getModifier ()
145+ public function getArguments ()
99146 {
100- return $ this ->modifier ;
147+ return $ this ->arguments ;
101148 }
102149
103150 /**
104- * @param mixed $modifier
151+ * @param ArgumentModel $argument
105152 *
106153 * @return $this
107154 */
108- public function setModifier ( $ modifier )
155+ public function addArgument ( ArgumentModel $ argument )
109156 {
110- $ this ->modifier = $ modifier ;
157+ $ this ->arguments [] = $ argument ;
111158
112159 return $ this ;
113160 }
114161
115162 /**
116- * @return RenderableCollection
163+ * @return boolean
117164 */
118- public function getArguments ()
165+ public function isStatic ()
119166 {
120- return $ this ->arguments ;
167+ return $ this ->static ;
121168 }
122169
123170 /**
124- * @param ArgumentModel $argument
125- *
171+ * @param boolean $static
126172 * @return $this
127173 */
128- public function addArgument ( ArgumentModel $ argument )
174+ public function setStatic ( $ static = true )
129175 {
130- $ this ->arguments [] = $ argument ;
176+ $ this ->static = boolval ($ static );
177+
178+ return $ this ;
179+ }
180+
181+ /**
182+ * @return boolean
183+ */
184+ public function isFinal ()
185+ {
186+ return $ this ->final ;
187+ }
188+
189+ /**
190+ * @param boolean $final
191+ * @return $this
192+ */
193+ public function setFinal ($ final = true )
194+ {
195+ $ this ->final = boolval ($ final );
196+
197+ return $ this ;
198+ }
199+
200+ /**
201+ * @return boolean
202+ */
203+ public function isAbstract ()
204+ {
205+ return $ this ->abstract ;
206+ }
207+
208+ /**
209+ * @param boolean $abstract
210+ * @return $this
211+ */
212+ public function setAbstract ($ abstract = true )
213+ {
214+ $ this ->abstract = boolval ($ abstract );
131215
132216 return $ this ;
133217 }
0 commit comments