Skip to content

Commit 8d6095a

Browse files
committed
Merge remote-tracking branch 'origin/simplification' into simplification
# Conflicts: # src/Constraint.php # src/README.md
2 parents e0405fc + 4655d1d commit 8d6095a

17 files changed

+227
-49
lines changed

src/Constraint.php

Lines changed: 3 additions & 1 deletion
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,6 +27,8 @@ 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

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: 53 additions & 9 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
{
@@ -22,6 +23,7 @@ public static function getSchemaKey()
2223
}
2324

2425
private $types;
26+
2527
public function __construct($schemaValue, Schema $ownerSchema = null)
2628
{
2729
$this->ownerSchema = $ownerSchema;
@@ -34,19 +36,29 @@ public function importFailed($data, &$entity)
3436
foreach ($this->types as $type) {
3537
switch ($type) {
3638
case self::OBJECT:
37-
$ok = is_object($data) || is_array($data);break;
39+
$ok = is_object($data) || is_array($data);
40+
if ($ok && !is_object($entity)) {
41+
$entity = new ObjectItem($data);
42+
}
43+
break;
3844
case self::_ARRAY:
39-
$ok = is_array($data);break;
45+
$ok = is_array($data);
46+
break;
4047
case self::STRING:
41-
$ok = is_string($data);break;
48+
$ok = is_string($data);
49+
break;
4250
case self::INTEGER:
43-
$ok = is_int($data);break;
51+
$ok = is_int($data);
52+
break;
4453
case self::NUMBER:
45-
$ok = is_int($data) || is_float($data);break;
54+
$ok = is_int($data) || is_float($data);
55+
break;
4656
case self::BOOLEAN:
47-
$ok = is_bool($data);break;
57+
$ok = is_bool($data);
58+
break;
4859
case self::NULL:
49-
$ok = null === $data;break;
60+
$ok = null === $data;
61+
break;
5062
}
5163
if ($ok) {
5264
return false;
@@ -55,9 +67,41 @@ public function importFailed($data, &$entity)
5567
return 'Wrong type';
5668
}
5769

58-
public function exportFailed($data, &$entity)
70+
public function exportFailed($entity, &$data)
5971
{
60-
return $this->importFailed($data, $entity);
72+
$ok = false;
73+
foreach ($this->types as $type) {
74+
switch ($type) {
75+
case self::OBJECT:
76+
$ok = is_object($data) || is_array($data);
77+
if ($ok && $entity instanceof ObjectItem) {
78+
$data = $entity->getUnmatchedProperties();
79+
}
80+
break;
81+
case self::_ARRAY:
82+
$ok = is_array($data);
83+
break;
84+
case self::STRING:
85+
$ok = is_string($data);
86+
break;
87+
case self::INTEGER:
88+
$ok = is_int($data);
89+
break;
90+
case self::NUMBER:
91+
$ok = is_int($data) || is_float($data);
92+
break;
93+
case self::BOOLEAN:
94+
$ok = is_bool($data);
95+
break;
96+
case self::NULL:
97+
$ok = null === $data;
98+
break;
99+
}
100+
if ($ok) {
101+
return false;
102+
}
103+
}
104+
return 'Wrong type';
61105
}
62106

63107
public static function getPriority()

src/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,9 @@ $structure->import('http://sfsdf.sdfsd/'); // OK
6262
Use `\stdClass` instead of `array` for `JSON` objects,
6363
cast to `array` on iteration
6464

65+
## Road map
66+
* Exceptions-less mode
67+
68+
6569

6670
https://github.com/epoberezkin/ajv

src/Schema.php

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,23 +118,33 @@ public function import($data, $deepValidation = false)
118118
if (self::$debug) {
119119
print_r($data);
120120
}
121-
$result = $data;
121+
$entity = $data;
122122
foreach ($this->constraints as $constraint) {
123-
$failReason = $constraint->importFailed($data, $result);
123+
$failReason = $constraint->importFailed($data, $entity);
124124
if ($failReason && !$deepValidation) {
125125
throw new Exception($failReason, Exception::INVALID_VALUE);
126126
}
127127
if (self::$debug) {
128128
var_dump(get_class($constraint), $data);
129129
}
130130
}
131-
return $result;
131+
return $entity;
132132
}
133133

134-
public function export($data, $deepValidation = false)
134+
public function export($entity, $deepValidation = false)
135135
{
136+
if (self::$debug) {
137+
print_r($entity);
138+
}
139+
$data = $entity;
136140
foreach ($this->constraints as $constraint) {
137-
$data = $constraint->export($data);
141+
$failReason = $constraint->exportFailed($entity, $data);
142+
if ($failReason && !$deepValidation) {
143+
throw new Exception($failReason, Exception::INVALID_VALUE);
144+
}
145+
if (self::$debug) {
146+
var_dump(get_class($constraint), $entity);
147+
}
138148
}
139149
return $data;
140150
}

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)

0 commit comments

Comments
 (0)