Skip to content

Commit ead8934

Browse files
committed
Add virtual properties and methods
1 parent bb6cccc commit ead8934

16 files changed

+405
-164
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
.idea
22
index.php
3+
test.php
34
vendor

src/Model/ArgumentModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ public function __construct($name, $type = null, $default = null)
4444
public function toLines()
4545
{
4646
if ($this->type !== null) {
47-
return $this->type . ' ' . $this->name;
47+
return $this->type . ' $' . $this->name;
4848
} else {
49-
return $this->name;
49+
return '$' . $this->name;
5050
}
5151
}
5252

src/Model/BaseMethodModel.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Model;
4+
5+
use Krlove\Generator\RenderableModel;
6+
7+
/**
8+
* Class BaseMethodModel
9+
* @package Krlove\Generator\Model
10+
*/
11+
abstract class BaseMethodModel extends RenderableModel
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $name;
17+
18+
/**
19+
* @var ArgumentModel[]
20+
*/
21+
protected $arguments = [];
22+
23+
/**
24+
* @return string
25+
*/
26+
public function getName()
27+
{
28+
return $this->name;
29+
}
30+
31+
/**
32+
* @param string $name
33+
*
34+
* @return $this
35+
*/
36+
public function setName($name)
37+
{
38+
$this->name = $name;
39+
40+
return $this;
41+
}
42+
43+
/**
44+
* @return ArgumentModel[]
45+
*/
46+
public function getArguments()
47+
{
48+
return $this->arguments;
49+
}
50+
51+
/**
52+
* @param ArgumentModel $argument
53+
*
54+
* @return $this
55+
*/
56+
public function addArgument(ArgumentModel $argument)
57+
{
58+
$this->arguments[] = $argument;
59+
60+
return $this;
61+
}
62+
63+
/**
64+
* @return string
65+
*/
66+
protected function renderArguments()
67+
{
68+
$result = '';
69+
if ($this->arguments) {
70+
$arguments = [];
71+
foreach ($this->arguments as $argument) {
72+
$arguments[] = $argument->render();
73+
}
74+
75+
$result .= implode(', ', $arguments);
76+
}
77+
78+
return $result;
79+
}
80+
}

src/Model/BasePropertyModel.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Model;
4+
5+
use Krlove\Generator\RenderableModel;
6+
7+
/**
8+
* Class BaseProperty
9+
* @package Krlove\Generator\Model
10+
*/
11+
abstract class BasePropertyModel extends RenderableModel
12+
{
13+
/**
14+
* @var string
15+
*/
16+
protected $name;
17+
18+
/**
19+
* @return string
20+
*/
21+
public function getName()
22+
{
23+
return $this->name;
24+
}
25+
26+
/**
27+
* @param string $name
28+
*
29+
* @return $this
30+
*/
31+
public function setName($name)
32+
{
33+
$this->name = $name;
34+
35+
return $this;
36+
}
37+
}

src/Model/ClassModel.php

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Krlove\Generator\Model;
44

5+
use Krlove\Generator\Exception\GeneratorException;
56
use Krlove\Generator\Model\Traits\DocBlockTrait;
67
use 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
}

src/Model/ClassNameModel.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,6 @@ public function toLines()
7171
return $lines;
7272
}
7373

74-
/**
75-
* {@inheritDoc}
76-
*/
77-
protected function validate()
78-
{
79-
if ($this->final && $this->abstract) {
80-
throw new ValidationException('Entity cannot be final and abstract at the same time');
81-
}
82-
83-
return parent::validate();
84-
}
85-
8674
/**
8775
* @return string
8876
*/
@@ -142,4 +130,16 @@ public function addImplements($implements)
142130

143131
return $this;
144132
}
133+
134+
/**
135+
* {@inheritDoc}
136+
*/
137+
protected function validate()
138+
{
139+
if ($this->final && $this->abstract) {
140+
throw new ValidationException('Entity cannot be final and abstract at the same time');
141+
}
142+
143+
return parent::validate();
144+
}
145145
}

src/Model/DocBlockModel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,13 +54,19 @@ public function getContent()
5454
}
5555

5656
/**
57-
* @param string $content
57+
* @param array|string $content
5858
*
5959
* @return $this
6060
*/
6161
public function addContent($content)
6262
{
63-
$this->content[] = $content;
63+
if (is_array($content)) {
64+
foreach ($content as $item) {
65+
$this->addContent($item);
66+
}
67+
} else {
68+
$this->content[] = $content;
69+
}
6470

6571
return $this;
6672
}

0 commit comments

Comments
 (0)