Skip to content

Commit 4993718

Browse files
committed
spec compliance
Tests: 275, Assertions: 259, Errors: 16, Failures: 1.
1 parent 71b48af commit 4993718

File tree

2 files changed

+92
-9
lines changed

2 files changed

+92
-9
lines changed

src/Schema.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,16 @@ class Schema extends MagicMap
7474
/** @var int */
7575
public $maxLength;
7676

77+
78+
/** @var Schema[] */
79+
public $allOf;
80+
/** @var Schema */
81+
public $not;
82+
/** @var Schema[] */
83+
public $anyOf;
84+
/** @var Schema[] */
85+
public $oneOf;
86+
7787
public function import($data)
7888
{
7989
$result = $data;
@@ -100,6 +110,58 @@ public function import($data)
100110
}
101111
}
102112

113+
if ($this->not !== null) {
114+
$exception = false;
115+
try {
116+
$this->not->import($data);
117+
} catch (Exception $exception) {
118+
}
119+
if ($exception === false) {
120+
$this->fail('Failed due to logical constraint: not');
121+
}
122+
}
123+
124+
if ($this->oneOf !== null) {
125+
$successes = 0;
126+
foreach ($this->oneOf as $item) {
127+
try {
128+
$result = $item->import($data);
129+
$successes++;
130+
if ($successes > 1) {
131+
break;
132+
}
133+
} catch (Exception $exception) {
134+
}
135+
}
136+
if ($successes !== 1) {
137+
$this->fail('Failed due to logical constraint: oneOf');
138+
}
139+
}
140+
141+
if ($this->anyOf !== null) {
142+
$successes = 0;
143+
foreach ($this->anyOf as $item) {
144+
try {
145+
$result = $item->import($data);
146+
$successes++;
147+
if ($successes) {
148+
break;
149+
}
150+
} catch (Exception $exception) {
151+
}
152+
}
153+
if (!$successes) {
154+
$this->fail('Failed due to logical constraint: anyOf');
155+
}
156+
}
157+
158+
if ($this->allOf !== null) {
159+
foreach ($this->allOf as $item) {
160+
$result = $item->import($data);
161+
}
162+
}
163+
164+
103165
if (is_string($data)) {
104166
if ($this->minLength !== null) {
105167
if (mb_strlen($data) < $this->minLength) {

src/SchemaLoader.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ class SchemaLoader extends Base
3838
const MIN_LENGTH = 'minLength';
3939
const MAX_LENGTH = 'maxLength';
4040

41+
const NOT = 'not';
42+
const ALL_OF = 'allOf';
43+
const ANY_OF = 'anyOf';
44+
const ONE_OF = 'oneOf';
45+
4146
/** @var Schema */
4247
private $rootSchema;
4348

@@ -53,7 +58,6 @@ public function readSchema($schemaData)
5358
}
5459

5560

56-
5761
protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
5862
{
5963
$schema = new Schema();
@@ -153,7 +157,6 @@ protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
153157
}
154158

155159

156-
157160
// Number
158161
if (isset($schemaArray[self::MINIMUM])) {
159162
$schema->minimum = $schemaArray[self::MINIMUM];
@@ -189,6 +192,26 @@ protected function readSchemaDeeper($schemaArray, Schema $parentSchema = null)
189192
$schema->enum = $schemaArray[self::ENUM];
190193
}
191194

195+
// Logic
196+
if (isset($schemaArray[self::ALL_OF])) {
197+
foreach ($schemaArray[self::ALL_OF] as $item) {
198+
$schema->allOf[] = $this->readSchemaDeeper($item);
199+
}
200+
}
201+
if (isset($schemaArray[self::ANY_OF])) {
202+
foreach ($schemaArray[self::ANY_OF] as $item) {
203+
$schema->anyOf[] = $this->readSchemaDeeper($item);
204+
}
205+
}
206+
if (isset($schemaArray[self::ONE_OF])) {
207+
foreach ($schemaArray[self::ONE_OF] as $item) {
208+
$schema->oneOf[] = $this->readSchemaDeeper($item);
209+
}
210+
}
211+
if (isset($schemaArray[self::NOT])) {
212+
$schema->not = $this->readSchemaDeeper($schemaArray[self::NOT]);
213+
}
214+
192215
// should resolve references on load
193216
if (isset($schemaArray[self::REF])) {
194217
$schema->ref = $this->resolveReference($schemaArray[self::REF]);
@@ -212,13 +235,9 @@ private function resolveReference($referencePath)
212235
$referencePath,
213236
SchemaLoader::create()->readSchema(json_decode(file_get_contents(__DIR__ . '/../spec/json-schema.json')))
214237
);
215-
}
216-
217-
elseif ($referencePath === '#') {
238+
} elseif ($referencePath === '#') {
218239
$ref = new Ref($referencePath, $this->rootSchema);
219-
}
220-
221-
elseif ($referencePath[0] === '#') {
240+
} elseif ($referencePath[0] === '#') {
222241
$path = explode('/', trim($referencePath, '#/'));
223242
$branch = &$this->rootData;
224243
while ($path) {
@@ -249,4 +268,6 @@ public function writeSchema()
249268
/**
250269
* @property $minimum
251270
*/
252-
class __stubJsonSchema {}
271+
class __stubJsonSchema
272+
{
273+
}

0 commit comments

Comments
 (0)