Skip to content

Commit a6caf1c

Browse files
committed
Fixed errors in compiled files
1 parent 48219af commit a6caf1c

File tree

4 files changed

+73
-27
lines changed

4 files changed

+73
-27
lines changed

.gitattributes

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
/.gitattributes export-ignore
33
/.gitignore export-ignore
44
/scheme.md export-ignore
5+
6+
*.pdata export-ignore

.gitignore

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
11
/vendor/
22
composer.phar
3-
4-
Data_*.php

app/Compiler.php

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,13 +83,13 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
8383
}
8484

8585
// TODO Direct default value set
86-
$result .= " \${$fieldModel->name} = null;\n";
86+
$result .= " \${$fieldModel->name} = {$fieldModel->type->getDefaultValue()};\n";
8787
}
8888

8989
$result .= "public function __construct(array \$init = []) {\n";
9090

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

@@ -116,11 +116,18 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
116116
foreach ($classModel->fields as $fieldModel) {
117117
$fieldModel->type->fixClassName($fileModel);
118118

119-
$result .= "public function get_{$fieldModel->name}(){$fieldModel->type->makeReturnTypeTip()} { return \$this->{$fieldModel->name}; }\n";
119+
$result .= "public function ";
120+
121+
if ($fieldModel->editable) {
122+
$result .= '&';
123+
}
124+
125+
$result .= "get_{$fieldModel->name}() { return \$this->{$fieldModel->name}; }\n";
120126

121127
if (!$fieldModel->type->isMixed() || !empty($fieldModel->validation)) {
122-
$result .= "protected function validate_{$fieldModel->name}".
123-
"({$fieldModel->type->makeArgumentTypeTip()}\$value){$fieldModel->type->makeReturnTypeTip()} {\n";
128+
$result .= "protected function validate_{$fieldModel->name}(\$value) {\n";
129+
130+
$result .= $fieldModel->type->makeValidator('$value', "Field {$fieldModel->name} has type {$fieldModel->type->getName()}");
124131

125132
foreach ($fieldModel->validators as $validator) {
126133
if (!isset($this->validators[$validator])) {
@@ -134,8 +141,7 @@ protected function compileClass(ClassModel $classModel, FileModel $fileModel): s
134141
}
135142

136143
if ($fieldModel->editable) {
137-
$result .= "public function set_{$fieldModel->name}".
138-
"({$fieldModel->type->makeArgumentTypeTip()}\$value) { ".
144+
$result .= "public function set_{$fieldModel->name}(\$value) { ".
139145
"\$this->{$fieldModel->name} = \$this->validate_{$fieldModel->name}(\$value);".
140146
"}\n";
141147
}

app/Type.php

Lines changed: 58 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828

2929
/**
3030
* Type representation
31+
*
32+
* TODO Not nullable
3133
*/
3234
class Type {
3335

@@ -38,6 +40,11 @@ class Type {
3840
*/
3941
protected $name = '';
4042

43+
/**
44+
* @var bool Is field nullable?
45+
*/
46+
protected $nullable = true;
47+
4148
/**
4249
* @var bool Is type mixed?
4350
*/
@@ -52,15 +59,50 @@ class Type {
5259
* @param string $type Type name of class
5360
*/
5461
public function __construct(string $type) {
55-
$type = strtolower($type);
56-
5762
$this->name = $type;
5863

64+
$type = strtolower($type);
65+
5966
if ($type === 'mixed') {
6067
$this->mixed = true;
6168
} else if (!in_array($type, self::TYPES)) {
6269
$this->class = true;
6370
}
71+
72+
if (!$this->class) {
73+
$this->name = $type;
74+
}
75+
}
76+
77+
/**
78+
* Returns default value for this type
79+
*
80+
* @return string
81+
*/
82+
public function getDefaultValue(): string {
83+
if ($this->nullable || $this->mixed || $this->class) {
84+
return 'null';
85+
}
86+
87+
switch ($this->name) {
88+
case 'array':
89+
return '[]';
90+
91+
case 'bool':
92+
return 'false';
93+
94+
case 'float':
95+
return '0.0';
96+
97+
case 'int':
98+
return '0';
99+
100+
case 'string':
101+
return '""';
102+
103+
}
104+
105+
return 'null';
64106
}
65107

66108
/**
@@ -78,37 +120,35 @@ public function fixClassName(FileModel $fileModel) {
78120
return;
79121
}
80122

81-
$this->name = $fileModel->getClassPath($this->name);
123+
$this->name = '\\'.$fileModel->getClassPath($this->name);
82124
}
83125

84126
/**
85-
* Makes type tip for return
127+
* Makes validation code for this type
86128
*
87129
* If type is mixed returns empty string
88130
*
131+
* @param string $expr Expression for validating
132+
* @param string $exception Message for exception
133+
*
89134
* @return string Type tip prefixed by ": " or empty
90135
*/
91-
public function makeReturnTypeTip(): string {
136+
public function makeValidator(string $expr, string $exception = 'Invalid type'): string {
92137
if ($this->mixed) {
93138
return '';
94139
}
95140

96-
return ': '.$this->name;
97-
}
141+
$result = 'if (!is_';
98142

99-
/**
100-
* Makes type tip for argument
101-
*
102-
* If type is mixed returns empty string
103-
*
104-
* @return string Type tip suffixed by " " or empty
105-
*/
106-
public function makeArgumentTypeTip(): string {
107-
if ($this->mixed) {
108-
return '';
143+
if ($this->class) {
144+
$result .= "a($expr, {$this->name}::class)";
145+
} else {
146+
$result .= "{$this->name}($expr)";
109147
}
110148

111-
return $this->name.' ';
149+
$result .= " && !is_null($expr)) { throw new \InvalidArgumentException('$exception'); }";
150+
151+
return "$result\n";
112152
}
113153

114154
public function getName(): string {

0 commit comments

Comments
 (0)