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)
282
282
$ file = $ this ->getRawFileDocumentForStream ($ stream );
283
283
284
284
// 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 );
286
286
}
287
287
288
288
/**
@@ -302,7 +302,7 @@ public function getFileIdForStream($stream)
302
302
* the root type so we can reliably access the ID.
303
303
*/
304
304
$ 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 );
306
306
307
307
if ( ! isset ($ file ->_id ) && ! property_exists ($ file , '_id ' )) {
308
308
throw new CorruptFileException ('file._id does not exist ' );
Original file line number Diff line number Diff line change 9
9
use MongoDB \Exception \InvalidArgumentException ;
10
10
use stdClass ;
11
11
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
+
12
34
/**
13
35
* Extracts an ID from an inserted document.
14
36
*
Original file line number Diff line number Diff line change 2
2
3
3
namespace MongoDB \Tests ;
4
4
5
+ use MongoDB \Model \BSONArray ;
5
6
use MongoDB \Model \BSONDocument ;
6
7
use MongoDB \Driver \ReadConcern ;
7
8
use MongoDB \Driver \WriteConcern ;
11
12
*/
12
13
class FunctionsTest extends TestCase
13
14
{
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
+
14
63
/**
15
64
* @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
16
65
*/
You can’t perform that action at this time.
0 commit comments