Skip to content

Commit f15bc5c

Browse files
committed
Added the ability to convert documents from an inherited index class
1 parent fe3801c commit f15bc5c

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

Mapping/Converter.php

+20-6
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,32 @@ public function convertArrayToDocument(string $namespace, array $raw)
3636

3737
public function convertDocumentToArray($document): array
3838
{
39-
$class = get_class($document);
39+
$documentClass = get_class($document);
40+
$class = $this->getConvertableClass($documentClass);
4041

41-
if (!isset($this->propertyMetadata[$class])) {
42-
throw new \Exception("Cannot convert object of class `$class` to array.");
42+
if ($class !== null) {
43+
return $this->normalize($document, null, $class);
4344
}
4445

45-
return $this->normalize($document);
46+
throw new \Exception("Cannot convert object of class `$documentClass` to array.");
4647
}
4748

48-
protected function normalize($document, $metadata = null)
49+
public function getConvertableClass($class) {
50+
while ($class) {
51+
if (isset($this->propertyMetadata[$class])) {
52+
return $class;
53+
}
54+
55+
$class = get_parent_class($class);
56+
}
57+
58+
return null;
59+
}
60+
61+
protected function normalize($document, $metadata = null, $class = null)
4962
{
50-
$metadata = $metadata ?? $this->propertyMetadata[get_class($document)];
63+
$class = $class ?? get_class($document);
64+
$metadata = $metadata ?? $this->propertyMetadata[$class];
5165
$result = [];
5266

5367
foreach ($metadata as $field => $fieldMeta) {

0 commit comments

Comments
 (0)