Skip to content

Commit c2b7a77

Browse files
committed
some refactorings
1 parent 6f6fa0e commit c2b7a77

18 files changed

+185
-91
lines changed

src/AbstractConstraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function setOwnerSchema(Schema $ownerSchema)
2020
*/
2121
public static function getFromSchema(Schema $schema)
2222
{
23-
$class = self::className();
23+
$class = static::className();
2424
if (isset($schema->constraints[$class])) {
2525
return $schema->constraints[$class];
2626
}

src/AbstractFlavour.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@
33
namespace Yaoi\Schema;
44

55

6-
class AbstractFlavour extends AbstractConstraint implements Flavour
6+
abstract class AbstractFlavour extends AbstractConstraint implements Flavour
77
{
88
public $value;
99

10-
public function __construct($value)
10+
public function __construct($schemaValue, Schema $ownerSchema = null)
1111
{
12-
$this->value = $value;
12+
$this->value = $schemaValue;
1313
}
1414

1515
}

src/ArrayFlavour/MinItems.php

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,13 @@
33
namespace Yaoi\Schema\ArrayFlavour;
44

55

6-
use Yaoi\Schema\AbstractConstraint;
6+
use Yaoi\Schema\AbstractFlavour;
77
use Yaoi\Schema\Flavour;
8-
use Yaoi\Schema\Validator;
98

10-
class MinItems extends AbstractConstraint implements Flavour
9+
class MinItems extends AbstractFlavour implements Flavour
1110
{
1211
public static function getSchemaKey()
1312
{
1413
return 'minItems';
1514
}
16-
17-
/** @var int */
18-
public $minItems;
19-
public function __construct($minItems)
20-
{
21-
$this->minItems = $minItems;
22-
}
23-
24-
25-
public function isValid($data)
26-
{
27-
return count($data) >= $this->minItems;
28-
}
29-
3015
}

src/CodeBuilder/PHPCodeBuilder.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@ class PHPCodeBuilder
2222

2323
public function getSchemaInstantiationCode(Schema $schema)
2424
{
25-
foreach ($schema->getConstraints() as $constraintClass => $constraint) {
26-
27-
}
28-
2925
switch (true) {
3026
case StringType::getFromSchema($schema):
3127
return StringType::className() . '::makeSchema(' . ');';

src/Flavour.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Yaoi\Schema;
44

5-
interface Flavour extends Constraint
5+
interface Flavour extends Schematic
66
{
7-
public function __construct($value);
87
}

src/Schema.php

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Yaoi\Schema\Logic\AllOf;
99
use Yaoi\Schema\ObjectFlavour\AdditionalProperties;
1010
use Yaoi\Schema\ObjectFlavour\Properties;
11+
use Yaoi\Schema\StringFlavour\Format;
1112

1213
/**
1314
* @method static Schema create($schemaValue = null, Schema $parentSchema = null)
@@ -44,7 +45,7 @@ public function getParentSchema()
4445
{
4546
return $this->parentSchema;
4647
}
47-
48+
4849
public function setParentSchema(Schema $parentSchema, $parentName)
4950
{
5051
$this->parentSchema = $parentSchema;
@@ -91,30 +92,10 @@ public function __construct($schemaValue = null, Schema $parentSchema = null)
9192

9293
foreach ($schemaValue as $constraintName => $constraintData) {
9394
$constraint = null;
94-
switch ($constraintName) {
95-
case Type::getSchemaKey():
96-
$constraint = new Type($constraintData, $this);
97-
break;
98-
case Properties::getSchemaKey():
99-
$constraint = new Properties($constraintData, $this);
100-
break;
101-
case AdditionalProperties::getSchemaKey():
102-
$constraint = new AdditionalProperties($constraintData, $this);
103-
break;
104-
case Items::getSchemaKey():
105-
$constraint = new Items($constraintData, $this);
106-
break;
107-
case Ref::getSchemaKey():
108-
$constraint = new Ref($constraintData, $this);
109-
break;
110-
case AllOf::getSchemaKey():
111-
$constraint = new AllOf($constraintData, $this);
112-
break;
113-
case MinItems::getSchemaKey():
114-
$constraint = new MinItems($constraintData);
115-
break;
116-
}
117-
if (null !== $constraint) {
95+
if (isset(self::$constraintKeys[$constraintName])) {
96+
/** @var Schematic $class */
97+
$class = self::$constraintKeys[$constraintName];
98+
$constraint = new $class($constraintData, $this);
11899
$this->setConstraint($constraint);
119100
}
120101
}
@@ -128,7 +109,7 @@ public function setConstraint(Constraint $constraint)
128109
}
129110

130111
static public $debug = false;
131-
112+
132113
public function import($data)
133114
{
134115
if (self::$debug) {
@@ -140,8 +121,7 @@ public function import($data)
140121
if (self::$debug) {
141122
var_dump(get_class($constraint), $data);
142123
}
143-
}
144-
elseif ($constraint instanceof Validator) {
124+
} elseif ($constraint instanceof Validator) {
145125
if (!$constraint->isValid($data)) {
146126
throw new Exception('Validation failed', Exception::INVALID_VALUE);
147127
}
@@ -155,8 +135,7 @@ public function export($data)
155135
foreach ($this->constraints as $constraint) {
156136
if ($constraint instanceof Transformer) {
157137
$data = $constraint->export($data);
158-
}
159-
elseif ($constraint instanceof Validator) {
138+
} elseif ($constraint instanceof Validator) {
160139
if (!$constraint->isValid($data)) {
161140
throw new Exception('Validation failed', Exception::INVALID_VALUE);
162141
}
@@ -182,4 +161,24 @@ public function getConstraints()
182161
{
183162
return $this->constraints;
184163
}
185-
}
164+
165+
166+
private static $constraintKeys;
167+
168+
public static function initConstraintKeys()
169+
{
170+
self::$constraintKeys = array(
171+
Type::getSchemaKey() => Type::className(),
172+
Properties::getSchemaKey() => Properties::className(),
173+
AdditionalProperties::getSchemaKey() => AdditionalProperties::className(),
174+
Items::getSchemaKey() => Items::className(),
175+
Ref::getSchemaKey() => Ref::className(),
176+
AllOf::getSchemaKey() => AllOf::className(),
177+
MinItems::getSchemaKey() => MinItems::className(),
178+
Format::getSchemaKey() => Format::className(),
179+
);
180+
181+
}
182+
}
183+
184+
Schema::initConstraintKeys();

src/StringFlavour/Format.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\StringFlavour;
4+
5+
use Yaoi\Schema\AbstractFlavour;
6+
7+
class Format extends AbstractFlavour
8+
{
9+
public static function getSchemaKey()
10+
{
11+
return 'format';
12+
}
13+
}

src/Type.php

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,48 @@ public static function getSchemaKey()
2121
public function __construct($schemaValue, Schema $ownerSchema = null)
2222
{
2323
$this->ownerSchema = $ownerSchema;
24-
$this->typeHandler = self::factory($schemaValue, $ownerSchema);
24+
if (!is_array($schemaValue)) {
25+
$schemaValue = array($schemaValue);
26+
}
27+
foreach ($schemaValue as $type) {
28+
$this->typeHandlers[$type] = self::factory($type, $ownerSchema);
29+
}
2530
}
2631

2732
/**
28-
* @var AbstractType
33+
* @var AbstractType[]
2934
*/
30-
private $typeHandler;
35+
private $typeHandlers = array();
3136

3237
public function import($data)
3338
{
34-
return $this->typeHandler->import($data);
39+
$lastException = null;
40+
foreach ($this->typeHandlers as $typeHandler) {
41+
try {
42+
return $typeHandler->import($data);
43+
}
44+
catch (Exception $exception) {
45+
$lastException = $exception;
46+
}
47+
}
48+
throw $lastException;
3549
}
3650

3751
public function export($data)
3852
{
39-
return $this->typeHandler->export($data);
53+
$lastException = null;
54+
foreach ($this->typeHandlers as $typeHandler) {
55+
try {
56+
return $typeHandler->export($data);
57+
} catch (Exception $exception) {
58+
$lastException = $exception;
59+
}
60+
}
61+
throw $lastException;
4062
}
4163

4264

43-
public static function factory($schemaValue, Schema $parentSchema = null)
65+
private static function factory($schemaValue, Schema $parentSchema = null)
4466
{
4567
if (is_array($schemaValue)) {
4668
throw new Exception("Please implement me", Exception::NOT_IMPLEMENTED);
@@ -64,6 +86,12 @@ public static function factory($schemaValue, Schema $parentSchema = null)
6486
}
6587
}
6688

89+
public function getHandlerByType($type) {
90+
if (isset($this->typeHandlers[$type])) {
91+
return $this->typeHandlers[$type];
92+
}
93+
return null;
94+
}
6795

6896

6997

src/Types/AbstractType.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@
33
namespace Yaoi\Schema\Types;
44

55
use Yaoi\Schema\AbstractConstraint;
6+
use Yaoi\Schema\Base;
67
use Yaoi\Schema\Constraint;
78
use Yaoi\Schema\Schema;
9+
use Yaoi\Schema\Transformer;
810
use Yaoi\Schema\Type;
911
use Yaoi\Schema\TypeConstraint;
1012

11-
abstract class AbstractType implements TypeConstraint
13+
abstract class AbstractType extends Base implements TypeConstraint, Transformer
1214
{
1315
const TYPE = null;
1416

@@ -38,5 +40,14 @@ public static function makeSchema($constraint = null)
3840
return $schema;
3941
}
4042

43+
public static function getFromSchema(Schema $schema)
44+
{
45+
$type = Type::getFromSchema($schema);
46+
if (null === $type) {
47+
return null;
48+
}
49+
return $type->getHandlerByType(static::TYPE);
50+
}
51+
4152

4253
}

src/Types/ArrayType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use Yaoi\Schema\ArrayFlavour\Items;
88
use Yaoi\Schema\Transformer;
99

10-
class ArrayType extends AbstractType implements Transformer
10+
class ArrayType extends AbstractType
1111
{
1212
const TYPE = 'array';
1313

@@ -17,8 +17,8 @@ protected function validate($data)
1717
throw new Exception('Array expected');
1818
}
1919
if ($minItems = MinItems::getFromSchema($this->ownerSchema)) {
20-
if (count($data) < $minItems->minItems) {
21-
throw new Exception('Not enough items, ' , $minItems->minItems . ' expected');
20+
if (count($data) < $minItems->value) {
21+
throw new Exception('Not enough items, ' , $minItems->value . ' expected');
2222
}
2323
}
2424
}

0 commit comments

Comments
 (0)