Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
M
mongo-php-library
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
sinan
mongo-php-library
Commits
e7a5e3ca
Commit
e7a5e3ca
authored
Dec 05, 2016
by
Jeremy Mikola
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Create apply_type_map_to_document() utility function
parent
1b207079
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
73 additions
and
2 deletions
+73
-2
Bucket.php
src/GridFS/Bucket.php
+2
-2
functions.php
src/functions.php
+22
-0
FunctionsTest.php
tests/FunctionsTest.php
+49
-0
No files found.
src/GridFS/Bucket.php
View file @
e7a5e3ca
...
...
@@ -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'
);
...
...
src/functions.php
View file @
e7a5e3ca
...
...
@@ -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.
*
...
...
tests/FunctionsTest.php
View file @
e7a5e3ca
...
...
@@ -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
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment