Skip to content

Commit ca768df

Browse files
committed
Remade models by generated data-classes
1 parent a6caf1c commit ca768df

15 files changed

+377
-104
lines changed

app/Building/ClassBuilder.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,13 @@ public function addField(FieldBuilder $field): ClassBuilder {
123123
* @return ClassModel
124124
*/
125125
public function build(): ClassModel {
126-
$model = new ClassModel();
126+
$model = new ClassModel([
127+
'name' => $this->name,
128+
'extends' => $this->extends,
129+
'implements' => $this->implements,
130+
'fields' => []
131+
]);
127132

128-
$model->name = $this->name;
129-
$model->extends = $this->extends;
130-
$model->implements = $this->implements;
131-
132-
$model->fields = [];
133133
foreach ($this->fields as $field) {
134134
$model->fields[] = $field->build();
135135
}

app/Building/FieldBuilder.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -177,15 +177,15 @@ public function addValidator(string $validator): FieldBuilder {
177177
* @return FieldModel
178178
*/
179179
public function build(): FieldModel {
180-
$model = new FieldModel();
181-
182-
$model->name = $this->name;
183-
$model->editable = $this->editable;
184-
$model->direct = $this->direct;
185-
$model->type = new Type($this->type);
186-
$model->validators = $this->validators;
187-
$model->filterDefault = $this->filterDefault;
188-
$model->default = $this->default;
180+
$model = new FieldModel([
181+
'name' => $this->name,
182+
'editable' => $this->editable,
183+
'direct' => $this->direct,
184+
'type' => new Type($this->type),
185+
'validators' => $this->validators,
186+
'filterDefault' => $this->filterDefault,
187+
'default' => $this->default,
188+
]);
189189

190190
return $model;
191191
}

app/Building/FileBuilder.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,12 @@ public function addClass(ClassBuilder $class) {
112112
* @return FileModel
113113
*/
114114
public function build(): FileModel {
115-
$model = new FileModel();
115+
$model = new FileModel([
116+
'namespace' => $this->namespace,
117+
'uses' => $this->uses,
118+
'classes' => []
119+
]);
116120

117-
$model->namespace = $this->namespace;
118-
$model->uses = $this->uses;
119-
120-
$model->classes = [];
121121
foreach ($this->classes as $class) {
122122
$model->classes[] = $class->build();
123123
}

app/Model/ClassModel.pdata

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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\Model;
24+
25+
class ClassModel {
26+
27+
val name: string;
28+
29+
val extends: array;
30+
var implements: array;
31+
32+
var fields: array;
33+
}

app/Model/ClassModel.php

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,4 @@
2727
/**
2828
* Class model
2929
*/
30-
class ClassModel {
31-
32-
/**
33-
* @var string Class name
34-
*/
35-
public $name = null;
36-
37-
/**
38-
* @var string Extended class name
39-
*/
40-
public $extends = null;
41-
42-
/**
43-
* @var string[] Implemented interface names
44-
*/
45-
public $implements = [];
46-
47-
/**
48-
* @var FieldModel[] Fields contained in file
49-
*/
50-
public $fields = [];
51-
}
30+
class ClassModel extends Data_ClassModel {}

app/Model/Data_ClassModel.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
namespace PHPDataGen\Model;
3+
class Data_ClassModel {
4+
use \PHPDataGen\DataClassTrait;
5+
private $name = null;
6+
private $extends = null;
7+
private $implements = null;
8+
private $fields = 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_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field name has type string'); }
18+
return $value;
19+
}
20+
public function get_extends() { return $this->extends; }
21+
protected function validate_extends($value) {
22+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field extends has type array'); }
23+
return $value;
24+
}
25+
public function &get_implements() { return $this->implements; }
26+
protected function validate_implements($value) {
27+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field implements has type array'); }
28+
return $value;
29+
}
30+
public function set_implements($value) { $this->implements = $this->validate_implements($value);}
31+
public function &get_fields() { return $this->fields; }
32+
protected function validate_fields($value) {
33+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field fields has type array'); }
34+
return $value;
35+
}
36+
public function set_fields($value) { $this->fields = $this->validate_fields($value);}
37+
}

app/Model/Data_FieldModel.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
namespace PHPDataGen\Model;
3+
class Data_FieldModel {
4+
use \PHPDataGen\DataClassTrait;
5+
private $name = null;
6+
private $editable = null;
7+
private $direct = null;
8+
private $type = null;
9+
private $validators = null;
10+
private $filterDefault = null;
11+
private $default = null;
12+
public function __construct(array $init = []) {
13+
$this->editable = $this->validate_editable(false);
14+
$this->direct = $this->validate_direct(false);
15+
$this->filterDefault = $this->validate_filterDefault(true);
16+
foreach ($init as $field => $value) {
17+
$validate = "validate_$field";
18+
$this->$field = $this->$validate($value);
19+
}
20+
}
21+
public function get_name() { return $this->name; }
22+
protected function validate_name($value) {
23+
if (!is_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field name has type string'); }
24+
return $value;
25+
}
26+
public function get_editable() { return $this->editable; }
27+
protected function validate_editable($value) {
28+
if (!is_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field editable has type bool'); }
29+
return $value;
30+
}
31+
public function get_direct() { return $this->direct; }
32+
protected function validate_direct($value) {
33+
if (!is_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field direct has type bool'); }
34+
return $value;
35+
}
36+
public function get_type() { return $this->type; }
37+
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'); }
39+
return $value;
40+
}
41+
public function get_validators() { return $this->validators; }
42+
protected function validate_validators($value) {
43+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field validators has type array'); }
44+
return $value;
45+
}
46+
public function get_filterDefault() { return $this->filterDefault; }
47+
protected function validate_filterDefault($value) {
48+
if (!is_bool($value) && !is_null($value)) { throw new \InvalidArgumentException('Field filterDefault has type bool'); }
49+
return $value;
50+
}
51+
public function get_default() { return $this->default; }
52+
protected function validate_default($value) {
53+
if (!is_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field default has type string'); }
54+
return $value;
55+
}
56+
}

app/Model/Data_FileModel.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
namespace PHPDataGen\Model;
3+
class Data_FileModel {
4+
use \PHPDataGen\DataClassTrait;
5+
private $namespace = null;
6+
private $uses = null;
7+
private $classes = null;
8+
public function __construct(array $init = []) {
9+
foreach ($init as $field => $value) {
10+
$validate = "validate_$field";
11+
$this->$field = $this->$validate($value);
12+
}
13+
}
14+
public function get_namespace() { return $this->namespace; }
15+
protected function validate_namespace($value) {
16+
if (!is_string($value) && !is_null($value)) { throw new \InvalidArgumentException('Field namespace has type string'); }
17+
return $value;
18+
}
19+
public function get_uses() { return $this->uses; }
20+
protected function validate_uses($value) {
21+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field uses has type array'); }
22+
return $value;
23+
}
24+
public function &get_classes() { return $this->classes; }
25+
protected function validate_classes($value) {
26+
if (!is_array($value) && !is_null($value)) { throw new \InvalidArgumentException('Field classes has type array'); }
27+
return $value;
28+
}
29+
public function set_classes($value) { $this->classes = $this->validate_classes($value);}
30+
}

app/Model/FieldModel.pdata

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,37 @@
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+
123
namespace PHPDataGen\Model;
224

25+
use PHPDataGen\Type;
26+
327
class FieldModel {
428

529
val name: string;
630

731
val editable: bool = false;
832
val direct: bool = false;
933

10-
val type: string;
34+
val type: Type;
1135
val validators: array;
1236

1337
val filterDefault: bool = true;

app/Model/FieldModel.php

Lines changed: 1 addition & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -27,40 +27,4 @@
2727
/**
2828
* Field model
2929
*/
30-
class FieldModel {
31-
32-
/**
33-
* @var string Field name
34-
*/
35-
public $name = null;
36-
37-
/**
38-
* @var bool Is editable
39-
*/
40-
public $editable = false;
41-
42-
/**
43-
* @var bool Has direct access from self class
44-
*/
45-
public $direct = false;
46-
47-
/**
48-
* @var Type Field type
49-
*/
50-
public $type = 'mixed';
51-
52-
/**
53-
* @var string[] Validators
54-
*/
55-
public $validators = [];
56-
57-
/**
58-
* @var bool Apply validators to default value?
59-
*/
60-
public $filterDefault = true;
61-
62-
/**
63-
* @var string Default value expression
64-
*/
65-
public $default = null;
66-
}
30+
class FieldModel extends Data_FieldModel {}

0 commit comments

Comments
 (0)