Skip to content

Commit 4655d1d

Browse files
committed
type object
1 parent bc39e9c commit 4655d1d

File tree

15 files changed

+141
-49
lines changed

15 files changed

+141
-49
lines changed

src/Constraint.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function setOwnerSchema(Schema $ownerSchema);
1616

1717

1818
/**
19-
* On import raw json data is available to validate against all of constraints.
19+
* On import raw json data ($data) is available to be validated against all of constraints.
2020
* Constraint stores result in referenced variable (raw data value by default).
2121
* Some constraints may alter result, e.g. `Properties` would create object and fill properties values.
2222
*
@@ -27,12 +27,14 @@ public function setOwnerSchema(Schema $ownerSchema);
2727
public function importFailed($data, &$entity);
2828

2929
/**
30+
* On export php data ($entity) is available to be validated against all the constraints.
31+
* Constraint stores result in referenced variable ($data).
3032
*
3133
* @param $data
3234
* @param $entity
3335
* @return mixed
3436
*/
35-
public function exportFailed($data, &$entity);
37+
public function exportFailed($entity, &$data);
3638

3739

3840
/**

src/Constraint/AllOf.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,10 @@ public function import($data)
3939
return $data;
4040
}
4141

42-
public function export($data)
42+
public function export($entity)
4343
{
4444
// TODO: Implement export() method.
45-
return $data;
45+
return $entity;
4646
}
4747

4848

src/Constraint/Properties.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Yaoi\Schema\Constraint;
77
use Yaoi\Schema\Exception;
88
use Yaoi\Schema\Schema;
9+
use Yaoi\Schema\Structure\ObjectItem;
910

1011
class Properties extends AbstractConstraint
1112
{
@@ -92,17 +93,19 @@ public function hasProperty($name)
9293
*/
9394
public function importFailed($data, &$result)
9495
{
95-
$result = new \stdClass();
96-
96+
if (!$result instanceof ObjectItem) {
97+
$result = new ObjectItem();
98+
}
9799
foreach ($this->properties as $name => $property) {
98-
99-
100+
$value = $property->import($data[$name]);
101+
$result->setProperty($name, $value);
100102
}
101103

104+
102105
return false;
103106
}
104107

105-
public function exportFailed($data, &$entity)
108+
public function exportFailed($entity, &$data)
106109
{
107110
// TODO: Implement exportFailed() method.
108111
}

src/Constraint/Type.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
use Yaoi\Schema\AbstractConstraint;
66
use Yaoi\Schema\Schema;
7+
use Yaoi\Schema\Structure\ObjectItem;
78

89
class Type extends AbstractConstraint
910
{
@@ -37,7 +38,7 @@ public function importFailed($data, &$entity)
3738
case self::OBJECT:
3839
$ok = is_object($data) || is_array($data);
3940
if ($ok && !is_object($entity)) {
40-
$entity = (object)$entity;
41+
$entity = new ObjectItem($data);
4142
}
4243
break;
4344
case self::_ARRAY:
@@ -66,15 +67,15 @@ public function importFailed($data, &$entity)
6667
return 'Wrong type';
6768
}
6869

69-
public function exportFailed($data, &$entity)
70+
public function exportFailed($entity, &$data)
7071
{
7172
$ok = false;
7273
foreach ($this->types as $type) {
7374
switch ($type) {
7475
case self::OBJECT:
7576
$ok = is_object($data) || is_array($data);
76-
if ($ok && !is_array($entity)) {
77-
$entity = (array)$entity;
77+
if ($ok && $entity instanceof ObjectItem) {
78+
$data = $entity->getUnmatchedProperties();
7879
}
7980
break;
8081
case self::_ARRAY:

src/Schema.php

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,35 +120,35 @@ public function import($data, $deepValidation = false)
120120
if (self::$debug) {
121121
print_r($data);
122122
}
123-
$result = $data;
123+
$entity = $data;
124124
foreach ($this->constraints as $constraint) {
125-
$failReason = $constraint->importFailed($data, $result);
125+
$failReason = $constraint->importFailed($data, $entity);
126126
if ($failReason && !$deepValidation) {
127127
throw new Exception($failReason, Exception::INVALID_VALUE);
128128
}
129129
if (self::$debug) {
130130
var_dump(get_class($constraint), $data);
131131
}
132132
}
133-
return $result;
133+
return $entity;
134134
}
135135

136-
public function export($data, $deepValidation = false)
136+
public function export($entity, $deepValidation = false)
137137
{
138138
if (self::$debug) {
139-
print_r($data);
139+
print_r($entity);
140140
}
141-
$result = $data;
141+
$data = $entity;
142142
foreach ($this->constraints as $constraint) {
143-
$failReason = $constraint->exportFailed($data, $result);
143+
$failReason = $constraint->exportFailed($entity, $data);
144144
if ($failReason && !$deepValidation) {
145145
throw new Exception($failReason, Exception::INVALID_VALUE);
146146
}
147147
if (self::$debug) {
148-
var_dump(get_class($constraint), $data);
148+
var_dump(get_class($constraint), $entity);
149149
}
150150
}
151-
return $result;
151+
return $data;
152152
}
153153

154154

src/Structure/ObjectItem.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\Structure;
4+
5+
6+
class ObjectItem
7+
{
8+
private $unmatchedProperties;
9+
private $additionalProperties = array();
10+
private $properties = array();
11+
12+
public function __construct($unmatchedProperties = array())
13+
{
14+
$this->unmatchedProperties = $unmatchedProperties;
15+
}
16+
17+
public function getAdditionalProperties()
18+
{
19+
return $this->additionalProperties;
20+
}
21+
22+
public function getUnmatchedProperties()
23+
{
24+
return $this->unmatchedProperties; // todo check for empty array and return new stdClass
25+
}
26+
27+
public function __get($name)
28+
{
29+
if (array_key_exists($name, $this->unmatchedProperties)) {
30+
return $this->unmatchedProperties[$name];
31+
} elseif (array_key_exists($name, $this->properties)) {
32+
return $this->properties[$name];
33+
} elseif (array_key_exists($name, $this->additionalProperties)) {
34+
return $this->additionalProperties[$name];
35+
}
36+
37+
return null;
38+
}
39+
40+
public function setProperty($name, $value)
41+
{
42+
$this->properties[$name] = $value;
43+
unset($this->unmatchedProperties[$name]);
44+
}
45+
46+
public function setAdditionalProperty($name, $value)
47+
{
48+
$this->additionalProperties[$name] = $value;
49+
unset($this->unmatchedProperties[$name]);
50+
}
51+
52+
public function hasUnmatchedPproperties()
53+
{
54+
return !empty($this->unmatchedProperties);
55+
}
56+
}

src/Transformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,5 @@
66
interface Transformer
77
{
88
public function import($data);
9-
public function export($data);
9+
public function export($entity);
1010
}

src/Types/ArrayType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,19 +44,19 @@ public function import($data)
4444
return $result;
4545
}
4646

47-
public function export($data)
47+
public function export($entity)
4848
{
49-
$this->validate($data);
49+
$this->validate($entity);
5050
$result = array();
5151
if ($items = Items::getFromSchema($this->ownerSchema)) {
52-
foreach ($data as $name => $value) {
52+
foreach ($entity as $name => $value) {
5353
try {
5454
$result[$name] = $items->itemsSchema->export($value);
5555
} catch (Exception $exception) {
5656
$exception->pushStructureTrace('Items:' . $name);
5757
throw $exception;
5858
}
59-
unset($data[$name]);
59+
unset($entity[$name]);
6060
}
6161
}
6262
return $result;

src/Types/BooleanType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@ public function import($data)
1515
return $data;
1616
}
1717

18-
public function export($data)
18+
public function export($entity)
1919
{
20-
$this->validate($data);
21-
return $data;
20+
$this->validate($entity);
21+
return $entity;
2222
}
2323

2424
public function validate($data)

src/Types/IntegerType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public function import($data)
1414
return $data;
1515
}
1616

17-
public function export($data)
17+
public function export($entity)
1818
{
19-
$this->validate($data);
20-
return $data;
19+
$this->validate($entity);
20+
return $entity;
2121
}
2222

2323

0 commit comments

Comments
 (0)