Skip to content

Commit 55bb922

Browse files
committed
Added final (and final final) class modifier
1 parent f0c8c18 commit 55bb922

16 files changed

+244
-188
lines changed

app/Building/ClassBuilder.php

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,16 @@ class ClassBuilder {
3636
*/
3737
protected $name = null;
3838

39+
/**
40+
* @var bool Is class final?
41+
*/
42+
protected $final = false;
43+
44+
/**
45+
* @var bool Is class final final?
46+
*/
47+
protected $finalFinal = false;
48+
3949
/**
4050
* @var string Extended class name
4151
*/
@@ -67,6 +77,34 @@ public function setName(string $name): ClassBuilder {
6777
return $this;
6878
}
6979

80+
/**
81+
* Sets class final
82+
*
83+
* @return static $this
84+
*/
85+
public function setFinal(): ClassBuilder {
86+
if ($this->final) {
87+
throw new \RuntimeException('Class is already set final');
88+
}
89+
90+
$this->final = true;
91+
return $this;
92+
}
93+
94+
/**
95+
* Sets class final final
96+
*
97+
* @return static $this
98+
*/
99+
public function setFinalFinal(): ClassBuilder {
100+
if ($this->finalFinal) {
101+
throw new \RuntimeException('Class is already set final final');
102+
}
103+
104+
$this->finalFinal = true;
105+
return $this;
106+
}
107+
70108
/**
71109
* Sets extended class name
72110
*
@@ -124,10 +162,12 @@ public function addField(FieldBuilder $field): ClassBuilder {
124162
*/
125163
public function build(): ClassModel {
126164
$model = new ClassModel([
127-
'name' => $this->name,
128-
'extends' => $this->extends,
165+
'name' => $this->name,
166+
'final' => $this->final,
167+
'finalFinal' => $this->finalFinal,
168+
'extends' => $this->extends,
129169
'implements' => $this->implements,
130-
'fields' => []
170+
'fields' => []
131171
]);
132172

133173
foreach ($this->fields as $field) {

app/Command/CompileCommand.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,20 @@ protected function compileFile(string $file, OutputStyle $io, Compiler $compiler
9191
return;
9292
}
9393

94-
$resultPath = "{$pathinfo->dirname}/Data_{$pathinfo->filename}.php";
94+
$resultPath = $pathinfo->dirname.'/';
95+
96+
if (count($model->classes) > 1) {
97+
$resultPath .= 'Data_'.$pathinfo->filename;
98+
} else {
99+
if (!$model->classes[0]->final) {
100+
$resultPath .= 'Data_';
101+
}
102+
103+
$resultPath .= $model->classes[0]->name;
104+
}
105+
106+
$resultPath .= '.php';
107+
95108
file_put_contents($resultPath, $result);
96109

97110
$io->success("Written in $resultPath");

app/Compiler.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,27 @@ public function compile(FileModel $fileModel): string {
5454

5555
protected function compileClass(ClassModel $classModel, FileModel $fileModel): string {
5656
// TODO Custom naming
57-
$result = "class Data_{$classModel->name}";
57+
$result = '';
58+
59+
if ($classModel->finalFinal) {
60+
if (!$classModel->final) {
61+
throw new CompilationException('Class cannot be final final and not final');
62+
}
63+
64+
$result .= 'final ';
65+
}
66+
67+
if (!$classModel->final) {
68+
$result .= 'abstract ';
69+
}
70+
71+
$result .= 'class ';
72+
73+
if (!$classModel->final) {
74+
$result .= 'Data_';
75+
}
76+
77+
$result .= $classModel->name;
5878

5979
if (!is_null($classModel->extends)) {
6080
$result .= ' extends '.$fileModel->getClassPath($classModel->extends);

app/Data_Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
namespace PHPDataGen;
3-
class Data_Type {
3+
abstract class Data_Type {
44
use \PHPDataGen\DataClassTrait;
55
protected $name = '';
66
private $nullable = false;

app/Model/ClassModel.pdata

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ SOFTWARE. */
2222

2323
namespace PHPDataGen\Model;
2424

25-
class ClassModel {
25+
final class ClassModel {
2626

2727
val name: string;
2828

29+
var final: bool <= false;
30+
var finalFinal: bool <= false;
31+
2932
val extends: string?;
3033
var implements: array;
3134

app/Model/ClassModel.php

Lines changed: 49 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,51 @@
11
<?php
2-
3-
/* MIT License
4-
5-
Copyright (c) 2018 Eridan Domoratskiy
6-
7-
Permission is hereby granted, free of charge, to any person obtaining a copy
8-
of this software and associated documentation files (the "Software"), to deal
9-
in the Software without restriction, including without limitation the rights
10-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11-
copies of the Software, and to permit persons to whom the Software is
12-
furnished to do so, subject to the following conditions:
13-
14-
The above copyright notice and this permission notice shall be included in all
15-
copies or substantial portions of the Software.
16-
17-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23-
SOFTWARE. */
24-
252
namespace PHPDataGen\Model;
26-
27-
/**
28-
* Class model
29-
*/
30-
class ClassModel extends Data_ClassModel {}
3+
class ClassModel {
4+
use \PHPDataGen\DataClassTrait;
5+
private $name = '';
6+
private $final = false;
7+
private $finalFinal = false;
8+
private $extends = null;
9+
private $implements = [];
10+
private $fields = [];
11+
public function __construct(array $init = []) {
12+
foreach ($init as $field => $value) {
13+
$validate = "validate_$field";
14+
$this->$field = $this->$validate($value);
15+
}
16+
}
17+
public function get_name() { return $this->name; }
18+
protected function validate_name($value) {
19+
if (!is_string($value)) { throw new \InvalidArgumentException('Field name has type string'); }
20+
return $value;
21+
}
22+
public function &get_final() { return $this->final; }
23+
protected function validate_final($value) {
24+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field final has type bool'); }
25+
return $value;
26+
}
27+
public function set_final($value) { $this->final = $this->validate_final($value);}
28+
public function &get_finalFinal() { return $this->finalFinal; }
29+
protected function validate_finalFinal($value) {
30+
if (!is_bool($value)) { throw new \InvalidArgumentException('Field finalFinal has type bool'); }
31+
return $value;
32+
}
33+
public function set_finalFinal($value) { $this->finalFinal = $this->validate_finalFinal($value);}
34+
public function get_extends() { return $this->extends; }
35+
protected function validate_extends($value) {
36+
if (!is_null($value) && !is_string($value)) { throw new \InvalidArgumentException('Field extends has type string'); }
37+
return $value;
38+
}
39+
public function &get_implements() { return $this->implements; }
40+
protected function validate_implements($value) {
41+
if (!is_array($value)) { throw new \InvalidArgumentException('Field implements has type array'); }
42+
return $value;
43+
}
44+
public function set_implements($value) { $this->implements = $this->validate_implements($value);}
45+
public function &get_fields() { return $this->fields; }
46+
protected function validate_fields($value) {
47+
if (!is_array($value)) { throw new \InvalidArgumentException('Field fields has type array'); }
48+
return $value;
49+
}
50+
public function set_fields($value) { $this->fields = $this->validate_fields($value);}
51+
}

app/Model/Data_ClassModel.php

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

app/Model/Data_FieldModel.php

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

app/Model/Data_FileModel.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22
namespace PHPDataGen\Model;
3-
class Data_FileModel {
3+
abstract class Data_FileModel {
44
use \PHPDataGen\DataClassTrait;
55
private $namespace = null;
66
private $uses = [];

app/Model/FieldModel.pdata

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ namespace PHPDataGen\Model;
2424

2525
use PHPDataGen\Type;
2626

27-
class FieldModel {
27+
final class FieldModel {
2828

2929
val name: string;
3030

0 commit comments

Comments
 (0)