Skip to content

Commit f4d1460

Browse files
committed
Added some (not all) Parsing classes
1 parent 0e5fa93 commit f4d1460

File tree

9 files changed

+463
-15
lines changed

9 files changed

+463
-15
lines changed

app/Building/ClassBuilder.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ class ClassBuilder {
5959
* @return static $this
6060
*/
6161
public function setName(string $name): ClassBuilder {
62+
if (!is_null($this->name)) {
63+
throw new \Exception('Name already setted');
64+
}
65+
6266
$this->name = $name;
6367
return $this;
6468
}
@@ -71,6 +75,10 @@ public function setName(string $name): ClassBuilder {
7175
* @return static $this
7276
*/
7377
public function setExtends(string $extends): ClassBuilder {
78+
if (!is_null($this->extends)) {
79+
throw new \Exception('Extends already setted');
80+
}
81+
7482
$this->extends = $extends;
7583
return $this;
7684
}
@@ -82,7 +90,7 @@ public function setExtends(string $extends): ClassBuilder {
8290
*
8391
* @return static $this
8492
*/
85-
public function addImplements(string $interface): ClassBuilder {
93+
public function addImplement(string $interface): ClassBuilder {
8694
if (!in_array($interface, $this->implements)) {
8795
$this->implements[] = $interface;
8896
}

app/Building/FileBuilder.php

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class FileBuilder {
3434
/**
3535
* @var string File namespace
3636
*/
37-
protected $namespace = '';
37+
protected $namespace = null;
3838

3939
/**
4040
* @var array(string => string) File uses (class name => full class name)
@@ -54,30 +54,37 @@ class FileBuilder {
5454
* @return static $this
5555
*/
5656
public function setNamespace(string $namespace) {
57+
if (!is_null($this->namespace)) {
58+
throw new \Exception('Namespace already setted');
59+
}
60+
5761
$this->namespace = $namespace;
5862
return $this;
5963
}
6064

6165
/**
6266
* Adds used class name in uses array
6367
*
64-
* @param string $use Used class name
68+
* @param string $use Used class name
69+
* @param string $alias Alias for class name
6570
*
6671
* @return static $this
6772
*/
68-
public function addUse(string $use) {
69-
$className = explode('\\', $use);
70-
$className = array_pop($className);
73+
public function addUse(string $use, string $alias = null) {
74+
if (is_null($alias)) {
75+
$alias = explode('\\', $use);
76+
$alias = array_pop($alias);
77+
}
7178

72-
if (is_null($className)) {
79+
if (is_null($alias)) {
7380
throw new \UnexpectedValueException("Invalid use \"$use\"");
7481
}
7582

76-
if (isset($this->uses[$className])) {
77-
throw new \Exception("Class name \"$className\" is already used");
83+
if (isset($this->uses[$alias])) {
84+
throw new \Exception("Class name \"$alias\" is already used");
7885
}
7986

80-
$this->uses[$className] = $use;
87+
$this->uses[$alias] = $use;
8188

8289
return $this;
8390
}

app/Compiler.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ class Compiler {
4040
public function compile(FileModel $fileModel): string {
4141
$result = "<?php\n";
4242

43-
// TODO Custom data class location
44-
$result .= "namespace {$fileModel->namespace};\n";
43+
if (!is_null($fileModel->namespace)) {
44+
// TODO Custom data class location
45+
$result .= "namespace {$fileModel->namespace};\n";
46+
}
4547

4648
foreach ($fileModel->classes as $class) {
4749
$result .= $this->compileClass($class, $fileModel)."\n";

app/Model/FileModel.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FileModel {
3232
/**
3333
* @var string File namespace
3434
*/
35-
public $namespace = '';
35+
public $namespace = null;
3636

3737
/**
3838
* @var array(string => string) File uses (class name => full class name)
@@ -60,7 +60,7 @@ public function getClassPath(string $className): string {
6060
return $this->uses[$className];
6161
}
6262

63-
if (empty($this->namespace)) {
63+
if (is_null($this->namespace)) {
6464
return "\\{$className}";
6565
}
6666

app/Parsing/ClassState.php

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
<?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+
25+
namespace PHPDataGen\Parsing;
26+
27+
use PHPDataGen\Building\ClassBuilder;
28+
29+
/**
30+
* Class parsing state
31+
*/
32+
class ClassState extends State {
33+
34+
/**
35+
* @var State Parent state
36+
*/
37+
protected $parent = null;
38+
39+
/**
40+
* @var ClassBuilder Builder
41+
*/
42+
protected $builder = null;
43+
44+
/**
45+
* @var FieldState Last produced field state
46+
*/
47+
protected $field = null;
48+
49+
/**
50+
* @var int Current file parsing state
51+
*
52+
* 0 - Class name
53+
* 1 - After class name
54+
* 2 - Extends
55+
* 3 - Implements
56+
* 4 - After implement
57+
* 5 - Fields
58+
* 6 - Field
59+
*
60+
*/
61+
protected $state = 0;
62+
63+
/**
64+
* @param State $parent Parent state
65+
*/
66+
public function __construct(State $parent) {
67+
$this->builder = new ClassBuilder();
68+
$this->parent = $parent;
69+
}
70+
71+
public function getBuilder(): ClassBuilder {
72+
return $this->builder;
73+
}
74+
75+
public function step(string &$next): State {
76+
switch ($this->state) {
77+
case 0:
78+
$this->builder->setName(Parser::readClassname($next));
79+
80+
$this->state = 1;
81+
return $this;
82+
83+
case 1:
84+
if (strpos($next, 'extends') === 0) {
85+
$this->state = 2;
86+
87+
$next = substr($next, 7);
88+
return $this;
89+
}
90+
91+
if (strpos($next, 'implements') === 0) {
92+
$this->state = 3;
93+
94+
$next = substr($next, 10);
95+
return $this;
96+
}
97+
98+
if (strpos($next, '{') === 0) {
99+
$this->state = 5;
100+
101+
$next = substr($next, 1);
102+
return $this;
103+
}
104+
105+
throw new \Exception('Open bracket not found');
106+
107+
case 2:
108+
$this->builder->setExtends(Parser::readExtendedClassname($next));
109+
110+
$this->state = 1;
111+
return $this;
112+
113+
case 3:
114+
$this->builder->addImplement(Parser::readExtendedClassname($next));
115+
116+
$this->state = 4;
117+
return $this;
118+
119+
case 4:
120+
if (strpos($next, ',') === 0) {
121+
$this->state = 3;
122+
123+
$next = substr($next, 1);
124+
return $this;
125+
}
126+
127+
$this->state = 1;
128+
return $this;
129+
130+
case 5:
131+
if (
132+
strpos($next, 'direct') === 0 ||
133+
strpos($next, 'val') === 0 ||
134+
strpos($next, 'var') === 0
135+
) {
136+
$this->state = 6;
137+
138+
$this->field = new FieldState($this);
139+
return $this->field;
140+
}
141+
142+
if (strpos($next, '}') === 0) {
143+
$next = substr($next, 1);
144+
145+
return $this->parent;
146+
}
147+
148+
throw new \Exception('Close bracket not found');
149+
}
150+
}
151+
}

0 commit comments

Comments
 (0)