Skip to content

Commit 229ce51

Browse files
committed
Added not nullable types syntax
1 parent a5ac759 commit 229ce51

File tree

13 files changed

+85
-69
lines changed

13 files changed

+85
-69
lines changed

app/Building/FieldBuilder.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ class FieldBuilder {
6868
*/
6969
protected $default = null;
7070

71+
public function __construct() {
72+
$this->type = new Type('mixed');
73+
}
74+
7175
/**
7276
* Sets field name
7377
*
@@ -119,7 +123,7 @@ public function setDirect(): FieldBuilder {
119123
*
120124
* @return static $this
121125
*/
122-
public function setType(string $type): FieldBuilder {
126+
public function setType(Type $type): FieldBuilder {
123127
$this->type = $type;
124128
return $this;
125129
}
@@ -181,7 +185,7 @@ public function build(): FieldModel {
181185
'name' => $this->name,
182186
'editable' => $this->editable,
183187
'direct' => $this->direct,
184-
'type' => new Type($this->type),
188+
'type' => $this->type,
185189
'validators' => $this->validators,
186190
'filterDefault' => $this->filterDefault,
187191
'default' => $this->default,

app/Data_Type.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
namespace PHPDataGen;
33
class Data_Type {
44
use \PHPDataGen\DataClassTrait;
5-
protected $name = null;
6-
private $nullable = null;
7-
private $mixed = null;
8-
private $class = null;
5+
protected $name = '';
6+
private $nullable = false;
7+
private $mixed = false;
8+
private $class = false;
99
public function __construct(array $init = []) {
1010
foreach ($init as $field => $value) {
1111
$validate = "validate_$field";
@@ -14,22 +14,22 @@ public function __construct(array $init = []) {
1414
}
1515
public function get_name() { return $this->name; }
1616
protected function validate_name($value) {
17-
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
17+
if (!is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
1818
return $value;
1919
}
2020
public function get_nullable() { return $this->nullable; }
2121
protected function validate_nullable($value) {
22-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field nullable has type bool'); }
22+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field nullable has type bool'); }
2323
return $value;
2424
}
2525
public function get_mixed() { return $this->mixed; }
2626
protected function validate_mixed($value) {
27-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field mixed has type bool'); }
27+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field mixed has type bool'); }
2828
return $value;
2929
}
3030
public function get_class() { return $this->class; }
3131
protected function validate_class($value) {
32-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field class has type bool'); }
32+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field class has type bool'); }
3333
return $value;
3434
}
3535
}

app/Model/ClassModel.pdata

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ namespace PHPDataGen\Model;
2424

2525
class ClassModel {
2626

27-
val name: string;
27+
val name: string!;
2828

29-
val extends: array;
30-
var implements: array;
29+
val extends: string;
30+
var implements: array!;
3131

32-
var fields: array;
32+
var fields: array!;
3333
}

app/Model/Data_ClassModel.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22
namespace PHPDataGen\Model;
33
class Data_ClassModel {
44
use \PHPDataGen\DataClassTrait;
5-
private $name = null;
5+
private $name = '';
66
private $extends = null;
7-
private $implements = null;
8-
private $fields = null;
7+
private $implements = [];
8+
private $fields = [];
99
public function __construct(array $init = []) {
1010
foreach ($init as $field => $value) {
1111
$validate = "validate_$field";
@@ -14,23 +14,23 @@ public function __construct(array $init = []) {
1414
}
1515
public function get_name() { return $this->name; }
1616
protected function validate_name($value) {
17-
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
17+
if (!is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
1818
return $value;
1919
}
2020
public function get_extends() { return $this->extends; }
2121
protected function validate_extends($value) {
22-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field extends has type array'); }
22+
if (!is_null($value) && !is_string($value)) { throw new \InvalidArgumentException('Field extends has type string'); }
2323
return $value;
2424
}
2525
public function &get_implements() { return $this->implements; }
2626
protected function validate_implements($value) {
27-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field implements has type array'); }
27+
if (!is_array($value)) { throw new \InvalidArgumentException('Field implements has type array'); }
2828
return $value;
2929
}
3030
public function set_implements($value) { $this->implements = $this->validate_implements($value);}
3131
public function &get_fields() { return $this->fields; }
3232
protected function validate_fields($value) {
33-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field fields has type array'); }
33+
if (!is_array($value)) { throw new \InvalidArgumentException('Field fields has type array'); }
3434
return $value;
3535
}
3636
public function set_fields($value) { $this->fields = $this->validate_fields($value);}

app/Model/Data_FieldModel.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
namespace PHPDataGen\Model;
33
class Data_FieldModel {
44
use \PHPDataGen\DataClassTrait;
5-
private $name = null;
6-
private $editable = null;
7-
private $direct = null;
5+
private $name = '';
6+
private $editable = false;
7+
private $direct = false;
88
private $type = null;
9-
private $validators = null;
10-
private $filterDefault = null;
9+
private $validators = [];
10+
private $filterDefault = false;
1111
private $default = null;
1212
public function __construct(array $init = []) {
1313
$this->editable = $this->validate_editable(false);
@@ -20,37 +20,37 @@ public function __construct(array $init = []) {
2020
}
2121
public function get_name() { return $this->name; }
2222
protected function validate_name($value) {
23-
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
23+
if (!is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
2424
return $value;
2525
}
2626
public function get_editable() { return $this->editable; }
2727
protected function validate_editable($value) {
28-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field editable has type bool'); }
28+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field editable has type bool'); }
2929
return $value;
3030
}
3131
public function get_direct() { return $this->direct; }
3232
protected function validate_direct($value) {
33-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field direct has type bool'); }
33+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field direct has type bool'); }
3434
return $value;
3535
}
3636
public function get_type() { return $this->type; }
3737
protected function validate_type($value) {
38-
if (!is_null($expr) && !is_a($value, \PHPDataGen\Type::class)) { throw new \InvalidArgumentException('Field type has type \PHPDataGen\Type'); }
38+
if (!is_a($value, \PHPDataGen\Type::class)) { throw new \InvalidArgumentException('Field type has type \PHPDataGen\Type'); }
3939
return $value;
4040
}
4141
public function get_validators() { return $this->validators; }
4242
protected function validate_validators($value) {
43-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field validators has type array'); }
43+
if (!is_array($value)) { throw new \InvalidArgumentException('Field validators has type array'); }
4444
return $value;
4545
}
4646
public function get_filterDefault() { return $this->filterDefault; }
4747
protected function validate_filterDefault($value) {
48-
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field filterDefault has type bool'); }
48+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field filterDefault has type bool'); }
4949
return $value;
5050
}
5151
public function get_default() { return $this->default; }
5252
protected function validate_default($value) {
53-
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field default has type string'); }
53+
if (!is_null($value) && !is_string($value)) { throw new \InvalidArgumentException('Field default has type string'); }
5454
return $value;
5555
}
5656
}

app/Model/Data_FileModel.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
class Data_FileModel {
44
use \PHPDataGen\DataClassTrait;
55
private $namespace = null;
6-
private $uses = null;
7-
private $classes = null;
6+
private $uses = [];
7+
private $classes = [];
88
public function __construct(array $init = []) {
99
foreach ($init as $field => $value) {
1010
$validate = "validate_$field";
@@ -13,17 +13,17 @@ public function __construct(array $init = []) {
1313
}
1414
public function get_namespace() { return $this->namespace; }
1515
protected function validate_namespace($value) {
16-
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field namespace has type string'); }
16+
if (!is_null($value) && !is_string($value)) { throw new \InvalidArgumentException('Field namespace has type string'); }
1717
return $value;
1818
}
1919
public function get_uses() { return $this->uses; }
2020
protected function validate_uses($value) {
21-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field uses has type array'); }
21+
if (!is_array($value)) { throw new \InvalidArgumentException('Field uses has type array'); }
2222
return $value;
2323
}
2424
public function &get_classes() { return $this->classes; }
2525
protected function validate_classes($value) {
26-
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field classes has type array'); }
26+
if (!is_array($value)) { throw new \InvalidArgumentException('Field classes has type array'); }
2727
return $value;
2828
}
2929
public function set_classes($value) { $this->classes = $this->validate_classes($value);}

app/Model/FieldModel.pdata

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ use PHPDataGen\Type;
2626

2727
class FieldModel {
2828

29-
val name: string;
29+
val name: string!;
3030

31-
val editable: bool = false;
32-
val direct: bool = false;
31+
val editable: bool! = false;
32+
val direct: bool! = false;
3333

34-
val type: Type;
35-
val validators: array;
34+
val type: Type!;
35+
val validators: array!;
3636

37-
val filterDefault: bool = true;
37+
val filterDefault: bool! = true;
3838
val default: string;
3939
}

app/Model/FileModel.pdata

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ class FileModel {
2626

2727
val namespace: string;
2828

29-
val uses: array;
30-
var classes: array;
29+
val uses: array!;
30+
var classes: array!;
3131
}

app/Parsing/Conveyor.php

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
use PHPDataGen\Exception\ParsingException;
2828

29+
use PHPDataGen\Type;
30+
2931
/**
3032
* Parsing conveyor
3133
*/
@@ -181,10 +183,10 @@ public function readNamespace(): string {
181183
}
182184

183185
/**
184-
* Reads class name from parser conveyor
185-
* Shifts parser coveyor on class name length
186+
* Reads name (classname, field name) from parser conveyor
187+
* Shifts parser coveyor on name length
186188
*
187-
* @return string Class name
189+
* @return string Name
188190
*/
189191
public function readName(): string {
190192
if (preg_match('/^[a-z_]\\w*/i', $this->next, $matches) === 1) {
@@ -197,10 +199,10 @@ public function readName(): string {
197199
}
198200

199201
/**
200-
* Reads extended class name (\Foo\Bar\Baz sequence or regular) from parser conveyor
202+
* Reads extended classname (\Foo\Bar\Baz sequence or regular classname) from parser conveyor
201203
* Shifts parser coveyor on extended class name length
202204
*
203-
* @return string Extended class name
205+
* @return string Extended classname
204206
*/
205207
public function readExtendedClassname(): string {
206208
if (preg_match('/^(\\\\[a-z_]\\w*)+/i', $this->next, $matches) === 1) {
@@ -216,6 +218,16 @@ public function readExtendedClassname(): string {
216218
}
217219
}
218220

221+
/**
222+
* Reads type (extended classname with(-out) "!") from parser conveyor
223+
* Shifts parser coveyor on type length
224+
*
225+
* @return Type Type
226+
*/
227+
public function readType(): Type {
228+
return new Type($this->readExtendedClassname(), !$this->readOperator('!'));
229+
}
230+
219231
/**
220232
* Reads semicolon from parser conveyor
221233
* Shifts parser coveyor on one symbol

app/Parsing/FieldState.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public function step(Conveyor $conveyor): State {
125125
return $this->parent;
126126

127127
case 4:
128-
$this->builder->setType($conveyor->readExtendedClassname());
128+
$this->builder->setType($conveyor->readType());
129129

130130
$this->state = 5;
131131
return $this;

0 commit comments

Comments
 (0)