Skip to content

Commit 7641235

Browse files
committed
class mapping
1 parent de29f20 commit 7641235

File tree

3 files changed

+26
-4
lines changed

3 files changed

+26
-4
lines changed

src/Context.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ class Context extends MagicMap
2222
/** @var bool */
2323
public $skipValidation = false;
2424

25+
/** @var string[] map of from -> to class names */
26+
public $objectItemClassMapping;
27+
2528
/**
2629
* @param boolean $skipValidation
2730
* @return Context

src/Schema.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public function process($data, Context $options, $path = '#', $result = null)
344344
if ($this->useObjectAsArray) {
345345
$result = array();
346346
} elseif (!$result instanceof ObjectItem) {
347-
$result = $this->makeObjectItem();
347+
$result = $this->makeObjectItem($options);
348348

349349
if ($result instanceof ClassStructure) {
350350
if ($result->__validateOnSet) {
@@ -720,14 +720,21 @@ public function getMeta($className)
720720
}
721721

722722
/**
723+
* @param Context $options
723724
* @return ObjectItem
724725
*/
725-
public function makeObjectItem()
726+
public function makeObjectItem(Context $options = null)
726727
{
727728
if (null === $this->objectItemClass) {
728729
return new ObjectItem();
729730
} else {
730-
return new $this->objectItemClass;
731+
$className = $this->objectItemClass;
732+
if ($options !== null) {
733+
if (isset($options->objectItemClassMapping[$className])) {
734+
$className = $options->objectItemClassMapping[$className];
735+
}
736+
}
737+
return new $className;
731738
}
732739
}
733740
}

src/Structure/ClassStructure.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,13 +57,25 @@ 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+
6066
/**
6167
* @param $data
62-
* @param Context $options
68+
* @param \Swaggest\JsonSchema\Context $options
6369
* @return static
6470
*/
6571
public static function import($data, Context $options = null)
6672
{
73+
if (static::$objectItemClassMapping !== null) {
74+
if ($options === null) {
75+
$options = new Context();
76+
}
77+
$options->objectItemClassMapping = static::$objectItemClassMapping;
78+
}
6779
return static::schema()->in($data, $options);
6880
}
6981

0 commit comments

Comments
 (0)