Skip to content

Commit 9db24f2

Browse files
committed
schema dump, scalar schema type, cleanup
1 parent 00c00f4 commit 9db24f2

File tree

8 files changed

+157
-21
lines changed

8 files changed

+157
-21
lines changed

src/Constraint/Properties.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
/**
1212
* @method Schema __get($key)
13+
* @method Schema[] toArray()
1314
*/
1415
class Properties extends MagicMap implements Constraint
1516
{

src/Constraint/Type.php

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,30 @@
22

33
namespace Swaggest\JsonSchema\Constraint;
44

5-
6-
use Swaggest\JsonSchema\Schema;
7-
85
class Type implements Constraint
96
{
107
const OBJECT = 'object';
118
const STRING = 'string';
129
const INTEGER = 'integer';
1310
const NUMBER = 'number';
14-
const _ARRAY = 'array';
11+
const ARR = 'array';
1512
const BOOLEAN = 'boolean';
1613
const NULL = 'null';
1714

1815
public $types;
1916

20-
public function __construct($type)
21-
{
22-
$this->types = is_array($type) ? $type : array($type);
23-
}
24-
25-
public function isValid($data)
17+
public static function isValid($types, $data)
2618
{
19+
if (!is_array($types)) {
20+
$types = array($types);
21+
}
2722
$ok = false;
28-
foreach ($this->types as $type) {
23+
foreach ($types as $type) {
2924
switch ($type) {
3025
case self::OBJECT:
3126
$ok = $data instanceof \stdClass;
3227
break;
33-
case self::_ARRAY:
28+
case self::ARR:
3429
$ok = is_array($data);
3530
break;
3631
case self::STRING:

src/Schema.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -476,38 +476,53 @@ private function fail(InvalidValue $exception, $path)
476476
public static function integer()
477477
{
478478
$schema = new Schema();
479-
$schema->type = new Type(Type::INTEGER);
479+
$schema->type = Type::INTEGER;
480480
return $schema;
481481
}
482482

483483
public static function number()
484484
{
485485
$schema = new Schema();
486-
$schema->type = new Type(Type::NUMBER);
486+
$schema->type = Type::NUMBER;
487487
return $schema;
488488
}
489489

490490
public static function string()
491491
{
492492
$schema = new Schema();
493-
$schema->type = new Type(Type::STRING);
493+
$schema->type = Type::STRING;
494494
return $schema;
495495
}
496496

497497
public static function boolean()
498498
{
499499
$schema = new Schema();
500-
$schema->type = new Type(Type::BOOLEAN);
500+
$schema->type = Type::BOOLEAN;
501501
return $schema;
502502
}
503503

504504
public static function object()
505505
{
506506
$schema = new Schema();
507-
$schema->type = new Type(Type::OBJECT);
507+
$schema->type = Type::OBJECT;
508508
return $schema;
509509
}
510510

511+
public static function arr()
512+
{
513+
$schema = new Schema();
514+
$schema->type = Type::ARR;
515+
return $schema;
516+
}
517+
518+
public static function null()
519+
{
520+
$schema = new Schema();
521+
$schema->type = Type::NULL;
522+
return $schema;
523+
}
524+
525+
511526
public static function create()
512527
{
513528
$schema = new Schema();

src/SchemaLoader.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,61 @@ public function readSchema($schemaData)
8080
return $this->readSchemaDeeper($schemaData);
8181
}
8282

83+
/** @var \SplObjectStorage */
84+
private $circularReferences;
85+
public function dumpSchema(Schema $schema)
86+
{
87+
$this->circularReferences = new \SplObjectStorage();
88+
$this->dumpDefinitions = array();
89+
$this->dumpDefIndex = 0;
90+
$contents = $this->dumpSchemaDeeper($schema, '#');
91+
return $contents;
92+
}
93+
94+
public function dumpSchemaDeeper(Schema $schema, $path)
95+
{
96+
$result = new \stdClass();
97+
98+
if ($this->circularReferences->contains($schema)) {
99+
$path = $this->circularReferences[$schema];
100+
$result->{self::REF} = $path;
101+
return $result;
102+
}
103+
$this->circularReferences->attach($schema, $path);
104+
105+
$data = get_object_vars($schema);
106+
foreach ($data as $key => $value) {
107+
if ($value === null) {
108+
continue;
109+
}
110+
111+
if ($value instanceof Schema) {
112+
$value = $this->dumpSchemaDeeper($value, $path . '/' . $key);
113+
}
114+
115+
if (is_array($value)) {
116+
foreach ($value as $k => $v) {
117+
if ($v instanceof Schema) {
118+
$value[$k] = $this->dumpSchemaDeeper($v, $path . '/' . $key . '/' . $k);
119+
}
120+
}
121+
}
122+
123+
if ($key === self::PROPERTIES) {
124+
/** @var Properties $properties */
125+
$properties = $value;
126+
$value = array();
127+
foreach ($properties->toArray() as $propertyName => $property) {
128+
$value[$propertyName] = $this->dumpSchemaDeeper($property, $path . '/' . $key . '/' . $propertyName);
129+
}
130+
}
131+
132+
133+
$result->$key = $value;
134+
}
135+
return $result;
136+
}
137+
83138
private $resolutionScope;
84139

85140
protected function readSchemaDeeper($schemaArray)
@@ -104,7 +159,7 @@ protected function readSchemaDeeper($schemaArray)
104159
}
105160

106161
if (isset($schemaArray[self::TYPE])) {
107-
$schema->type = new Type($schemaArray[self::TYPE]);
162+
$schema->type = $schemaArray[self::TYPE];
108163
}
109164

110165

src/Structure/ClassStructure.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public static function schema()
2020

2121
if (null === $schema) {
2222
$schema = new ClassSchema();
23-
$schema->type = new Type(Type::OBJECT);
23+
$schema->type = Type::OBJECT;
2424
$properties = new Properties();
2525
$schema->properties = $properties;
2626
$schema->objectItemClass = get_called_class();
@@ -128,4 +128,8 @@ public function __set($name, $column) // todo nested schemas
128128
$this->__arrayOfData[$name] = $column;
129129
return $this;
130130
}
131+
132+
public static function className() {
133+
return get_called_class();
134+
}
131135
}

src/Structure/Composition.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class Composition extends Schema
1616
*/
1717
public function __construct()
1818
{
19-
$this->type = new Type(Type::OBJECT);
19+
$this->type = Type::OBJECT;
2020
$properties = new Properties();
2121
$this->properties = $properties;
2222

tests/src/Helper/LevelThreeClass.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Swaggest\JsonSchema\Schema;
77
use Swaggest\JsonSchema\Constraint\Properties;
88
use Swaggest\JsonSchema\Structure\ClassStructure;
9-
use Swaggest\JsonSchema\Types\IntegerType;
109

1110
class LevelThreeClass extends ClassStructure
1211
{
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\DumpSchema;
4+
5+
6+
use Swaggest\JsonSchema\Schema;
7+
use Swaggest\JsonSchema\SchemaLoader;
8+
9+
class DumpTest extends \PHPUnit_Framework_TestCase
10+
{
11+
public function testDump()
12+
{
13+
$anotherSchema = Schema::object()
14+
->setProperty('hello', Schema::boolean())
15+
->setProperty('world', Schema::string());
16+
17+
18+
$schema = Schema::object()
19+
->setProperty('sampleInt', Schema::integer())
20+
->setProperty('sampleBool', Schema::boolean())
21+
->setProperty('sampleString', Schema::string())
22+
->setProperty('sampleNumber', Schema::number());
23+
$schema
24+
->setProperty('sampleSelf', $schema)
25+
->setProperty('another', $anotherSchema);
26+
27+
$loader = SchemaLoader::create();
28+
29+
$schemaData = $loader->dumpSchema($schema);
30+
$expected = <<<'JSON'
31+
{
32+
"type": "object",
33+
"properties": {
34+
"sampleInt": {
35+
"type": "integer"
36+
},
37+
"sampleBool": {
38+
"type": "boolean"
39+
},
40+
"sampleString": {
41+
"type": "string"
42+
},
43+
"sampleNumber": {
44+
"type": "number"
45+
},
46+
"sampleSelf": {
47+
"$ref": "#"
48+
},
49+
"another": {
50+
"type": "object",
51+
"properties": {
52+
"hello": {
53+
"type": "boolean"
54+
},
55+
"world": {
56+
"type": "string"
57+
}
58+
}
59+
}
60+
}
61+
}
62+
JSON;
63+
64+
$this->assertSame($expected, json_encode($schemaData, JSON_PRETTY_PRINT));
65+
}
66+
67+
}

0 commit comments

Comments
 (0)