Skip to content

Commit bc39e9c

Browse files
committed
type object
1 parent e90cd3e commit bc39e9c

File tree

4 files changed

+97
-11
lines changed

4 files changed

+97
-11
lines changed

src/Constraint/Type.php

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public static function getSchemaKey()
2222
}
2323

2424
private $types;
25+
2526
public function __construct($schemaValue, Schema $ownerSchema = null)
2627
{
2728
$this->ownerSchema = $ownerSchema;
@@ -34,19 +35,29 @@ public function importFailed($data, &$entity)
3435
foreach ($this->types as $type) {
3536
switch ($type) {
3637
case self::OBJECT:
37-
$ok = is_object($data) || is_array($data);break;
38+
$ok = is_object($data) || is_array($data);
39+
if ($ok && !is_object($entity)) {
40+
$entity = (object)$entity;
41+
}
42+
break;
3843
case self::_ARRAY:
39-
$ok = is_array($data);break;
44+
$ok = is_array($data);
45+
break;
4046
case self::STRING:
41-
$ok = is_string($data);break;
47+
$ok = is_string($data);
48+
break;
4249
case self::INTEGER:
43-
$ok = is_int($data);break;
50+
$ok = is_int($data);
51+
break;
4452
case self::NUMBER:
45-
$ok = is_int($data) || is_float($data);break;
53+
$ok = is_int($data) || is_float($data);
54+
break;
4655
case self::BOOLEAN:
47-
$ok = is_bool($data);break;
56+
$ok = is_bool($data);
57+
break;
4858
case self::NULL:
49-
$ok = null === $data;break;
59+
$ok = null === $data;
60+
break;
5061
}
5162
if ($ok) {
5263
return false;
@@ -57,7 +68,39 @@ public function importFailed($data, &$entity)
5768

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

63106
public static function getPriority()

src/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,8 @@ $structure->import('http://sfsdf.sdfsd/'); // OK
6060
## Solid detection
6161

6262
Use `\stdClass` instead of `array` for `JSON` objects,
63-
cast to `array` on iteration
63+
cast to `array` on iteration
64+
65+
## Road map
66+
* Exceptions-less mode
67+

src/Schema.php

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,10 +135,20 @@ public function import($data, $deepValidation = false)
135135

136136
public function export($data, $deepValidation = false)
137137
{
138+
if (self::$debug) {
139+
print_r($data);
140+
}
141+
$result = $data;
138142
foreach ($this->constraints as $constraint) {
139-
$data = $constraint->export($data);
143+
$failReason = $constraint->exportFailed($data, $result);
144+
if ($failReason && !$deepValidation) {
145+
throw new Exception($failReason, Exception::INVALID_VALUE);
146+
}
147+
if (self::$debug) {
148+
var_dump(get_class($constraint), $data);
149+
}
140150
}
141-
return $data;
151+
return $result;
142152
}
143153

144154

tests/src/Naive/TypeObjectTest.php

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

0 commit comments

Comments
 (0)