Skip to content

Commit

Permalink
Added the ability to convert documents from an inherited index class
Browse files Browse the repository at this point in the history
  • Loading branch information
0x6368 committed May 13, 2020
1 parent fe3801c commit f15bc5c
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions Mapping/Converter.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,32 @@ public function convertArrayToDocument(string $namespace, array $raw)

public function convertDocumentToArray($document): array
{
$class = get_class($document);
$documentClass = get_class($document);
$class = $this->getConvertableClass($documentClass);

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

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

protected function normalize($document, $metadata = null)
public function getConvertableClass($class) {
while ($class) {
if (isset($this->propertyMetadata[$class])) {
return $class;
}

$class = get_parent_class($class);
}

return null;
}

protected function normalize($document, $metadata = null, $class = null)
{
$metadata = $metadata ?? $this->propertyMetadata[get_class($document)];
$class = $class ?? get_class($document);
$metadata = $metadata ?? $this->propertyMetadata[$class];
$result = [];

foreach ($metadata as $field => $fieldMeta) {
Expand Down

0 comments on commit f15bc5c

Please sign in to comment.