Skip to content

Commit a53ad07

Browse files
committed
overriding mapping classes
1 parent 8b004ed commit a53ad07

File tree

7 files changed

+94
-34
lines changed

7 files changed

+94
-34
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,31 @@ $options->skipValidation = true;
324324
325325
$res = $schema->in(json_decode('{"one":4}'), $options);
326326
$this->assertSame(4, $res->one);
327+
```
328+
329+
330+
#### Overriding mapping classes
331+
332+
If you want to map data to a different class you can register mapping at top level of your importer structure.
333+
334+
```
335+
class CustomSwaggerSchema extends SwaggerSchema
336+
{
337+
public static function setUpProperties($properties, JsonBasicSchema $ownerSchema)
338+
{
339+
parent::setUpProperties($properties, $ownerSchema);
340+
self::$objectItemClassMapping[Schema::className()] = CustomSchema::className();
341+
}
342+
}
343+
```
344+
345+
Or specify it in processing context
346+
347+
```
348+
$context = new Context();
349+
$context->objectItemClassMapping[Schema::className()] = CustomSchema::className();
350+
$schema = SwaggerSchema::schema()->in(json_decode(
351+
file_get_contents(__DIR__ . '/../../../../spec/petstore-swagger.json')
352+
), $context);
353+
$this->assertInstanceOf(CustomSchema::className(), $schema->definitions['User']);
327354
```

src/Schema.php

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -371,12 +371,13 @@ public function process($data, Context $options, $path = '#', $result = null)
371371
) {
372372
$refString = $data->{self::REF};
373373
// TODO consider process # by reference here ?
374-
$preRefScope = $options->refResolver->getResolutionScope();
374+
$refResolver = $options->refResolver;
375+
$preRefScope = $refResolver->getResolutionScope();
375376
/** @noinspection PhpUnusedLocalVariableInspection */
376-
$deferRefScope = new ScopeExit(function () use ($preRefScope, $options) {
377-
$options->refResolver->setResolutionScope($preRefScope);
377+
$deferRefScope = new ScopeExit(function () use ($preRefScope, $refResolver) {
378+
$refResolver->setResolutionScope($preRefScope);
378379
});
379-
$ref = $options->refResolver->resolveReference($refString);
380+
$ref = $refResolver->resolveReference($refString);
380381
if ($ref->isImported()) {
381382
$refResult = $ref->getImported();
382383
return $refResult;
@@ -597,27 +598,6 @@ public function setUseObjectAsArray($useObjectAsArray)
597598
return $this;
598599
}
599600

600-
/**
601-
* @param bool|Schema $additionalProperties
602-
* @return Schema
603-
*/
604-
public function setAdditionalProperties($additionalProperties)
605-
{
606-
$this->additionalProperties = $additionalProperties;
607-
return $this;
608-
}
609-
610-
/**
611-
* @param Schema|Schema[] $items
612-
* @return Schema
613-
*/
614-
public function setItems($items)
615-
{
616-
$this->items = $items;
617-
return $this;
618-
}
619-
620-
621601
private function fail(InvalidValue $exception, $path)
622602
{
623603
if ($path !== '#') {
@@ -676,13 +656,6 @@ public static function null()
676656
}
677657

678658

679-
public static function create()
680-
{
681-
$schema = new static();
682-
return $schema;
683-
}
684-
685-
686659
/**
687660
* @param Properties $properties
688661
* @return Schema

src/Structure/ClassStructure.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,14 @@ public static function properties()
7070
*/
7171
public static function import($data, Context $options = null)
7272
{
73+
$schema = static::schema();
7374
if (static::$objectItemClassMapping !== null) {
7475
if ($options === null) {
7576
$options = new Context();
7677
}
7778
$options->objectItemClassMapping = static::$objectItemClassMapping;
7879
}
79-
return static::schema()->in($data, $options);
80+
return $schema->in($data, $options);
8081
}
8182

8283
/**

tests/src/Helper/CustomSchema.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\Helper;
4+
5+
use Swaggest\JsonSchema\SwaggerSchema\Schema;
6+
7+
class CustomSchema extends Schema
8+
{
9+
10+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\Helper;
4+
5+
6+
use Swaggest\JsonSchema\Schema as JsonBasicSchema;
7+
use Swaggest\JsonSchema\SwaggerSchema\Schema;
8+
use Swaggest\JsonSchema\SwaggerSchema\SwaggerSchema;
9+
10+
class CustomSwaggerSchema extends SwaggerSchema
11+
{
12+
public static function setUpProperties($properties, JsonBasicSchema $ownerSchema)
13+
{
14+
parent::setUpProperties($properties, $ownerSchema);
15+
self::$objectItemClassMapping[Schema::className()] = CustomSchema::className();
16+
}
17+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\PHPUnit\CustomMapping;
4+
5+
6+
use Swaggest\JsonSchema\Context;
7+
use Swaggest\JsonSchema\SwaggerSchema\Schema;
8+
use Swaggest\JsonSchema\SwaggerSchema\SwaggerSchema;
9+
use Swaggest\JsonSchema\Tests\Helper\CustomSchema;
10+
use Swaggest\JsonSchema\Tests\Helper\CustomSwaggerSchema;
11+
12+
class CustomMappingTest extends \PHPUnit_Framework_TestCase
13+
{
14+
public function testMapping() {
15+
$schema = CustomSwaggerSchema::import(json_decode(
16+
file_get_contents(__DIR__ . '/../../../../spec/petstore-swagger.json')
17+
));
18+
19+
$this->assertInstanceOf(CustomSchema::className(), $schema->definitions['User']);
20+
}
21+
22+
public function testMappingWithContext() {
23+
$context = new Context();
24+
$context->objectItemClassMapping[Schema::className()] = CustomSchema::className();
25+
$schema = SwaggerSchema::schema()->in(json_decode(
26+
file_get_contents(__DIR__ . '/../../../../spec/petstore-swagger.json')
27+
), $context);
28+
$this->assertInstanceOf(CustomSchema::className(), $schema->definitions['User']);
29+
30+
}
31+
32+
}

tests/src/PHPUnit/Schema/ObjectStructure.php renamed to tests/src/PHPUnit/Schema/ObjectStructureTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
use Swaggest\JsonSchema\Schema;
77

8-
class ObjectStructure extends \PHPUnit_Framework_TestCase
8+
class ObjectStructureTest extends \PHPUnit_Framework_TestCase
99
{
1010

1111
public function testObjectAsArray()

0 commit comments

Comments
 (0)