1515use Doctrine \Common \Annotations \Reader ;
1616use ONGR \ElasticsearchBundle \Annotation \Document ;
1717use ONGR \ElasticsearchBundle \Annotation \Embedded ;
18+ use ONGR \ElasticsearchBundle \Annotation \HashMap ;
1819use ONGR \ElasticsearchBundle \Annotation \MetaField ;
20+ use ONGR \ElasticsearchBundle \Annotation \Nested ;
1921use ONGR \ElasticsearchBundle \Annotation \ParentDocument ;
2022use ONGR \ElasticsearchBundle \Annotation \Property ;
2123use ONGR \ElasticsearchBundle \Exception \MissingDocumentAnnotationException ;
@@ -36,6 +38,7 @@ class DocumentParser
3638 const PARENT_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\ParentDocument ' ;
3739 const ROUTING_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Routing ' ;
3840 const VERSION_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\Version ' ;
41+ const HASH_MAP_ANNOTATION = 'ONGR\ElasticsearchBundle\Annotation\HashMap ' ;
3942
4043 /**
4144 * @var Reader Used to read document annotations.
@@ -62,13 +65,6 @@ class DocumentParser
6265 */
6366 private $ properties = [];
6467
65- /**
66- * Analyzers used in documents.
67- *
68- * @var string[]
69- */
70- private $ analyzers = [];
71-
7268 /**
7369 * @param Reader $reader Used for reading annotations.
7470 * @param DocumentFinder $finder Used for resolving namespaces.
@@ -126,7 +122,7 @@ public function parse(\ReflectionClass $class)
126122 *
127123 * @param \ReflectionClass $document
128124 *
129- * @return Document|null
125+ * @return Document|object| null
130126 */
131127 private function getDocumentAnnotationData ($ document )
132128 {
@@ -138,7 +134,7 @@ private function getDocumentAnnotationData($document)
138134 *
139135 * @param \ReflectionProperty $property
140136 *
141- * @return Property|null
137+ * @return Property|object| null
142138 */
143139 private function getPropertyAnnotationData (\ReflectionProperty $ property )
144140 {
@@ -156,7 +152,7 @@ private function getPropertyAnnotationData(\ReflectionProperty $property)
156152 *
157153 * @param \ReflectionProperty $property
158154 *
159- * @return Embedded|null
155+ * @return Embedded|object| null
160156 */
161157 private function getEmbeddedAnnotationData (\ReflectionProperty $ property )
162158 {
@@ -169,6 +165,24 @@ private function getEmbeddedAnnotationData(\ReflectionProperty $property)
169165 return $ result ;
170166 }
171167
168+ /**
169+ * Returns HashMap annotation data from reader.
170+ *
171+ * @param \ReflectionProperty $property
172+ *
173+ * @return HashMap|object|null
174+ */
175+ private function getHashMapAnnotationData (\ReflectionProperty $ property )
176+ {
177+ $ result = $ this ->reader ->getPropertyAnnotation ($ property , self ::HASH_MAP_ANNOTATION );
178+
179+ if ($ result !== null && $ result ->name === null ) {
180+ $ result ->name = Caser::snake ($ property ->getName ());
181+ }
182+
183+ return $ result ;
184+ }
185+
172186 /**
173187 * Returns meta field annotation data from reader.
174188 *
@@ -237,6 +251,8 @@ private function getAliases(\ReflectionClass $reflectionClass, array &$metaField
237251 foreach ($ properties as $ name => $ property ) {
238252 $ type = $ this ->getPropertyAnnotationData ($ property );
239253 $ type = $ type !== null ? $ type : $ this ->getEmbeddedAnnotationData ($ property );
254+ $ type = $ type !== null ? $ type : $ this ->getHashMapAnnotationData ($ property );
255+
240256 if ($ type === null && $ metaFields !== null
241257 && ($ metaData = $ this ->getMetaFieldAnnotationData ($ property )) !== null ) {
242258 $ metaFields [$ metaData ['name ' ]] = $ metaData ['settings ' ];
@@ -252,6 +268,13 @@ private function getAliases(\ReflectionClass $reflectionClass, array &$metaField
252268 $ alias [$ type ->name ]['type ' ] = $ type ->type ;
253269 }
254270
271+ if ($ type instanceof HashMap) {
272+ $ alias [$ type ->name ]['type ' ] = HashMap::NAME ;
273+ $ alias [$ type ->name ]['aliases ' ] = [];
274+ }
275+
276+ $ alias [$ type ->name ][HashMap::NAME ] = $ type instanceof HashMap;
277+
255278 switch (true ) {
256279 case $ property ->isPublic ():
257280 $ propertyType = 'public ' ;
@@ -364,6 +387,7 @@ private function registerAnnotations()
364387 'ParentDocument ' ,
365388 'Routing ' ,
366389 'Version ' ,
390+ 'HashMap ' ,
367391 ];
368392
369393 foreach ($ annotations as $ annotation ) {
@@ -480,6 +504,7 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
480504 foreach ($ this ->getDocumentPropertiesReflection ($ reflectionClass ) as $ name => $ property ) {
481505 $ type = $ this ->getPropertyAnnotationData ($ property );
482506 $ type = $ type !== null ? $ type : $ this ->getEmbeddedAnnotationData ($ property );
507+ $ type = $ type !== null ? $ type : $ this ->getHashMapAnnotationData ($ property );
483508
484509 if ((in_array ($ name , $ properties ) && !$ flag )
485510 || (!in_array ($ name , $ properties ) && $ flag )
@@ -495,6 +520,14 @@ private function getProperties(\ReflectionClass $reflectionClass, $properties =
495520 $ map = array_replace_recursive ($ map , $ this ->getObjectMapping ($ type ->class ));
496521 }
497522
523+ // HashMap object
524+ if ($ type instanceof HashMap) {
525+ $ map = array_replace_recursive ($ map , [
526+ 'type ' => Nested::NAME ,
527+ 'dynamic ' => true ,
528+ ]);
529+ }
530+
498531 // If there is set some Raw options, it will override current ones.
499532 if (isset ($ map ['options ' ])) {
500533 $ options = $ map ['options ' ];
0 commit comments