Skip to content

Commit a5ac759

Browse files
committed
Added nullability setting to Type. Fixes
1 parent 78a0082 commit a5ac759

File tree

12 files changed

+123
-79
lines changed

12 files changed

+123
-79
lines changed

app/Building/FieldBuilder.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ class FieldBuilder {
4949
protected $direct = false;
5050

5151
/**
52-
* @var string Type name
52+
* @var Type Type
5353
*/
54-
protected $type = 'mixed';
54+
protected $type = null;
5555

5656
/**
5757
* @var string[] Validators

app/Command/CompileCommand.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,6 @@ protected function execute(InputInterface $input, OutputInterface $output) {
113113

114114
// TODO Config file reading
115115

116-
var_dump($input->getArgument('files'));
117-
118116
foreach ($input->getArgument('files') as $file) {
119117
if (!file_exists($file)) {
120118
$io->error("File \"$file\" is not exists. Skipping");

app/Compiler.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,10 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
124124

125125
$result .= "get_{$fieldModel->name}() { return \$this->{$fieldModel->name}; }\n";
126126

127-
if (!$fieldModel->type->isMixed() || !empty($fieldModel->validation)) {
127+
if (!$fieldModel->type->mixed || !empty($fieldModel->validation)) {
128128
$result .= "protected function validate_{$fieldModel->name}(\$value) {\n";
129129

130-
$result .= $fieldModel->type->makeValidator('$value', "Field {$fieldModel->name} has type {$fieldModel->type->getName()}");
130+
$result .= $fieldModel->type->makeValidator('$value', "Field {$fieldModel->name} has type {$fieldModel->type->name}");
131131

132132
foreach ($fieldModel->validators as $validator) {
133133
if (!isset($this->validators[$validator])) {

app/Data_Type.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
namespace PHPDataGen;
3+
class Data_Type {
4+
use \PHPDataGen\DataClassTrait;
5+
protected $name = null;
6+
private $nullable = null;
7+
private $mixed = null;
8+
private $class = null;
9+
public function __construct(array $init = []) {
10+
foreach ($init as $field => $value) {
11+
$validate = "validate_$field";
12+
$this->$field = $this->$validate($value);
13+
}
14+
}
15+
public function get_name() { return $this->name; }
16+
protected function validate_name($value) {
17+
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
18+
return $value;
19+
}
20+
public function get_nullable() { return $this->nullable; }
21+
protected function validate_nullable($value) {
22+
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field nullable has type bool'); }
23+
return $value;
24+
}
25+
public function get_mixed() { return $this->mixed; }
26+
protected function validate_mixed($value) {
27+
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field mixed has type bool'); }
28+
return $value;
29+
}
30+
public function get_class() { return $this->class; }
31+
protected function validate_class($value) {
32+
if (!is_null($expr) && !is_bool($value)) { throw new \InvalidArgumentException('Field class has type bool'); }
33+
return $value;
34+
}
35+
}

app/Model/Data_ClassModel.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field name has type string'); }
17+
if (!is_null($expr) && !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_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field extends has type array'); }
22+
if (!is_null($expr) && !is_array($value)) { throw new \InvalidArgumentException('Field extends has type array'); }
2323
return $value;
2424
}
2525
public function &get_implements() { return $this->implements; }
2626
protected function validate_implements($value) {
27-
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field implements has type array'); }
27+
if (!is_null($expr) && !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_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field fields has type array'); }
33+
if (!is_null($expr) && !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: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -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_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field name has type string'); }
23+
if (!is_null($expr) && !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_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field editable has type bool'); }
28+
if (!is_null($expr) && !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_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field direct has type bool'); }
33+
if (!is_null($expr) && !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_a($value, \PHPDataGen\Type::class) && !is_null($value)) { throw new \InvalidArgumentException('Field type has type \PHPDataGen\Type'); }
38+
if (!is_null($expr) && !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_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field validators has type array'); }
43+
if (!is_null($expr) && !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_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field filterDefault has type bool'); }
48+
if (!is_null($expr) && !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_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field default has type string'); }
53+
if (!is_null($expr) && !is_string($value)) { throw new \InvalidArgumentException('Field default has type string'); }
5454
return $value;
5555
}
5656
}

app/Model/Data_FileModel.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field namespace has type string'); }
16+
if (!is_null($expr) && !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_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field uses has type array'); }
21+
if (!is_null($expr) && !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_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field classes has type array'); }
26+
if (!is_null($expr) && !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/FileModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function getClassPath(string $className): string {
4242
}
4343

4444
if (isset($this->uses[$className])) {
45-
return $this->uses[$className];
45+
return '\\'.$this->uses[$className];
4646
}
4747

4848
if (is_null($this->namespace)) {

app/Type.pdata

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

app/Type.php

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,50 +28,36 @@
2828

2929
/**
3030
* Type representation
31-
*
32-
* TODO Not nullable
3331
*/
34-
class Type {
32+
class Type extends Data_Type {
3533

3634
const TYPES = ['mixed', 'array', 'bool', 'callable', 'float', 'int', 'iterable', 'string'];
3735

38-
/**
39-
* @var string Type name
40-
*/
41-
protected $name = '';
42-
43-
/**
44-
* @var bool Is field nullable?
45-
*/
46-
protected $nullable = true;
47-
48-
/**
49-
* @var bool Is type mixed?
50-
*/
51-
protected $mixed = false;
52-
53-
/**
54-
* @var bool Is type class?
55-
*/
56-
protected $class = false;
57-
5836
/**
5937
* @param string $type Type name of class
6038
*/
61-
public function __construct(string $type) {
62-
$this->name = $type;
39+
public function __construct(string $type, bool $nullable = true) {
40+
$fields = [
41+
'name' => $type,
42+
43+
'nullable' => $nullable,
44+
'mixed' => false,
45+
'class' => false,
46+
];
6347

6448
$type = strtolower($type);
6549

6650
if ($type === 'mixed') {
67-
$this->mixed = true;
51+
$fields['mixed'] = true;
6852
} else if (!in_array($type, self::TYPES)) {
69-
$this->class = true;
53+
$fields['class'] = true;
7054
}
7155

72-
if (!$this->class) {
73-
$this->name = $type;
56+
if (!$fields['class']) {
57+
$fields['name'] = $type;
7458
}
59+
60+
parent::__construct($fields);
7561
}
7662

7763
/**
@@ -98,7 +84,7 @@ public function getDefaultValue(): string {
9884
return '0';
9985

10086
case 'string':
101-
return '""';
87+
return "''";
10288

10389
}
10490

@@ -120,7 +106,7 @@ public function fixClassName(FileModel $fileModel) {
120106
return;
121107
}
122108

123-
$this->name = '\\'.$fileModel->getClassPath($this->name);
109+
$this->name = $fileModel->getClassPath($this->name);
124110
}
125111

126112
/**
@@ -138,28 +124,21 @@ public function makeValidator(string $expr, string $exception = 'Invalid type'):
138124
return '';
139125
}
140126

141-
$result = 'if (!is_';
127+
$result = 'if (';
142128

129+
if ($this->nullable) {
130+
$result .= '!is_null($expr) && ';
131+
}
132+
133+
$result .= '!is_';
143134
if ($this->class) {
144135
$result .= "a($expr, {$this->name}::class)";
145136
} else {
146137
$result .= "{$this->name}($expr)";
147138
}
148139

149-
$result .= " && !is_null($expr)) { throw new \InvalidArgumentException('$exception'); }";
140+
$result .= ") { throw new \InvalidArgumentException('$exception'); }";
150141

151142
return "$result\n";
152143
}
153-
154-
public function getName(): string {
155-
return $this->name;
156-
}
157-
158-
public function isMixed(): bool {
159-
return $this->mixed;
160-
}
161-
162-
public function isClass(): bool {
163-
return $this->class;
164-
}
165144
}

0 commit comments

Comments
 (0)