Skip to content

Commit

Permalink
Added ObjectInterface for all (nested) objects in documents to implement
Browse files Browse the repository at this point in the history
  • Loading branch information
pmishev committed Nov 19, 2015
1 parent ed9c8c6 commit 562198c
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 18 deletions.
4 changes: 2 additions & 2 deletions Document/DocumentInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Sineflow\ElasticsearchBundle\Document;

/**
* Interface for ES Documents.
* Interface for Elasticsearch documents.
*/
interface DocumentInterface
interface DocumentInterface extends ObjectInterface
{
}
10 changes: 10 additions & 0 deletions Document/ObjectInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

namespace Sineflow\ElasticsearchBundle\Document;

/**
* Interface for (nested) objects within Elasticsearch documents.
*/
interface ObjectInterface
{
}
34 changes: 18 additions & 16 deletions Result/DocumentConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Sineflow\ElasticsearchBundle\Document\DocumentInterface;
use Sineflow\ElasticsearchBundle\Document\MLProperty;
use Sineflow\ElasticsearchBundle\Document\ObjectInterface;
use Sineflow\ElasticsearchBundle\Mapping\ClassMetadata;
use Sineflow\ElasticsearchBundle\Mapping\DocumentMetadata;
use Sineflow\ElasticsearchBundle\Mapping\DocumentMetadataCollector;
Expand Down Expand Up @@ -76,23 +77,23 @@ public function convertToDocument($rawData, $documentClass)
}
}

/** @var DocumentInterface $object */
/** @var DocumentInterface $document */
$className = $metadata->getClassName();
$object = $this->assignArrayToObject($data, new $className(), $metadata->getPropertiesMetadata());
$document = $this->assignArrayToObject($data, new $className(), $metadata->getPropertiesMetadata());

return $object;
return $document;
}

/**
* Assigns all properties to object.
*
* @param array $array Flat array with fields and their value
* @param object $object A document or a (nested) object object
* @param array $propertiesMetadata
* @param array $array Flat array with fields and their value
* @param ObjectInterface $object A document or a (nested) object
* @param array $propertiesMetadata
*
* @return object
* @return ObjectInterface
*/
public function assignArrayToObject(array $array, $object, array $propertiesMetadata)
public function assignArrayToObject(array $array, ObjectInterface $object, array $propertiesMetadata)
{
foreach ($propertiesMetadata as $esField => $propertyMetadata) {
// Skip fields from the mapping that have no value set, unless they are multilanguage fields
Expand Down Expand Up @@ -138,12 +139,12 @@ public function assignArrayToObject(array $array, $object, array $propertiesMeta
/**
* Converts document or (nested) object to an array.
*
* @param mixed $object Can be instance of DocumentInterface or a (nested) object
* @param array $propertiesMetadata
* @param ObjectInterface $object A document or a (nested) object
* @param array $propertiesMetadata
*
* @return array
*/
public function convertToArray($object, $propertiesMetadata = [])
public function convertToArray(ObjectInterface $object, $propertiesMetadata = [])
{
if (empty($propertiesMetadata)) {
$propertiesMetadata = $this->metadataCollector->getDocumentMetadata(get_class($object))->getPropertiesMetadata();
Expand All @@ -159,6 +160,7 @@ public function convertToArray($object, $propertiesMetadata = [])
}

if (isset($value)) {
// If this is a (nested) object
if (array_key_exists('propertiesMetadata', $propertyMetadata)) {
$new = [];
if ($propertyMetadata['multiple']) {
Expand All @@ -168,11 +170,11 @@ public function convertToArray($object, $propertiesMetadata = [])
}

foreach ($value as $item) {
$this->checkVariableType($item, $propertyMetadata['className']);
$this->checkObjectType($item, $propertyMetadata['className']);
$new[] = $this->convertToArray($item, $propertyMetadata['propertiesMetadata']);
}
} else {
$this->checkVariableType($value, $propertyMetadata['className']);
$this->checkObjectType($value, $propertyMetadata['className']);
$new = $this->convertToArray($value, $propertyMetadata['propertiesMetadata']);
}
$value = $new;
Expand All @@ -194,14 +196,14 @@ public function convertToArray($object, $propertiesMetadata = [])
/**
* Check if object is the correct type
*
* @param object $object
* @param ObjectInterface $object
* @param array $expectedClass
*
* @throws \InvalidArgumentException
*/
private function checkVariableType($object, $expectedClass)
private function checkObjectType(ObjectInterface $object, $expectedClass)
{
if (!is_object($object) || get_class($object) !== $expectedClass) {
if (get_class($object) !== $expectedClass) {
throw new \InvalidArgumentException(
sprintf('Expected object of type "%s", got "%s"', $expectedClass, get_class($object))
);
Expand Down

0 comments on commit 562198c

Please sign in to comment.