@@ -59,38 +59,7 @@ public function setPropertyAccessor(PropertyAccessorInterface $propertyAccessor)
5959 public function transform ($ object , array $ fields )
6060 {
6161 $ identifier = $ this ->propertyAccessor ->getValue ($ object , $ this ->options ['identifier ' ]);
62- $ document = new Document ($ identifier );
63-
64- foreach ($ fields as $ key => $ mapping ) {
65- if ($ key == '_parent ' ) {
66- $ property = (null !== $ mapping ['property ' ]) ? $ mapping ['property ' ] : $ mapping ['type ' ];
67- $ value = $ this ->propertyAccessor ->getValue ($ object , $ property );
68- $ document ->setParent ($ this ->propertyAccessor ->getValue ($ value , $ mapping ['identifier ' ]));
69- continue ;
70- }
71-
72- $ value = $ this ->propertyAccessor ->getValue ($ object , $ key );
73-
74- if (isset ($ mapping ['type ' ]) && in_array ($ mapping ['type ' ], array ('nested ' , 'object ' )) && isset ($ mapping ['properties ' ]) && !empty ($ mapping ['properties ' ])) {
75- /* $value is a nested document or object. Transform $value into
76- * an array of documents, respective the mapped properties.
77- */
78- $ document ->set ($ key , $ this ->transformNested ($ value , $ mapping ['properties ' ]));
79- continue ;
80- }
81-
82- if (isset ($ mapping ['type ' ]) && $ mapping ['type ' ] == 'attachment ' ) {
83- // $value is an attachment. Add it to the document.
84- if ($ value instanceof \SplFileInfo) {
85- $ document ->addFile ($ key , $ value ->getPathName ());
86- } else {
87- $ document ->addFileContent ($ key , $ value );
88- }
89- continue ;
90- }
91-
92- $ document ->set ($ key , $ this ->normalizeValue ($ value ));
93- }
62+ $ document = $ this ->transformObjectToDocument ($ object , $ fields , $ identifier );
9463
9564 return $ document ;
9665 }
@@ -108,13 +77,13 @@ protected function transformNested($objects, array $fields)
10877 if (is_array ($ objects ) || $ objects instanceof \Traversable || $ objects instanceof \ArrayAccess) {
10978 $ documents = array ();
11079 foreach ($ objects as $ object ) {
111- $ document = $ this ->transform ($ object , $ fields );
80+ $ document = $ this ->transformObjectToDocument ($ object , $ fields );
11281 $ documents [] = $ document ->getData ();
11382 }
11483
11584 return $ documents ;
11685 } elseif (null !== $ objects ) {
117- $ document = $ this ->transform ($ objects , $ fields );
86+ $ document = $ this ->transformObjectToDocument ($ objects , $ fields );
11887
11988 return $ document ->getData ();
12089 }
@@ -148,4 +117,50 @@ protected function normalizeValue($value)
148117
149118 return $ value ;
150119 }
120+
121+ /**
122+ * Transforms the given object to an elastica document
123+ *
124+ * @param object $object the object to convert
125+ * @param array $fields the keys we want to have in the returned array
126+ * @param string $identifier the identifier for the new document
127+ * @return Document
128+ */
129+ protected function transformObjectToDocument ($ object , array $ fields , $ identifier = '' )
130+ {
131+ $ document = new Document ($ identifier );
132+
133+ foreach ($ fields as $ key => $ mapping ) {
134+ if ($ key == '_parent ' ) {
135+ $ property = (null !== $ mapping ['property ' ]) ? $ mapping ['property ' ] : $ mapping ['type ' ];
136+ $ value = $ this ->propertyAccessor ->getValue ($ object , $ property );
137+ $ document ->setParent ($ this ->propertyAccessor ->getValue ($ value , $ mapping ['identifier ' ]));
138+ continue ;
139+ }
140+
141+ $ value = $ this ->propertyAccessor ->getValue ($ object , $ key );
142+
143+ if (isset ($ mapping ['type ' ]) && in_array ($ mapping ['type ' ], array ('nested ' , 'object ' )) && isset ($ mapping ['properties ' ]) && !empty ($ mapping ['properties ' ])) {
144+ /* $value is a nested document or object. Transform $value into
145+ * an array of documents, respective the mapped properties.
146+ */
147+ $ document ->set ($ key , $ this ->transformNested ($ value , $ mapping ['properties ' ]));
148+ continue ;
149+ }
150+
151+ if (isset ($ mapping ['type ' ]) && $ mapping ['type ' ] == 'attachment ' ) {
152+ // $value is an attachment. Add it to the document.
153+ if ($ value instanceof \SplFileInfo) {
154+ $ document ->addFile ($ key , $ value ->getPathName ());
155+ } else {
156+ $ document ->addFileContent ($ key , $ value );
157+ }
158+ continue ;
159+ }
160+
161+ $ document ->set ($ key , $ this ->normalizeValue ($ value ));
162+ }
163+
164+ return $ document ;
165+ }
151166}
0 commit comments