From 562198ce697c94e23de1662def80fdda918433f9 Mon Sep 17 00:00:00 2001 From: pmishev Date: Thu, 19 Nov 2015 14:03:54 +0000 Subject: [PATCH] Added ObjectInterface for all (nested) objects in documents to implement --- Document/DocumentInterface.php | 4 ++-- Document/ObjectInterface.php | 10 ++++++++++ Result/DocumentConverter.php | 34 ++++++++++++++++++---------------- 3 files changed, 30 insertions(+), 18 deletions(-) create mode 100644 Document/ObjectInterface.php diff --git a/Document/DocumentInterface.php b/Document/DocumentInterface.php index e5a0a01..3c62182 100644 --- a/Document/DocumentInterface.php +++ b/Document/DocumentInterface.php @@ -3,8 +3,8 @@ namespace Sineflow\ElasticsearchBundle\Document; /** - * Interface for ES Documents. + * Interface for Elasticsearch documents. */ -interface DocumentInterface +interface DocumentInterface extends ObjectInterface { } diff --git a/Document/ObjectInterface.php b/Document/ObjectInterface.php new file mode 100644 index 0000000..619bd6c --- /dev/null +++ b/Document/ObjectInterface.php @@ -0,0 +1,10 @@ +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 @@ -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(); @@ -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']) { @@ -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; @@ -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)) );