Skip to content

Commit 18f803d

Browse files
committed
bettr buildr
1 parent 4f031d4 commit 18f803d

18 files changed

+1688
-139
lines changed

spec/petstore-swagger.json

Lines changed: 1035 additions & 0 deletions
Large diffs are not rendered by default.

src/Constraint/Properties.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +3,34 @@
33
namespace Swaggest\JsonSchema\Constraint;
44

55
use Swaggest\JsonSchema\Exception;
6-
use Swaggest\JsonSchema\MagicMap;
76
use Swaggest\JsonSchema\Schema;
87
use Swaggest\JsonSchema\Structure\Egg;
98
use Swaggest\JsonSchema\Structure\Nested;
9+
use Swaggest\JsonSchema\Structure\ObjectItem;
1010

1111
/**
1212
* @method Schema __get($key)
1313
* @method Schema[] toArray()
1414
*/
15-
class Properties extends MagicMap implements Constraint
15+
class Properties extends ObjectItem implements Constraint
1616
{
1717
/** @var Schema[] */
1818
protected $__arrayOfData = array();
1919

20+
/** @var Schema */
21+
protected $__schema;
22+
23+
public function setSchema(Schema $schema)
24+
{
25+
$this->__schema = $schema;
26+
return $this;
27+
}
28+
29+
public function getSchema()
30+
{
31+
return $this->__schema;
32+
}
33+
2034
public function __set($name, $column)
2135
{
2236
if ($column instanceof Nested) {

src/Constraint/Ref.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,36 @@ public function __construct($ref, $data = null)
1414
/** @var mixed */
1515
private $data;
1616

17+
/** @var mixed */
18+
private $imported;
19+
/** @var boolean */
20+
private $isImported = false;
21+
22+
/**
23+
* @return mixed
24+
*/
25+
public function getImported()
26+
{
27+
return $this->imported;
28+
}
29+
30+
/**
31+
* @param mixed $imported
32+
*/
33+
public function setImported($imported)
34+
{
35+
$this->isImported = true;
36+
$this->imported = $imported;
37+
}
38+
39+
/**
40+
* @return boolean
41+
*/
42+
public function isImported()
43+
{
44+
return $this->isImported;
45+
}
46+
1747
/**
1848
* @param mixed $data
1949
* @return Ref

src/JsonSchema.php

Lines changed: 41 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
namespace Swaggest\JsonSchema;
44

5-
use Swaggest\JsonSchema\Structure\ClassStructure;
5+
use Swaggest\JsonSchema\Constraint\Properties;
6+
use Swaggest\JsonSchema\Structure\SchemaStructure;
67

7-
class JsonSchema extends ClassStructure {
8+
9+
class JsonSchema extends SchemaStructure {
810
/** @var string */
911
public $id;
1012

@@ -73,7 +75,7 @@ class JsonSchema extends ClassStructure {
7375
/** @var JsonSchema[] */
7476
public $definitions;
7577

76-
/** @var JsonSchema[] */
78+
/** @var JsonSchema|JsonSchema[] */
7779
public $properties;
7880

7981
/** @var JsonSchema[] */
@@ -112,68 +114,69 @@ class JsonSchema extends ClassStructure {
112114
*/
113115
public static function setUpProperties($properties, Schema $ownerSchema)
114116
{
115-
$properties->id = Schema::string();
116-
$properties->schema = Schema::string();
117+
$properties->id = JsonSchema::string();
118+
$properties->schema = JsonSchema::string();
117119
$ownerSchema->addPropertyMapping('$schema', self::names()->schema);
118-
$properties->title = Schema::string();
119-
$properties->description = Schema::string();
120+
$properties->title = JsonSchema::string();
121+
$properties->description = JsonSchema::string();
120122
$properties->default = new Schema();
121-
$properties->multipleOf = Schema::number();
123+
$properties->multipleOf = JsonSchema::number();
122124
$properties->multipleOf->minimum = 0;
123125
$properties->multipleOf->exclusiveMinimum = true;
124-
$properties->maximum = Schema::number();
125-
$properties->exclusiveMaximum = Schema::boolean();
126-
$properties->minimum = Schema::number();
127-
$properties->exclusiveMinimum = Schema::boolean();
128-
$properties->maxLength = Schema::integer();
126+
$properties->maximum = JsonSchema::number();
127+
$properties->exclusiveMaximum = JsonSchema::boolean();
128+
$properties->minimum = JsonSchema::number();
129+
$properties->exclusiveMinimum = JsonSchema::boolean();
130+
$properties->maxLength = JsonSchema::integer();
129131
$properties->maxLength->minimum = 0;
130132
$properties->minLength = new Schema();
131-
$properties->minLength->allOf[0] = Schema::integer();
133+
$properties->minLength->allOf[0] = JsonSchema::integer();
132134
$properties->minLength->allOf[0]->minimum = 0;
133135
$properties->minLength->allOf[1] = new Schema();
134-
$properties->pattern = Schema::string();
136+
$properties->pattern = JsonSchema::string();
135137
$properties->additionalItems = new Schema();
136-
$properties->additionalItems->anyOf[0] = Schema::boolean();
138+
$properties->additionalItems->anyOf[0] = JsonSchema::boolean();
137139
$properties->additionalItems->anyOf[1] = JsonSchema::schema();
138140
$properties->items = new Schema();
139141
$properties->items->anyOf[0] = JsonSchema::schema();
140-
$properties->items->anyOf[1] = Schema::arr();
142+
$properties->items->anyOf[1] = JsonSchema::arr();
141143
$properties->items->anyOf[1]->items = JsonSchema::schema();
142144
$properties->items->anyOf[1]->minItems = 1;
143-
$properties->maxItems = Schema::integer();
145+
$properties->maxItems = JsonSchema::integer();
144146
$properties->maxItems->minimum = 0;
145147
$properties->minItems = new Schema();
146-
$properties->minItems->allOf[0] = Schema::integer();
148+
$properties->minItems->allOf[0] = JsonSchema::integer();
147149
$properties->minItems->allOf[0]->minimum = 0;
148150
$properties->minItems->allOf[1] = new Schema();
149-
$properties->uniqueItems = Schema::boolean();
150-
$properties->maxProperties = Schema::integer();
151+
$properties->uniqueItems = JsonSchema::boolean();
152+
$properties->maxProperties = JsonSchema::integer();
151153
$properties->maxProperties->minimum = 0;
152154
$properties->minProperties = new Schema();
153-
$properties->minProperties->allOf[0] = Schema::integer();
155+
$properties->minProperties->allOf[0] = JsonSchema::integer();
154156
$properties->minProperties->allOf[0]->minimum = 0;
155157
$properties->minProperties->allOf[1] = new Schema();
156-
$properties->required = Schema::arr();
157-
$properties->required->items = Schema::string();
158+
$properties->required = JsonSchema::arr();
159+
$properties->required->items = JsonSchema::string();
158160
$properties->required->uniqueItems = true;
159161
$properties->required->minItems = 1;
160162
$properties->additionalProperties = new Schema();
161-
$properties->additionalProperties->anyOf[0] = Schema::boolean();
163+
$properties->additionalProperties->anyOf[0] = JsonSchema::boolean();
162164
$properties->additionalProperties->anyOf[1] = JsonSchema::schema();
163-
$properties->definitions = Schema::object();
165+
$properties->definitions = JsonSchema::object();
164166
$properties->definitions->additionalProperties = JsonSchema::schema();
165-
$properties->properties = Schema::object();
167+
$properties->properties = JsonSchema::object();
166168
$properties->properties->additionalProperties = JsonSchema::schema();
167-
$properties->patternProperties = Schema::object();
169+
$properties->patternProperties = JsonSchema::object();
168170
$properties->patternProperties->additionalProperties = JsonSchema::schema();
169-
$properties->dependencies = Schema::object();
171+
$properties->dependencies = JsonSchema::object();
172+
//$properties->dependencies->additionalProperties = JsonSchema::schema();
170173
$properties->dependencies->additionalProperties = new Schema();
171174
$properties->dependencies->additionalProperties->anyOf[0] = JsonSchema::schema();
172-
$properties->dependencies->additionalProperties->anyOf[1] = Schema::arr();
173-
$properties->dependencies->additionalProperties->anyOf[1]->items = Schema::string();
175+
$properties->dependencies->additionalProperties->anyOf[1] = JsonSchema::arr();
176+
$properties->dependencies->additionalProperties->anyOf[1]->items = JsonSchema::string();
174177
$properties->dependencies->additionalProperties->anyOf[1]->uniqueItems = true;
175178
$properties->dependencies->additionalProperties->anyOf[1]->minItems = 1;
176-
$properties->enum = Schema::arr();
179+
$properties->enum = JsonSchema::arr();
177180
$properties->enum->uniqueItems = true;
178181
$properties->enum->minItems = 1;
179182
$properties->type = new Schema();
@@ -187,7 +190,7 @@ public static function setUpProperties($properties, Schema $ownerSchema)
187190
5 => 'object',
188191
6 => 'string',
189192
);
190-
$properties->type->anyOf[1] = Schema::arr();
193+
$properties->type->anyOf[1] = JsonSchema::arr();
191194
$properties->type->anyOf[1]->items = new Schema();
192195
$properties->type->anyOf[1]->items->enum = array (
193196
0 => 'array',
@@ -200,16 +203,16 @@ public static function setUpProperties($properties, Schema $ownerSchema)
200203
);
201204
$properties->type->anyOf[1]->uniqueItems = true;
202205
$properties->type->anyOf[1]->minItems = 1;
203-
$properties->format = Schema::string();
204-
$properties->ref = Schema::string();
206+
$properties->format = JsonSchema::string();
207+
$properties->ref = JsonSchema::string();
205208
$ownerSchema->addPropertyMapping('$ref', self::names()->ref);
206-
$properties->allOf = Schema::arr();
209+
$properties->allOf = JsonSchema::arr();
207210
$properties->allOf->items = JsonSchema::schema();
208211
$properties->allOf->minItems = 1;
209-
$properties->anyOf = Schema::arr();
212+
$properties->anyOf = JsonSchema::arr();
210213
$properties->anyOf->items = JsonSchema::schema();
211214
$properties->anyOf->minItems = 1;
212-
$properties->oneOf = Schema::arr();
215+
$properties->oneOf = JsonSchema::arr();
213216
$properties->oneOf->items = JsonSchema::schema();
214217
$properties->oneOf->minItems = 1;
215218
$properties->not = JsonSchema::schema();

src/ProcessingOptions.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema;
4+
5+
class ProcessingOptions extends MagicMap
6+
{
7+
protected $import = true;
8+
/** @var DataPreProcessor */
9+
public $dataPreProcessor;
10+
/** @var RefResolver */
11+
protected $refResolver;
12+
13+
/** @var RemoteRefProvider */
14+
public $remoteRefProvider;
15+
16+
/** @var string */
17+
public $propagateObjectItemClass;
18+
19+
/**
20+
* @return DataPreProcessor
21+
*/
22+
public function getDataPreProcessor()
23+
{
24+
return $this->dataPreProcessor;
25+
}
26+
27+
/**
28+
* @param DataPreProcessor $dataPreProcessor
29+
* @return ProcessingOptions
30+
*/
31+
public function setDataPreProcessor($dataPreProcessor)
32+
{
33+
$this->dataPreProcessor = $dataPreProcessor;
34+
return $this;
35+
}
36+
37+
/**
38+
* @return RemoteRefProvider
39+
*/
40+
public function getRemoteRefProvider()
41+
{
42+
return $this->remoteRefProvider;
43+
}
44+
45+
/**
46+
* @param RemoteRefProvider $remoteRefProvider
47+
* @return ProcessingOptions
48+
*/
49+
public function setRemoteRefProvider($remoteRefProvider)
50+
{
51+
$this->remoteRefProvider = $remoteRefProvider;
52+
return $this;
53+
}
54+
55+
56+
}

src/RefResolver.php

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,28 @@
22

33
namespace Swaggest\JsonSchema;
44

5-
use PhpLang\ScopeExit;
65
use Swaggest\JsonSchema\Constraint\Ref;
76
use Swaggest\JsonSchema\RemoteRef\BasicFetcher;
87

98
class RefResolver
109
{
11-
private $resolutionScope;
10+
private $resolutionScope = '';
11+
private $url;
12+
13+
/**
14+
* @param mixed $resolutionScope
15+
*/
16+
public function setResolutionScope($resolutionScope)
17+
{
18+
$this->resolutionScope = $resolutionScope;
19+
}
20+
21+
public function updateResolutionScope($id)
22+
{
23+
$prev = $this->resolutionScope;
24+
$this->resolutionScope = Helper::resolveURI($this->resolutionScope, $id);
25+
return $prev;
26+
}
1227

1328
private $rootData;
1429

@@ -46,26 +61,17 @@ private function getRefProvider()
4661
}
4762

4863

49-
public function wat1()
50-
{
51-
if (isset($schemaArray[self::ID])) {
52-
$parentScope = $this->resolutionScope;
53-
$this->resolutionScope = Helper::resolveURI($parentScope, $schemaArray[self::ID]);
54-
/** @noinspection PhpUnusedLocalVariableInspection */
55-
$defer = new ScopeExit(function () use ($parentScope) {
56-
$this->resolutionScope = $parentScope;
57-
});
58-
}
59-
}
60-
6164
/**
6265
* @param $referencePath
6366
* @param string $resolutionScope
6467
* @return Ref
6568
* @throws \Exception
6669
*/
67-
private function resolveReference($referencePath, $resolutionScope = '')
70+
public function resolveReference($referencePath)
6871
{
72+
if ($this->resolutionScope) {
73+
$referencePath = Helper::resolveURI($this->resolutionScope, $referencePath);
74+
}
6975
$ref = &$this->refs[$referencePath];
7076
if (null === $ref) {
7177
if ($referencePath[0] === '#') {
@@ -88,26 +94,33 @@ private function resolveReference($referencePath, $resolutionScope = '')
8894
} elseif (is_array($branch) && isset($branch[$folder])) {
8995
$branch = &$branch[$folder];
9096
} else {
91-
throw new \Exception('Could not resolve ' . $referencePath . ': ' . $folder);
97+
throw new InvalidValue('Could not resolve ' . $referencePath . '@' . $this->resolutionScope . ': ' . $folder);
9298
}
9399
}
94100
$ref->setData($branch);
95101
}
96102
} else {
97103
$refParts = explode('#', $referencePath);
98-
$url = Helper::resolveURI($resolutionScope, $refParts[0]);
104+
$url = Helper::resolveURI($this->resolutionScope, $refParts[0]);
99105
$url = rtrim($url, '#');
100106
$refLocalPath = isset($refParts[1]) ? '#' . $refParts[1] : '#';
101107
$refResolver = &$this->remoteRefResolvers[$url];
102108
if (null === $refResolver) {
103109
$refResolver = new RefResolver($this->getRefProvider()->getSchemaData($url));
110+
$refResolver->url = $url;
104111
}
105112

106113
$ref = $refResolver->resolveReference($refLocalPath);
107114
}
108115
}
109116

110-
return $this->refs[$referencePath];
117+
$refData = $ref->getData();
118+
// we need to go deeper
119+
if (isset($refData->{'$ref'})) {
120+
return $this->resolveReference($refData->{'$ref'});
121+
}
122+
123+
return $ref;
111124
}
112125

113126

0 commit comments

Comments
 (0)