Skip to content

Commit 9b02b08

Browse files
committed
better
1 parent 596b4ba commit 9b02b08

File tree

11 files changed

+241
-48
lines changed

11 files changed

+241
-48
lines changed

src/Constraint/Constraint.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55

66
interface Constraint
77
{
8-
public function getConstraintName();
8+
public static function getConstraintName();
99

1010
}

src/Constraint/Definitions.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\Constraint;
4+
5+
6+
use Yaoi\Schema\NG\MagicMap;
7+
use Yaoi\Schema\NG\Schema;
8+
9+
class Definitions extends MagicMap implements Constraint
10+
{
11+
/** @var Schema[] */
12+
protected $_arrayOfData = array();
13+
14+
public static function getConstraintName()
15+
{
16+
return 'definitions';
17+
}
18+
19+
20+
}

src/Constraint/Properties.php

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace Yaoi\Schema\Constraint;
44

5-
use Yaoi\Schema\MagicMap;
6-
use Yaoi\Schema\Schema;
5+
use Yaoi\Schema\NG\MagicMap;
6+
use Yaoi\Schema\NG\Schema;
77
use Yaoi\Schema\Structure\ObjectItem;
88

99
class Properties extends MagicMap implements Constraint
@@ -27,7 +27,7 @@ public static function create()
2727
return new static;
2828
}
2929

30-
public function getConstraintName()
30+
public static function getConstraintName()
3131
{
3232
return 'properties';
3333
}
@@ -40,4 +40,13 @@ public function import($data, ObjectItem $result)
4040
}
4141
}
4242
}
43+
44+
public function export(ObjectItem $data)
45+
{
46+
$result = array();
47+
foreach ($this->_arrayOfData as $key => $value) {
48+
$result[$key] = $value->export($data->$key);
49+
}
50+
return $result;
51+
}
4352
}

src/Constraint/Ref.php

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\Constraint;
4+
5+
use Yaoi\Schema\NG\SchemaLoader;
6+
use Yaoi\Schema\NG\Schema;
7+
8+
class Ref implements Constraint
9+
{
10+
public static function getConstraintName()
11+
{
12+
return '$ref';
13+
}
14+
15+
private $ref;
16+
private $rootSchema;
17+
public function __construct($ref, Schema $rootSchema)
18+
{
19+
$this->ref = $ref;
20+
$this->rootSchema = $rootSchema;
21+
}
22+
23+
/** @var Schema */
24+
private $schema;
25+
26+
27+
/**
28+
* @return Schema
29+
* @throws \Exception
30+
*/
31+
public function getSchema()
32+
{
33+
if (null === $this->schema) {
34+
$this->schema = $this->rootSchema->getDefinition($this->ref);
35+
}
36+
return $this->schema;
37+
}
38+
}

src/Constraint/Type.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public function __construct($type)
2020
$this->types = is_array($type) ? $type : array($type);
2121
}
2222

23-
public function getConstraintName()
23+
public static function getConstraintName()
2424
{
2525
return 'type';
2626
}

src/MagicMap.php renamed to src/NG/MagicMap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Yaoi\Schema;
3+
namespace Yaoi\Schema\NG;
44

55
class MagicMap implements \ArrayAccess
66
{

src/NG/Schema.php

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\NG;
4+
5+
6+
use Yaoi\Schema\Constraint\Definitions;
7+
use Yaoi\Schema\Constraint\Properties;
8+
use Yaoi\Schema\Constraint\Ref;
9+
use Yaoi\Schema\Constraint\Type;
10+
use Yaoi\Schema\Exception;
11+
use Yaoi\Schema\Structure\ObjectItem;
12+
13+
class Schema
14+
{
15+
/** @var Type */
16+
public $type;
17+
18+
/** @var Properties */
19+
public $properties;
20+
21+
/** @var Definitions */
22+
public $definitions;
23+
24+
/** @var Ref */
25+
public $ref;
26+
27+
28+
public function import($data)
29+
{
30+
$result = $data;
31+
if ($this->ref !== null) {
32+
$result = $this->ref->getSchema()->import($data);
33+
}
34+
35+
if ($this->type !== null) {
36+
if (!$this->type->isValid($data)) {
37+
throw new Exception('Invalid type');
38+
}
39+
}
40+
41+
if ($this->properties !== null) {
42+
if (!$result instanceof ObjectItem) {
43+
$result = new ObjectItem();
44+
}
45+
$this->properties->import($data, $result);
46+
}
47+
48+
49+
return $result;
50+
}
51+
52+
53+
public function export($data) {
54+
$result = $data;
55+
if ($this->ref !== null) {
56+
$result = $this->ref->getSchema()->export($data);
57+
}
58+
59+
60+
if ($this->type !== null) {
61+
if (!$this->type->isValid($data)) {
62+
throw new Exception('Invalid type');
63+
}
64+
}
65+
66+
if ($this->properties !== null && ($data instanceof ObjectItem)) {
67+
$result = $this->properties->export($data);
68+
}
69+
70+
return $result;
71+
}
72+
73+
74+
/**
75+
* @param $ref
76+
* @return $this|mixed
77+
* @throws \Exception
78+
*/
79+
public function getDefinition($ref)
80+
{
81+
if ($ref === '#') {
82+
return $this;
83+
}
84+
85+
if (substr($ref, 0, 14) === '#/definitions/') {
86+
$defName = substr($ref, 14);
87+
if (isset($this->definitions[$defName])) {
88+
return $this->definitions[$defName];
89+
}
90+
}
91+
92+
throw new \Exception('Could not resolve ' . $ref);
93+
}
94+
95+
96+
}

src/NG/SchemaLoader.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\NG;
4+
5+
6+
use Yaoi\Schema\Constraint\Definitions;
7+
use Yaoi\Schema\Constraint\Properties;
8+
use Yaoi\Schema\Constraint\Ref;
9+
use Yaoi\Schema\Constraint\Type;
10+
11+
class SchemaLoader
12+
{
13+
private $rootSchema;
14+
15+
public function readSchema($schemaData)
16+
{
17+
$schema = new Schema();
18+
if (null === $this->rootSchema) {
19+
$this->rootSchema = $schema;
20+
}
21+
22+
$definitionsKey = Definitions::getConstraintName();
23+
if (isset($schemaData[$definitionsKey])) {
24+
if (null === $schema->definitions) {
25+
$schema->definitions = new Definitions();
26+
}
27+
foreach ($schemaData[$definitionsKey] as $name => $defData) {
28+
$schema->definitions->__set($name, $this->readSchema($defData));
29+
}
30+
}
31+
32+
if (isset($schemaData[Type::getConstraintName()])) {
33+
$schema->type = new Type($schemaData[Type::getConstraintName()]);
34+
}
35+
36+
if (isset($schemaData[Properties::getConstraintName()])) {
37+
$schema->properties = new Properties();
38+
foreach ($schemaData[Properties::getConstraintName()] as $name => $data) {
39+
$schema->properties->__set($name, $this->readSchema($data));
40+
}
41+
}
42+
43+
if (isset($schemaData[Ref::getConstraintName()])) {
44+
$schema->ref = new Ref($schemaData[Ref::getConstraintName()], $this->rootSchema);
45+
}
46+
47+
return $schema;
48+
}
49+
50+
51+
public function writeSchema()
52+
{
53+
54+
}
55+
56+
}

src/Schema.php

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

tests/src/PHPUnit/RefTest.php

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace Yaoi\Schema\Tests\PHPUnit;
44

55

6-
use Yaoi\Schema\OldSchema;
6+
use Yaoi\Schema\NG\SchemaLoader;
77

88
class RefTest extends \PHPUnit_Framework_TestCase
99
{
@@ -24,8 +24,11 @@ public function testResolve()
2424
),
2525
);
2626

27-
$schema = new OldSchema($schemaData);
28-
$this->assertSame(123, $schema->import(array('one' => 123))->one);
27+
$loader = new SchemaLoader();
28+
$schema = $loader->readSchema($schemaData);
29+
30+
$import = $schema->import(array('one' => 123));
31+
$this->assertSame(123, $import->one);
2932
}
3033

3134

@@ -53,7 +56,10 @@ public function testRootResolve()
5356
),
5457
);
5558

56-
$schema = new OldSchema($schemaData);
59+
60+
$loader = new SchemaLoader();
61+
$schema = $loader->readSchema($schemaData);
62+
5763
$object = $schema->import(array(
5864
'two' => array(
5965
'one' => 123

0 commit comments

Comments
 (0)