22
33namespace Krlove \Generator \Model ;
44
5+ use Krlove \Generator \Exception \GeneratorException ;
56use Krlove \Generator \Model \Traits \DocBlockTrait ;
67use Krlove \Generator \RenderableModel ;
78
@@ -39,12 +40,12 @@ class ClassModel extends RenderableModel
3940 protected $ constants = [];
4041
4142 /**
42- * @var PropertyModel []
43+ * @var BasePropertyModel []
4344 */
4445 protected $ properties = [];
4546
4647 /**
47- * @var MethodModel []
48+ * @var BaseMethodModel []
4849 */
4950 protected $ methods = [];
5051
@@ -61,6 +62,7 @@ public function toLines()
6162 if (count ($ this ->uses ) > 0 ) {
6263 $ lines [] = $ this ->renderArrayLn ($ this ->uses );
6364 }
65+ $ this ->prepareDocBlock ();
6466 if ($ this ->docBlock !== null ) {
6567 $ lines [] = $ this ->ln ($ this ->docBlock ->render ());
6668 }
@@ -71,12 +73,8 @@ public function toLines()
7173 if (count ($ this ->constants ) > 0 ) {
7274 $ lines [] = $ this ->renderArrayLn ($ this ->constants , 4 );
7375 }
74- if (count ($ this ->properties ) > 0 ) {
75- $ lines [] = $ this ->renderArrayLn ($ this ->properties , 4 , str_repeat (PHP_EOL , 2 ));
76- }
77- if (count ($ this ->methods ) > 0 ) {
78- $ lines [] = $ this ->renderArray ($ this ->methods , 4 , str_repeat (PHP_EOL , 2 ));
79- }
76+ $ this ->processProperties ($ lines );
77+ $ this ->processMethods ($ lines );
8078 $ lines [] = $ this ->ln ('} ' );
8179
8280 return $ lines ;
@@ -183,42 +181,97 @@ public function addConstant(ConstantModel $constant)
183181 }
184182
185183 /**
186- * @return PropertyModel []
184+ * @return BasePropertyModel []
187185 */
188186 public function getProperties ()
189187 {
190188 return $ this ->properties ;
191189 }
192190
193191 /**
194- * @param PropertyModel $property
192+ * @param BasePropertyModel $property
195193 *
196194 * @return $this
197195 */
198- public function addProperty (PropertyModel $ property )
196+ public function addProperty (BasePropertyModel $ property )
199197 {
200198 $ this ->properties [] = $ property ;
201199
202200 return $ this ;
203201 }
204202
205203 /**
206- * @return MethodModel []
204+ * @return BaseMethodModel []
207205 */
208206 public function getMethods ()
209207 {
210208 return $ this ->methods ;
211209 }
212210
213211 /**
214- * @param MethodModel
212+ * @param BaseMethodModel
215213 *
216214 * @return $this
217215 */
218- public function addMethod (MethodModel $ method )
216+ public function addMethod (BaseMethodModel $ method )
219217 {
220218 $ this ->methods [] = $ method ;
221219
222220 return $ this ;
223221 }
222+
223+ /**
224+ * Convert virtual properties and methods to DocBlock content
225+ */
226+ protected function prepareDocBlock ()
227+ {
228+ $ content = [];
229+
230+ foreach ($ this ->properties as $ property ) {
231+ if ($ property instanceof VirtualPropertyModel) {
232+ $ content [] = $ property ->toLines ();
233+ }
234+ }
235+
236+ foreach ($ this ->methods as $ method ) {
237+ if ($ method instanceof VirtualMethodModel) {
238+ $ content [] = $ method ->toLines ();
239+ }
240+ }
241+
242+ if ($ content ) {
243+ if ($ this ->docBlock === null ) {
244+ $ this ->docBlock = new DocBlockModel ();
245+ }
246+
247+ $ this ->docBlock ->addContent ($ content );
248+ }
249+ }
250+
251+ /**
252+ * @param array $lines
253+ */
254+ protected function processProperties (&$ lines )
255+ {
256+ $ properties = array_filter ($ this ->properties , function ($ property ) {
257+ return !$ property instanceof VirtualPropertyModel;
258+ });
259+ if (count ($ properties ) > 0 ) {
260+ $ lines [] = $ this ->renderArrayLn ($ properties , 4 , str_repeat (PHP_EOL , 2 ));
261+ }
262+ }
263+
264+ /**
265+ * @param array $lines
266+ * @throws GeneratorException
267+ */
268+ protected function processMethods (&$ lines )
269+ {
270+ $ methods = array_filter ($ this ->methods , function ($ method ) {
271+ return !$ method instanceof VirtualMethodModel;
272+ });
273+ if (count ($ methods ) > 0 ) {
274+ $ lines [] = $ this ->renderArray ($ methods , 4 , str_repeat (PHP_EOL , 2 ));
275+ }
276+ }
224277}
0 commit comments