Commit e7a5e3ca authored by Jeremy Mikola's avatar Jeremy Mikola

Create apply_type_map_to_document() utility function

parent 1b207079
......@@ -282,7 +282,7 @@ class Bucket
$file = $this->getRawFileDocumentForStream($stream);
// Filter the raw document through the specified type map
return \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($file), $this->typeMap);
return \MongoDB\apply_type_map_to_document($file, $this->typeMap);
}
/**
......@@ -302,7 +302,7 @@ class Bucket
* the root type so we can reliably access the ID.
*/
$typeMap = ['root' => 'stdClass'] + $this->typeMap;
$file = \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($file), $typeMap);
$file = \MongoDB\apply_type_map_to_document($file, $typeMap);
if ( ! isset($file->_id) && ! property_exists($file, '_id')) {
throw new CorruptFileException('file._id does not exist');
......
......@@ -9,6 +9,28 @@ use MongoDB\Driver\WriteConcern;
use MongoDB\Exception\InvalidArgumentException;
use stdClass;
/**
* Applies a type map to a document.
*
* This function is used by operations where it is not possible to apply a type
* map to the cursor directly because the root document is a command response
* (e.g. findAndModify).
*
* @internal
* @param array|object $document Document to which the type map will be applied
* @param array $typeMap Type map for BSON deserialization.
* @return array|object
* @throws InvalidArgumentException
*/
function apply_type_map_to_document($document, array $typeMap)
{
if ( ! is_array($document) && ! is_object($document)) {
throw InvalidArgumentException::invalidType('$document', $document, 'array or object');
}
return \MongoDB\BSON\toPHP(\MongoDB\BSON\fromPHP($document), $typeMap);
}
/**
* Extracts an ID from an inserted document.
*
......
......@@ -2,6 +2,7 @@
namespace MongoDB\Tests;
use MongoDB\Model\BSONArray;
use MongoDB\Model\BSONDocument;
use MongoDB\Driver\ReadConcern;
use MongoDB\Driver\WriteConcern;
......@@ -11,6 +12,54 @@ use MongoDB\Driver\WriteConcern;
*/
class FunctionsTest extends TestCase
{
/**
* @dataProvider provideDocumentAndTypeMap
*/
public function testApplyTypeMapToDocument($document, array $typeMap, $expectedDocument)
{
$this->assertEquals($expectedDocument, \MongoDB\apply_type_map_to_document($document, $typeMap));
}
public function provideDocumentAndTypeMap()
{
return [
[
[
'x' => 1,
'y' => (object) ['foo' => 'bar'],
'z' => [1, 2, 3],
],
[
'root' => 'object',
'document' => 'stdClass',
'array' => 'array',
],
(object) [
'x' => 1,
'y' => (object) ['foo' => 'bar'],
'z' => [1, 2, 3],
],
],
[
[
'x' => 1,
'y' => (object) ['foo' => 'bar'],
'z' => [1, 2, 3],
],
[
'root' => 'MongoDB\Model\BSONDocument',
'document' => 'MongoDB\Model\BSONDocument',
'array' => 'MongoDB\Model\BSONArray',
],
new BSONDocument([
'x' => 1,
'y' => new BSONDocument(['foo' => 'bar']),
'z' => new BSONArray([1, 2, 3]),
]),
],
];
}
/**
* @dataProvider provideIndexSpecificationDocumentsAndGeneratedNames
*/
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment