Skip to content

Commit d7feeab

Browse files
committed
Create apply_type_map_to_document() utility function
1 parent b433869 commit d7feeab

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

src/GridFS/Bucket.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff 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');

src/functions.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,28 @@
99
use MongoDB\Exception\InvalidArgumentException;
1010
use 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
*

tests/FunctionsTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace MongoDB\Tests;
44

5+
use MongoDB\Model\BSONArray;
56
use MongoDB\Model\BSONDocument;
67
use MongoDB\Driver\ReadConcern;
78
use MongoDB\Driver\WriteConcern;
@@ -11,6 +12,54 @@
1112
*/
1213
class 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' => 'object',
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
*/

0 commit comments

Comments
 (0)