Skip to content

Commit 8b71a4e

Browse files
author
Andrew Mischenko
committed
Add modifiers
1 parent a06b8e9 commit 8b71a4e

File tree

7 files changed

+275
-73
lines changed

7 files changed

+275
-73
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?php
2+
3+
namespace Krlove\Generator\Exception;
4+
5+
/**
6+
* Class ValidationException
7+
* @package Krlove\Generator\Exception
8+
*/
9+
class ValidationException extends \Exception
10+
{
11+
}

src/Model/ClassNameModel.php

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Krlove\Generator\Model;
44

5+
use Krlove\Generator\Exception\ValidationException;
56
use Krlove\Generator\RenderableModel;
67

78
/**
@@ -15,6 +16,16 @@ class ClassNameModel extends RenderableModel
1516
*/
1617
protected $name;
1718

19+
/**
20+
* @var boolean
21+
*/
22+
protected $final;
23+
24+
/**
25+
* @var boolean
26+
*/
27+
protected $abstract;
28+
1829
/**
1930
* @var string
2031
*/
@@ -42,7 +53,16 @@ public function __construct($name, $extends = null)
4253
public function toLines()
4354
{
4455
$lines = [];
45-
$name = sprintf('class %s', $this->name);
56+
57+
$name = '';
58+
if ($this->final) {
59+
$name .= 'final ';
60+
}
61+
if ($this->abstract) {
62+
$name .= 'abstract ';
63+
}
64+
$name .= 'class ' . $this->name;
65+
4666
if ($this->extends !== null) {
4767
$name .= sprintf(' extends %s', $this->extends);
4868
}
@@ -56,6 +76,18 @@ public function toLines()
5676
return $lines;
5777
}
5878

79+
/**
80+
* {@inheritDoc}
81+
*/
82+
protected function validate()
83+
{
84+
if ($this->final && $this->abstract) {
85+
throw new ValidationException('Entity cannot be final and abstract at the same time');
86+
}
87+
88+
return parent::validate();
89+
}
90+
5991
/**
6092
* @return string
6193
*/
@@ -115,4 +147,42 @@ public function addImplements($implements)
115147

116148
return $this;
117149
}
150+
151+
/**
152+
* @return boolean
153+
*/
154+
public function isFinal()
155+
{
156+
return $this->final;
157+
}
158+
159+
/**
160+
* @param boolean $final
161+
* @return $this
162+
*/
163+
public function setFinal($final = true)
164+
{
165+
$this->final = boolval($final);
166+
167+
return $this;
168+
}
169+
170+
/**
171+
* @return boolean
172+
*/
173+
public function isAbstract()
174+
{
175+
return $this->abstract;
176+
}
177+
178+
/**
179+
* @param boolean $abstract
180+
* @return $this
181+
*/
182+
public function setAbstract($abstract = true)
183+
{
184+
$this->abstract = boolval($abstract);
185+
186+
return $this;
187+
}
118188
}

src/Model/MethodModel.php

Lines changed: 108 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,41 @@
22

33
namespace Krlove\Generator\Model;
44

5+
use Krlove\Generator\Exception\ValidationException;
6+
use Krlove\Generator\Model\Traits\AccessModifierTrait;
57
use Krlove\Generator\Model\Traits\DocBlockTrait;
6-
use Krlove\Generator\Model\Traits\ModifierTrait;
78
use 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
*/
1415
class 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

Comments
 (0)