Skip to content

Commit ba69941

Browse files
committed
Added CompilationException. Improved builders
Added ParsingException calling to Parser::step
1 parent 87cf239 commit ba69941

File tree

6 files changed

+86
-29
lines changed

6 files changed

+86
-29
lines changed

app/Building/ClassBuilder.php

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class ClassBuilder {
6060
*/
6161
public function setName(string $name): ClassBuilder {
6262
if (!is_null($this->name)) {
63-
throw new \Exception('Name already setted');
63+
throw new \RuntimeException('Class name is already specified');
6464
}
6565

6666
$this->name = $name;
@@ -76,7 +76,7 @@ public function setName(string $name): ClassBuilder {
7676
*/
7777
public function setExtends(string $extends): ClassBuilder {
7878
if (!is_null($this->extends)) {
79-
throw new \Exception('Extends already setted');
79+
throw new \RuntimeException('Class extending is already specified');
8080
}
8181

8282
$this->extends = $extends;
@@ -91,10 +91,12 @@ public function setExtends(string $extends): ClassBuilder {
9191
* @return static $this
9292
*/
9393
public function addImplement(string $interface): ClassBuilder {
94-
if (!in_array($interface, $this->implements)) {
95-
$this->implements[] = $interface;
94+
if (in_array($interface, $this->implements)) {
95+
throw new \RuntimeException("Class implementing \"$interface\" is already added");
9696
}
9797

98+
$this->implements[] = $interface;
99+
98100
return $this;
99101
}
100102

@@ -106,10 +108,12 @@ public function addImplement(string $interface): ClassBuilder {
106108
* @return static $this
107109
*/
108110
public function addField(FieldBuilder $field): ClassBuilder {
109-
if (!in_array($field, $this->fields)) {
110-
$this->fields[] = $field;
111+
if (in_array($field, $this->fields)) {
112+
throw new \RuntimeException("Field is already added");
111113
}
112114

115+
$this->fields[] = $field;
116+
113117
return $this;
114118
}
115119

app/Building/FieldBuilder.php

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,39 @@ class FieldBuilder {
7676
* @return static $this
7777
*/
7878
public function setName(string $name): FieldBuilder {
79+
if (isset($this->name)) {
80+
throw new \RuntimeException('Field name is already specified');
81+
}
82+
7983
$this->name = $name;
8084
return $this;
8185
}
8286

8387
/**
84-
* Sets field editable or not
85-
*
86-
* @param bool $editable Is field editable
88+
* Sets field editable
8789
*
8890
* @return static $this
8991
*/
90-
public function setEditable(bool $editable): FieldBuilder {
91-
$this->editable = $editable;
92+
public function setEditable(): FieldBuilder {
93+
if ($this->editable) {
94+
throw new \RuntimeException('Field is already setted editable');
95+
}
96+
97+
$this->editable = true;
9298
return $this;
9399
}
94100

95101
/**
96-
* Sets field direct or not
97-
*
98-
* @param bool $editable Is field direct
102+
* Sets field direct
99103
*
100104
* @return static $this
101105
*/
102-
public function setDirect(bool $direct): FieldBuilder {
103-
$this->direct = $direct;
106+
public function setDirect(): FieldBuilder {
107+
if ($this->direct) {
108+
throw new \RuntimeException('Field is already setted direct');
109+
}
110+
111+
$this->direct = true;
104112
return $this;
105113
}
106114

@@ -117,13 +125,15 @@ public function setType(string $type): FieldBuilder {
117125
}
118126

119127
/**
120-
* Sets filter default value
121-
*
122-
* @param string $filterDefault New filter default field value
128+
* Sets field not filters default value
123129
*
124130
* @return static $this
125131
*/
126-
public function setFilterDefault(string $filterDefault): FieldBuilder {
132+
public function setNotFilterDefault(): FieldBuilder {
133+
if (!$this->filterDefault) {
134+
throw new \RuntimeException('Field is already setted not filters default');
135+
}
136+
127137
$this->filterDefault = $filterDefault;
128138
return $this;
129139
}
@@ -137,7 +147,7 @@ public function setFilterDefault(string $filterDefault): FieldBuilder {
137147
*/
138148
public function setDefault(string $default): FieldBuilder {
139149
if (isset($this->default)) {
140-
throw new \Exception('Default value is already setted');
150+
throw new \RuntimeException('Default value is already specified');
141151
}
142152

143153
$this->default = $default;
@@ -152,10 +162,12 @@ public function setDefault(string $default): FieldBuilder {
152162
* @return static $this
153163
*/
154164
public function addValidator(string $validator): FieldBuilder {
155-
if (!in_array($validator, $this->validators)) {
156-
$this->validators[] = $validator;
165+
if (in_array($validator, $this->validators)) {
166+
throw new \RuntimeException("Validator \"$validator\" is already added");
157167
}
158168

169+
$this->validators[] = $validator;
170+
159171
return $this;
160172
}
161173

app/Building/FileBuilder.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class FileBuilder {
5555
*/
5656
public function setNamespace(string $namespace) {
5757
if (!is_null($this->namespace)) {
58-
throw new \Exception('Namespace already setted');
58+
throw new \RuntimeException('Namespace is already specified');
5959
}
6060

6161
$this->namespace = $namespace;
@@ -81,7 +81,7 @@ public function addUse(string $use, string $alias = null) {
8181
}
8282

8383
if (isset($this->uses[$alias])) {
84-
throw new \Exception("Class name \"$alias\" is already used");
84+
throw new \RuntimeException("Alias \"$alias\" is already used");
8585
}
8686

8787
$this->uses[$alias] = $use;
@@ -97,10 +97,12 @@ public function addUse(string $use, string $alias = null) {
9797
* @return static $this
9898
*/
9999
public function addClass(ClassBuilder $class) {
100-
if (!in_array($class, $this->classes)) {
101-
$this->classes[] = $class;
100+
if (in_array($class, $this->classes)) {
101+
throw new \RuntimeException("Class is already added");
102102
}
103103

104+
$this->classes[] = $class;
105+
104106
return $this;
105107
}
106108

app/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
124124

125125
foreach ($fieldModel->validators as $validator) {
126126
if (!isset($this->validators[$validator])) {
127-
throw new \Exception("Validator \"{$validator}\" is not exists");
127+
throw new CompilationException("Validator \"{$validator}\" is not defined");
128128
}
129129

130130
$result .= "\$value = {$this->validators[$validator]}(\$value);\n";
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
/* MIT License
4+
5+
Copyright (c) 2018 Eridan Domoratskiy
6+
7+
Permission is hereby granted, free of charge, to any person obtaining a copy
8+
of this software and associated documentation files (the "Software"), to deal
9+
in the Software without restriction, including without limitation the rights
10+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
copies of the Software, and to permit persons to whom the Software is
12+
furnished to do so, subject to the following conditions:
13+
14+
The above copyright notice and this permission notice shall be included in all
15+
copies or substantial portions of the Software.
16+
17+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23+
SOFTWARE. */
24+
25+
namespace PHPDataGen\Exception;
26+
27+
/**
28+
* Compilation exception
29+
*/
30+
class CompilationException extends \Exception {}

app/Parsing/Parser.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,16 @@ public function parse() {
6161
*/
6262
public function step() {
6363
$this->conveyor->skipSpaces();
64-
$this->state = $this->state->step($this->conveyor);
64+
65+
try {
66+
$this->state = $this->state->step($this->conveyor);
67+
} catch (\Throwable $e) {
68+
if (is_a($e, ParsingException::class)) {
69+
throw $e;
70+
}
71+
72+
throw $this->conveyor->makeException($e->getMessage(), $e->getCode(), $e);
73+
}
6574

6675
$this->conveyor->skipSpaces();
6776
$this->conveyor->skipComment();

0 commit comments

Comments
 (0)