Skip to content

Commit 6f6fa0e

Browse files
committed
types refactoring
1 parent 8293976 commit 6f6fa0e

File tree

13 files changed

+75
-24
lines changed

13 files changed

+75
-24
lines changed

src/ArrayFlavour/Items.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,10 @@
88

99
class Items extends AbstractConstraint implements Schematic
1010
{
11-
const KEY = 'items';
11+
public static function getSchemaKey()
12+
{
13+
return 'items';
14+
}
1215

1316
/**
1417
* @var Schema

src/ArrayFlavour/MinItems.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
class MinItems extends AbstractConstraint implements Flavour
1111
{
12-
const KEY = 'minItems';
12+
public static function getSchemaKey()
13+
{
14+
return 'minItems';
15+
}
1316

1417
/** @var int */
1518
public $minItems;

src/Constraint.php

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

55
interface Constraint
66
{
7-
const KEY = '';
8-
7+
public static function getSchemaKey();
98
public function setOwnerSchema(Schema $ownerSchema);
109
}

src/Logic/AllOf.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
class AllOf extends AbstractConstraint implements Transformer, Constraint
1212
{
13-
const KEY = 'allOf';
13+
public static function getSchemaKey()
14+
{
15+
return 'allOf';
16+
}
1417

1518
/** @var Schema[] */
1619
private $composition;

src/ObjectFlavour/AdditionalProperties.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@
99

1010
class AdditionalProperties extends AbstractConstraint implements Schematic
1111
{
12-
const KEY = 'additionalProperties';
12+
public static function getSchemaKey()
13+
{
14+
return 'additionalProperties';
15+
}
1316

1417
/**
1518
* @var Schema

src/ObjectFlavour/Properties.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,10 @@
1010

1111
class Properties extends AbstractConstraint implements Schematic
1212
{
13-
const KEY = 'properties';
13+
public static function getSchemaKey()
14+
{
15+
return 'properties';
16+
}
1417

1518
/**
1619
* @var Schema[]

src/Ref.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55

66
class Ref extends AbstractConstraint implements Transformer, Constraint
77
{
8-
const KEY = '$ref';
8+
public static function getSchemaKey()
9+
{
10+
return '$ref';
11+
}
912

1013
public $ref;
1114

src/Schema.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,25 @@ public function __construct($schemaValue = null, Schema $parentSchema = null)
9292
foreach ($schemaValue as $constraintName => $constraintData) {
9393
$constraint = null;
9494
switch ($constraintName) {
95-
case Type::KEY:
96-
$constraint = Type::factory($constraintData, $this);
95+
case Type::getSchemaKey():
96+
$constraint = new Type($constraintData, $this);
9797
break;
98-
case Properties::KEY:
98+
case Properties::getSchemaKey():
9999
$constraint = new Properties($constraintData, $this);
100100
break;
101-
case AdditionalProperties::KEY:
101+
case AdditionalProperties::getSchemaKey():
102102
$constraint = new AdditionalProperties($constraintData, $this);
103103
break;
104-
case Items::KEY:
104+
case Items::getSchemaKey():
105105
$constraint = new Items($constraintData, $this);
106106
break;
107-
case Ref::KEY:
107+
case Ref::getSchemaKey():
108108
$constraint = new Ref($constraintData, $this);
109109
break;
110-
case AllOf::KEY:
110+
case AllOf::getSchemaKey():
111111
$constraint = new AllOf($constraintData, $this);
112112
break;
113-
case MinItems::KEY:
113+
case MinItems::getSchemaKey():
114114
$constraint = new MinItems($constraintData);
115115
break;
116116
}

src/Structure/ClassStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Yaoi\Schema\Base;
66
use Yaoi\Schema\ObjectFlavour\Properties;
77
use Yaoi\Schema\Schema;
8+
use Yaoi\Schema\Type;
89
use Yaoi\Schema\Types\ObjectType;
910

1011
abstract class ClassStructure extends Base implements ClassStructureContract
@@ -17,7 +18,7 @@ public static function makeSchema()
1718
$schema = new Schema();
1819
$properties = new Properties();
1920
static::setUpProperties($properties, $schema);
20-
$schema->setConstraint(new ObjectType());
21+
$schema->setConstraint(new Type(ObjectType::TYPE));
2122
$schema->setConstraint($properties);
2223
return $schema;
2324
}

src/Type.php

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

55

6+
use Yaoi\Schema\Types\AbstractType;
67
use Yaoi\Schema\Types\ArrayType;
78
use Yaoi\Schema\Types\BooleanType;
89
use Yaoi\Schema\Types\IntegerType;
910
use Yaoi\Schema\Types\NumberType;
1011
use Yaoi\Schema\Types\ObjectType;
1112
use Yaoi\Schema\Types\StringType;
1213

13-
class Type
14+
class Type extends AbstractConstraint implements Transformer
1415
{
15-
const KEY = 'type';
16+
public static function getSchemaKey()
17+
{
18+
return 'type';
19+
}
20+
21+
public function __construct($schemaValue, Schema $ownerSchema = null)
22+
{
23+
$this->ownerSchema = $ownerSchema;
24+
$this->typeHandler = self::factory($schemaValue, $ownerSchema);
25+
}
26+
27+
/**
28+
* @var AbstractType
29+
*/
30+
private $typeHandler;
31+
32+
public function import($data)
33+
{
34+
return $this->typeHandler->import($data);
35+
}
36+
37+
public function export($data)
38+
{
39+
return $this->typeHandler->export($data);
40+
}
41+
1642

1743
public static function factory($schemaValue, Schema $parentSchema = null)
1844
{
@@ -38,5 +64,7 @@ public static function factory($schemaValue, Schema $parentSchema = null)
3864
}
3965
}
4066

67+
68+
4169

4270
}

0 commit comments

Comments
 (0)