Skip to content

Commit a2ca4f1

Browse files
committed
custom class mapping
1 parent 8791bc4 commit a2ca4f1

File tree

3 files changed

+16
-22
lines changed

3 files changed

+16
-22
lines changed

README.md

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,13 @@ If you want to map data to a different class you can register mapping at top lev
334334
```php
335335
class CustomSwaggerSchema extends SwaggerSchema
336336
{
337-
public static function setUpProperties($properties, JsonBasicSchema $ownerSchema)
337+
public static function import($data, Context $options = null)
338338
{
339-
parent::setUpProperties($properties, $ownerSchema);
340-
self::$objectItemClassMapping[Schema::className()] = CustomSchema::className();
339+
if ($options === null) {
340+
$options = new Context();
341+
}
342+
$options->objectItemClassMapping[Schema::className()] = CustomSchema::className();
343+
return parent::import($data, $options);
341344
}
342345
}
343346
```

src/Structure/ClassStructure.php

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,14 @@ public static function properties()
5757
return static::schema()->properties;
5858
}
5959

60-
/**
61-
* @var string[] override of objectItemClass
62-
* @see \Swaggest\JsonSchema\Context::$objectItemClassMapping
63-
*/
64-
protected static $objectItemClassMapping;
65-
6660
/**
6761
* @param $data
6862
* @param \Swaggest\JsonSchema\Context $options
6963
* @return static
7064
*/
7165
public static function import($data, Context $options = null)
7266
{
73-
$schema = static::schema();
74-
if (static::$objectItemClassMapping !== null) {
75-
if ($options === null) {
76-
$options = new Context();
77-
}
78-
$options->objectItemClassMapping = static::$objectItemClassMapping;
79-
}
80-
return $schema->in($data, $options);
67+
return static::schema()->in($data, $options);
8168
}
8269

8370
/**
@@ -159,7 +146,8 @@ public function __set($name, $column) // todo nested schemas
159146
return $this;
160147
}
161148

162-
public static function className() {
149+
public static function className()
150+
{
163151
return get_called_class();
164152
}
165153
}

tests/src/Helper/CustomSwaggerSchema.php

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,18 @@
33
namespace Swaggest\JsonSchema\Tests\Helper;
44

55

6-
use Swaggest\JsonSchema\Schema as JsonBasicSchema;
6+
use Swaggest\JsonSchema\Context;
77
use Swaggest\JsonSchema\SwaggerSchema\Schema;
88
use Swaggest\JsonSchema\SwaggerSchema\SwaggerSchema;
99

1010
class CustomSwaggerSchema extends SwaggerSchema
1111
{
12-
public static function setUpProperties($properties, JsonBasicSchema $ownerSchema)
12+
public static function import($data, Context $options = null)
1313
{
14-
parent::setUpProperties($properties, $ownerSchema);
15-
self::$objectItemClassMapping[Schema::className()] = CustomSchema::className();
14+
if ($options === null) {
15+
$options = new Context();
16+
}
17+
$options->objectItemClassMapping[Schema::className()] = CustomSchema::className();
18+
return parent::import($data, $options);
1619
}
1720
}

0 commit comments

Comments
 (0)