Skip to content

Commit 5c0345c

Browse files
committed
All works except validators. Alpha 0.1!
1 parent 063ecf9 commit 5c0345c

File tree

10 files changed

+232
-154
lines changed

10 files changed

+232
-154
lines changed

FieldModel.php

Lines changed: 0 additions & 76 deletions
This file was deleted.

FieldModel.php-data

Lines changed: 8 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,56 +2,14 @@ namespace PHPDataGen\Model;
22

33
class FieldModel {
44

5-
}
6-
/*
7-
addField(
8-
(new FieldBuilder())->
9-
setName('name')->
10-
setType('string')
11-
)->
12-
13-
addField(
14-
(new FieldBuilder())->
15-
setName('editable')->
16-
setType('bool')->
17-
setDefault('false')
18-
)->
19-
20-
addField(
21-
(new FieldBuilder())->
22-
setName('direct')->
23-
setType('bool')->
24-
setDefault('false')
25-
)->
5+
val name: string;
266

27-
addField(
28-
(new FieldBuilder())->
29-
setName('type')->
30-
setType('string')
31-
)->
7+
val editable: bool = false;
8+
val direct: bool = false;
329

33-
addField(
34-
(new FieldBuilder())->
35-
setName('validators')->
36-
setType('array')
37-
)->
10+
val type: string;
11+
val validators: array;
3812

39-
addField(
40-
(new FieldBuilder())->
41-
setName('hasDefault')->
42-
setType('bool')->
43-
setDefault('false')
44-
)->
45-
46-
addField(
47-
(new FieldBuilder())->
48-
setName('filterDefault')->
49-
setType('bool')->
50-
setDefault('true')
51-
)->
52-
53-
addField(
54-
(new FieldBuilder())->
55-
setName('default')
56-
);
57-
*/
13+
val filterDefault: bool = true;
14+
val default: string;
15+
}

Test.php-data

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
namespace Foo;
1+
class Test {
22

3-
use Foo\Bar;
4-
use Foo\Baz as A;
3+
val A;
4+
val A1: string;
5+
val A2 = "Foo";
6+
val A3 := "Fee";
7+
val A4: string = "Bar";
8+
val A5: string := "Baz";
59

6-
class Test extends \B implements Bar, A {
10+
var B;
11+
var B1: string;
12+
var B2 = "Foo";
13+
var B3 := "Fee";
14+
var B4: string = "Bar";
15+
var B5: string := "Baz";
716

8-
}
9-
10-
class Test1 extends C {
17+
direct val C;
18+
direct val C1: string;
19+
direct val C2 = "Foo";
20+
direct val C3 := "Fee";
21+
direct val C4: string = "Bar";
22+
direct val C5: string := "Baz";
1123

24+
direct var D;
25+
direct var D1: string;
26+
direct var D2 = "Foo";
27+
direct var D3 := "Fee";
28+
direct var D4: string = "Bar";
29+
direct var D5: string := "Baz";
1230
}

app/Building/FieldBuilder.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,6 @@ class FieldBuilder {
5858
*/
5959
protected $validators = [];
6060

61-
/**
62-
* @var bool Has field default value?
63-
*/
64-
protected $hasDefault = false;
65-
6661
/**
6762
* @var bool Apply validators to default value?
6863
*/
@@ -141,8 +136,11 @@ public function setFilterDefault(string $filterDefault): FieldBuilder {
141136
* @return static $this
142137
*/
143138
public function setDefault(string $default): FieldBuilder {
139+
if (isset($this->default)) {
140+
throw new \Exception('Default value is already setted');
141+
}
142+
144143
$this->default = $default;
145-
$this->hasDefault = true;
146144
return $this;
147145
}
148146

@@ -174,7 +172,6 @@ public function build(): FieldModel {
174172
$model->direct = $this->direct;
175173
$model->type = new Type($this->type);
176174
$model->validators = $this->validators;
177-
$model->hasDefault = $this->hasDefault;
178175
$model->filterDefault = $this->filterDefault;
179176
$model->default = $this->default;
180177

app/Compiler.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
8989
$result .= "public function __construct(array \$init = []) {\n";
9090

9191
foreach ($classModel->fields as $fieldModel) {
92-
if (!$fieldModel->hasDefault) {
92+
if (!isset($fieldModel->default)) {
9393
continue;
9494
}
9595

app/Model/FieldModel.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ class FieldModel {
5454
*/
5555
public $validators = [];
5656

57-
/**
58-
* @var bool Has field default value?
59-
*/
60-
public $hasDefault = false;
61-
6257
/**
6358
* @var bool Apply validators to default value?
6459
*/

app/Parsing/ClassState.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,14 @@ public function step(Conveyor $conveyor): State {
125125

126126
case 5:
127127
if (
128-
$conveyor->readOperator('direct') ||
129-
$conveyor->readOperator('val') ||
130-
$conveyor->readOperator('var')
128+
$conveyor->readOperator('direct', false) ||
129+
$conveyor->readOperator('val', false) ||
130+
$conveyor->readOperator('var', false)
131131
) {
132-
$this->state = 6;
133-
134132
$this->field = new FieldState($this);
133+
$this->field->step($conveyor);
134+
135+
$this->state = 6;
135136
return $this->field;
136137
}
137138

@@ -142,6 +143,13 @@ public function step(Conveyor $conveyor): State {
142143
}
143144

144145
throw new \Exception('Close bracket not found');
146+
147+
case 6:
148+
$this->builder->addField($this->field->getBuilder());
149+
$this->field = null;
150+
151+
$this->state = 5;
152+
return $this;
145153
}
146154
}
147155
}

app/Parsing/Conveyor.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ public function move(int $offset) {
9595
* If required is true throws Exception
9696
*
9797
* @param string $operator Operator
98+
* @param bool $skip Skip operator? Default is true
9899
* @param bool $required Is operator required? Default is false
99100
*
100101
* @return Operator finded
101102
*/
102-
public function readOperator(string $operator, bool $required = false): bool {
103+
public function readOperator(string $operator, bool $skip = true, bool $required = false): bool {
103104
if (strpos($this->next, $operator) === 0) {
104-
$this->move(strlen($operator));
105+
if ($skip) {
106+
$this->move(strlen($operator));
107+
}
108+
105109
return true;
106110
}
107111

0 commit comments

Comments
 (0)