File tree Expand file tree Collapse file tree 3 files changed +73
-2
lines changed
Expand file tree Collapse file tree 3 files changed +73
-2
lines changed Original file line number Diff line number Diff line change @@ -282,7 +282,7 @@ public function getFileDocumentForStream($stream)
282282 $ file = $ this ->getRawFileDocumentForStream ($ stream );
283283
284284 // Filter the raw document through the specified type map
285- return \MongoDB \BSON \toPHP ( \ MongoDB \ BSON \fromPHP ( $ file) , $ this ->typeMap );
285+ return \MongoDB \apply_type_map_to_document ( $ file , $ this ->typeMap );
286286 }
287287
288288 /**
@@ -302,7 +302,7 @@ public function getFileIdForStream($stream)
302302 * the root type so we can reliably access the ID.
303303 */
304304 $ typeMap = ['root ' => 'stdClass ' ] + $ this ->typeMap ;
305- $ file = \MongoDB \BSON \toPHP ( \ MongoDB \ BSON \fromPHP ( $ file) , $ typeMap );
305+ $ file = \MongoDB \apply_type_map_to_document ( $ file , $ typeMap );
306306
307307 if ( ! isset ($ file ->_id ) && ! property_exists ($ file , '_id ' )) {
308308 throw new CorruptFileException ('file._id does not exist ' );
Original file line number Diff line number Diff line change 99use MongoDB \Exception \InvalidArgumentException ;
1010use stdClass ;
1111
12+ /**
13+ * Applies a type map to a document.
14+ *
15+ * This function is used by operations where it is not possible to apply a type
16+ * map to the cursor directly because the root document is a command response
17+ * (e.g. findAndModify).
18+ *
19+ * @internal
20+ * @param array|object $document Document to which the type map will be applied
21+ * @param array $typeMap Type map for BSON deserialization.
22+ * @return array|object
23+ * @throws InvalidArgumentException
24+ */
25+ function apply_type_map_to_document ($ document , array $ typeMap )
26+ {
27+ if ( ! is_array ($ document ) && ! is_object ($ document )) {
28+ throw InvalidArgumentException::invalidType ('$document ' , $ document , 'array or object ' );
29+ }
30+
31+ return \MongoDB \BSON \toPHP (\MongoDB \BSON \fromPHP ($ document ), $ typeMap );
32+ }
33+
1234/**
1335 * Extracts an ID from an inserted document.
1436 *
Original file line number Diff line number Diff line change 22
33namespace MongoDB \Tests ;
44
5+ use MongoDB \Model \BSONArray ;
56use MongoDB \Model \BSONDocument ;
67use MongoDB \Driver \ReadConcern ;
78use MongoDB \Driver \WriteConcern ;
1112 */
1213class FunctionsTest extends TestCase
1314{
15+ /**
16+ * @dataProvider provideDocumentAndTypeMap
17+ */
18+ public function testApplyTypeMapToDocument ($ document , array $ typeMap , $ expectedDocument )
19+ {
20+ $ this ->assertEquals ($ expectedDocument , \MongoDB \apply_type_map_to_document ($ document , $ typeMap ));
21+ }
22+
23+ public function provideDocumentAndTypeMap ()
24+ {
25+ return [
26+ [
27+ [
28+ 'x ' => 1 ,
29+ 'y ' => (object ) ['foo ' => 'bar ' ],
30+ 'z ' => [1 , 2 , 3 ],
31+ ],
32+ [
33+ 'root ' => 'object ' ,
34+ 'document ' => 'stdClass ' ,
35+ 'array ' => 'array ' ,
36+ ],
37+ (object ) [
38+ 'x ' => 1 ,
39+ 'y ' => (object ) ['foo ' => 'bar ' ],
40+ 'z ' => [1 , 2 , 3 ],
41+ ],
42+ ],
43+ [
44+ [
45+ 'x ' => 1 ,
46+ 'y ' => (object ) ['foo ' => 'bar ' ],
47+ 'z ' => [1 , 2 , 3 ],
48+ ],
49+ [
50+ 'root ' => 'MongoDB\Model\BSONDocument ' ,
51+ 'document ' => 'MongoDB\Model\BSONDocument ' ,
52+ 'array ' => 'MongoDB\Model\BSONArray ' ,
53+ ],
54+ new BSONDocument ([
55+ 'x ' => 1 ,
56+ 'y ' => new BSONDocument (['foo ' => 'bar ' ]),
57+ 'z ' => new BSONArray ([1 , 2 , 3 ]),
58+ ]),
59+ ],
60+ ];
61+ }
62+
1463 /**
1564 * @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
1665 */
You can’t perform that action at this time.
0 commit comments