Skip to content

Commit e90cd3e

Browse files
committed
i dont know
1 parent 574b78d commit e90cd3e

File tree

6 files changed

+119
-76
lines changed

6 files changed

+119
-76
lines changed

src/Constraint.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
interface Constraint
66
{
7+
const P0 = 0;
8+
const P1 = 1;
9+
10+
711
public function __construct($schemaValue, Schema $ownerSchema = null);
812

913
public static function getSchemaKey();
@@ -17,11 +21,23 @@ public function setOwnerSchema(Schema $ownerSchema);
1721
* Some constraints may alter result, e.g. `Properties` would create object and fill properties values.
1822
*
1923
* @param $data
20-
* @param $result
24+
* @param $entity
2125
* @return string invalidation reason, false if valid
2226
*/
23-
public function importFailed($data, &$result);
27+
public function importFailed($data, &$entity);
28+
29+
/**
30+
*
31+
* @param $data
32+
* @param $entity
33+
* @return mixed
34+
*/
35+
public function exportFailed($data, &$entity);
36+
2437

25-
public function exportFailed($data);
38+
/**
39+
* Constraints have priority, `Properties` first, `AdditionalProperties` after, etc...
40+
*/
41+
public static function getPriority();
2642

2743
}

src/Constraint/Properties.php

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,33 @@ public function hasProperty($name)
8585
return isset($this->properties[$name]);
8686
}
8787

88+
/**
89+
* @param $data
90+
* @param $result
91+
* @return bool
92+
*/
93+
public function importFailed($data, &$result)
94+
{
95+
$result = new \stdClass();
96+
97+
foreach ($this->properties as $name => $property) {
98+
99+
100+
}
101+
102+
return false;
103+
}
104+
105+
public function exportFailed($data, &$entity)
106+
{
107+
// TODO: Implement exportFailed() method.
108+
}
109+
110+
111+
public static function getPriority()
112+
{
113+
return self::P1;
114+
}
115+
116+
88117
}

src/Constraint/Type.php

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

55
use Yaoi\Schema\AbstractConstraint;
6-
use Yaoi\Schema\Exception;
76
use Yaoi\Schema\Schema;
8-
use Yaoi\Schema\Types\AbstractType;
9-
use Yaoi\Schema\Types\ArrayType;
10-
use Yaoi\Schema\Types\BooleanType;
11-
use Yaoi\Schema\Types\IntegerType;
12-
use Yaoi\Schema\Types\NumberType;
13-
use Yaoi\Schema\Types\ObjectType;
14-
use Yaoi\Schema\Types\StringType;
157

168
class Type extends AbstractConstraint
179
{
10+
const OBJECT = 'object';
11+
const STRING = 'string';
12+
const INTEGER = 'integer';
13+
const NUMBER = 'number';
14+
const _ARRAY = 'array';
15+
const BOOLEAN = 'boolean';
16+
const NULL = 'null';
17+
18+
1819
public static function getSchemaKey()
1920
{
2021
return 'type';
2122
}
2223

24+
private $types;
2325
public function __construct($schemaValue, Schema $ownerSchema = null)
2426
{
2527
$this->ownerSchema = $ownerSchema;
26-
if (!is_array($schemaValue)) {
27-
$schemaValue = array($schemaValue);
28-
}
29-
foreach ($schemaValue as $type) {
30-
$this->typeHandlers[$type] = self::factory($type, $ownerSchema);
31-
}
28+
$this->types = is_array($schemaValue) ? $schemaValue : array($schemaValue);
3229
}
3330

34-
/**
35-
* @var AbstractType[]
36-
*/
37-
private $typeHandlers = array();
38-
39-
public function import($data)
31+
public function importFailed($data, &$entity)
4032
{
41-
$lastException = null;
42-
foreach ($this->typeHandlers as $typeHandler) {
43-
try {
44-
return $typeHandler->import($data);
33+
$ok = false;
34+
foreach ($this->types as $type) {
35+
switch ($type) {
36+
case self::OBJECT:
37+
$ok = is_object($data) || is_array($data);break;
38+
case self::_ARRAY:
39+
$ok = is_array($data);break;
40+
case self::STRING:
41+
$ok = is_string($data);break;
42+
case self::INTEGER:
43+
$ok = is_int($data);break;
44+
case self::NUMBER:
45+
$ok = is_int($data) || is_float($data);break;
46+
case self::BOOLEAN:
47+
$ok = is_bool($data);break;
48+
case self::NULL:
49+
$ok = null === $data;break;
4550
}
46-
catch (Exception $exception) {
47-
$lastException = $exception;
51+
if ($ok) {
52+
return false;
4853
}
4954
}
50-
throw $lastException;
55+
return 'Wrong type';
5156
}
5257

53-
public function export($data)
58+
public function exportFailed($data, &$entity)
5459
{
55-
$lastException = null;
56-
foreach ($this->typeHandlers as $typeHandler) {
57-
try {
58-
return $typeHandler->export($data);
59-
} catch (Exception $exception) {
60-
$lastException = $exception;
61-
}
62-
}
63-
throw $lastException;
60+
return $this->importFailed($data, $entity);
6461
}
6562

66-
67-
private static function factory($schemaValue, Schema $parentSchema = null)
63+
public static function getPriority()
6864
{
69-
if (is_array($schemaValue)) {
70-
throw new Exception("Please implement me", Exception::NOT_IMPLEMENTED);
71-
}
72-
73-
switch ($schemaValue) {
74-
case ObjectType::TYPE:
75-
return new ObjectType($parentSchema);
76-
case StringType::TYPE:
77-
return new StringType($parentSchema);
78-
case IntegerType::TYPE:
79-
return new IntegerType($parentSchema);
80-
case NumberType::TYPE:
81-
return new NumberType($parentSchema);
82-
case BooleanType::TYPE:
83-
return new BooleanType($parentSchema);
84-
case ArrayType::TYPE:
85-
return new ArrayType($parentSchema);
86-
default:
87-
throw new Exception('Unknown type ' . $schemaValue);
88-
}
65+
return self::P0;
8966
}
9067

91-
public function getHandlerByType($type) {
92-
if (isset($this->typeHandlers[$type])) {
93-
return $this->typeHandlers[$type];
94-
}
95-
return null;
96-
}
97-
98-
9968

10069
}

src/Schema.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,28 +108,32 @@ public function __construct($schemaValue = null, Schema $parentSchema = null)
108108

109109
public function setConstraint(Constraint $constraint)
110110
{
111-
$this->constraints[get_class($constraint)] = $constraint;
111+
$this->constraints[$constraint::getSchemaKey()] = $constraint;
112112
$constraint->setOwnerSchema($this);
113113
return $this;
114114
}
115115

116116
static public $debug = false;
117117

118-
public function import($data)
118+
public function import($data, $deepValidation = false)
119119
{
120120
if (self::$debug) {
121121
print_r($data);
122122
}
123+
$result = $data;
123124
foreach ($this->constraints as $constraint) {
124-
$data = $constraint->import($data);
125+
$failReason = $constraint->importFailed($data, $result);
126+
if ($failReason && !$deepValidation) {
127+
throw new Exception($failReason, Exception::INVALID_VALUE);
128+
}
125129
if (self::$debug) {
126130
var_dump(get_class($constraint), $data);
127131
}
128132
}
129-
return $data;
133+
return $result;
130134
}
131135

132-
public function export($data)
136+
public function export($data, $deepValidation = false)
133137
{
134138
foreach ($this->constraints as $constraint) {
135139
$data = $constraint->export($data);
@@ -163,6 +167,7 @@ public static function initConstraintKeys()
163167
self::$constraintKeys = array(
164168
Type::getSchemaKey() => Type::className(),
165169
Properties::getSchemaKey() => Properties::className(),
170+
/*
166171
AdditionalProperties::getSchemaKey() => AdditionalProperties::className(),
167172
Items::getSchemaKey() => Items::className(),
168173
Ref::getSchemaKey() => Ref::className(),
@@ -172,6 +177,7 @@ public static function initConstraintKeys()
172177
Minimum::getSchemaKey() => Minimum::className(),
173178
MinLength::getSchemaKey() => MinLength::className(),
174179
MaxLength::getSchemaKey() => MaxLength::className(),
180+
*/
175181
);
176182

177183
}

tests/src/AdditionalPropertiesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public function testBasic()
2121
//print_r($schema);
2222

2323
$object = $schema->import(
24-
array('one' => 1, 'two' => 2)
24+
(object)array('one' => 1, 'two' => 2)
2525
);
2626

2727
$this->assertSame(1, $object->one);

tests/src/Naive/TypeStringTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Yaoi\Schema\Tests\Naive;
4+
5+
6+
use Yaoi\Schema\Exception;
7+
use Yaoi\Schema\Schema;
8+
9+
class TypeStringTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testValid()
12+
{
13+
$schema = new Schema(array('type' => 'string'));
14+
$this->assertSame('123', $schema->import('123'));
15+
}
16+
17+
public function testInvalidInteger()
18+
{
19+
$this->setExpectedException(get_class(new Exception), 'Wrong type', Exception::INVALID_VALUE);
20+
$schema = new Schema(array('type' => 'string'));
21+
$this->assertSame(123, $schema->import(123));
22+
}
23+
}

0 commit comments

Comments
 (0)