Skip to content

Commit bfbd07e

Browse files
committed
some refactorings
1 parent f7d5855 commit bfbd07e

19 files changed

+217
-41
lines changed

src/AbstractConstraint.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66
abstract class AbstractConstraint extends Base implements Constraint
77
{
8+
const KEY = '';
9+
810
/** @var Schema */
911
protected $ownerSchema;
1012

src/AbstractFlavour.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
namespace Yaoi\Schema;
4+
5+
6+
class AbstractFlavour extends AbstractConstraint implements Flavour
7+
{
8+
public $value;
9+
10+
public function __construct($value)
11+
{
12+
$this->value = $value;
13+
}
14+
15+
}

src/ArrayFlavour/Items.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44

55
use Yaoi\Schema\AbstractConstraint;
66
use Yaoi\Schema\Schema;
7+
use Yaoi\Schema\Schematic;
78

8-
class Items extends AbstractConstraint
9+
class Items extends AbstractConstraint implements Schematic
910
{
1011
const KEY = 'items';
1112

@@ -27,5 +28,12 @@ public function __construct($schemaValue, Schema $ownerSchema = null)
2728
}
2829
}
2930

31+
public function setOwnerSchema(Schema $ownerSchema)
32+
{
33+
$this->ownerSchema = $ownerSchema;
34+
$this->itemsSchema->setParentSchema($ownerSchema, 'Items');
35+
return $this;
36+
}
37+
3038

3139
}

src/ArrayFlavour/MinItems.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,15 @@
44

55

66
use Yaoi\Schema\AbstractConstraint;
7+
use Yaoi\Schema\Flavour;
78
use Yaoi\Schema\Validator;
89

9-
class MinItems extends AbstractConstraint implements Validator
10+
class MinItems extends AbstractConstraint implements Flavour
1011
{
12+
const KEY = 'minItems';
13+
1114
/** @var int */
12-
private $minItems;
15+
public $minItems;
1316
public function __construct($minItems)
1417
{
1518
$this->minItems = $minItems;

src/ArrayFlavour/UniqueItems.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\ArrayFlavour;
4+
5+
use Yaoi\Schema\AbstractFlavour;
6+
7+
class UniqueItems extends AbstractFlavour
8+
{
9+
10+
}

src/CodeBuilder/PHPCodeBuilder.php

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@ class PHPCodeBuilder
1818
{
1919
public $namespace;
2020
public $rootClassName;
21+
2122

2223
public function getSchemaInstantiationCode(Schema $schema)
2324
{
25+
foreach ($schema->getConstraints() as $constraintClass => $constraint) {
26+
27+
}
28+
29+
switch (true) {
30+
case StringType::getFromSchema($schema):
31+
return StringType::className() . '::makeSchema(' . ');';
32+
}
33+
2434
if (ObjectType::getFromSchema($schema)) {
2535
if (Properties::getFromSchema($schema)) {
2636
return $this->getClassName($schema) . '::create()';
@@ -36,10 +46,10 @@ public function getTypeHint(Schema $schema)
3646

3747
}
3848

39-
public function getPhpDoc(Schema $schema)
49+
public function getPhpDocType(Schema $schema)
4050
{
4151
if ($ref = Ref::getFromSchema($schema)) {
42-
return $this->getPhpDoc($ref->constraintSchema);
52+
return $this->getPhpDocType($ref->constraintSchema);
4353
}
4454

4555
switch (true) {
@@ -53,6 +63,9 @@ public function getPhpDoc(Schema $schema)
5363
return 'bool';
5464
case ArrayType::getFromSchema($schema):
5565
return 'array'; // @todo resolve item
66+
case ObjectType::getFromSchema($schema):
67+
$this->resolveObject($schema);
68+
return 'object';
5669
}
5770

5871
//throw new Exception("Please im");
@@ -61,15 +74,29 @@ public function getPhpDoc(Schema $schema)
6174

6275
public $classes = array();
6376

64-
public function makeClass(Schema $schema, $className) {
77+
public function resolveObject(Schema $schema)
78+
{
79+
if (Properties::getFromSchema($schema)) {
80+
81+
82+
}
83+
$path = $schema->getPath();
84+
//print_r($path);
85+
//print_r($schema->getSchemaData());
86+
return;
87+
}
88+
89+
public function makeClass(Schema $schema, $className)
90+
{
6591
if ($objectType = ObjectType::getFromSchema($schema)) {
6692
if ($properties = Properties::getFromSchema($schema)) {
6793
$this->classes[$this->rootClassName] = ClassStructurePhp::create($schema, $this, $className, $this->namespace)->toString();
6894
}
6995
}
7096
}
7197

72-
public function getClassName(Schema $schema) {
98+
public function getClassName(Schema $schema)
99+
{
73100
$className = $this->rootClassName;
74101
if (!isset($this->classes[$className])) {
75102
$this->makeClass($schema, $className);
@@ -129,4 +156,11 @@ public function makeClassName($tracePath)
129156
}
130157

131158

159+
public function storeToDisk($srcPath)
160+
{
161+
print_r($this->classes);
162+
163+
}
164+
165+
132166
}

src/CodeBuilder/Templates/ClassStructurePhp.php

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private function getPhpDocHead()
3636

3737
if ($properties = Properties::getFromSchema($this->schema)) {
3838
foreach ($properties->properties as $name => $property) {
39-
$phpDocType = $this->codeBuilder->getPhpDoc($property);
39+
$phpDocType = $this->codeBuilder->getPhpDocType($property);
4040
$result .= <<<PHPDOC
4141
* @property $phpDocType $$name
4242
@@ -45,21 +45,18 @@ private function getPhpDocHead()
4545
}
4646

4747
return $result;
48-
4948
}
5049

5150
public function toString()
5251
{
5352
$result = '';
5453
$phpDocHead = $this->getPhpDocHead();
55-
5654

5755
$result .= /** @lang PHP */<<<PHP
5856
namespace $this->namespace;
5957
6058
/**
61-
$phpDocHead
62-
*/
59+
$phpDocHead */
6360
class $this->className
6461
{
6562
@@ -68,7 +65,5 @@ class $this->className
6865
6966
PHP;
7067
return $result;
71-
72-
7368
}
7469
}

src/Exception.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,11 @@ public function getStructureTrace()
2626
{
2727
return $this->structureTrace;
2828
}
29+
30+
public $constraint;
31+
public function setConstraint(Constraint $constraint)
32+
{
33+
$this->constraint = $constraint;
34+
return $this;
35+
}
2936
}

src/Flavour.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
namespace Yaoi\Schema;
4+
5+
interface Flavour extends Constraint
6+
{
7+
public function __construct($value);
8+
}

src/NumberFlavour/Minimum.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\NumberFlavour;
4+
5+
use Yaoi\Schema\AbstractFlavour;
6+
7+
class Minimum extends AbstractFlavour
8+
{
9+
10+
}

0 commit comments

Comments
 (0)