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
e0908555
Unverified
Commit
e0908555
authored
Aug 05, 2019
by
Andreas Braun
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
PHPLIB-242: Create helper to apply field paths for type maps
parent
01bf56b9
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
109 additions
and
0 deletions
+109
-0
functions.php
src/functions.php
+43
-0
FunctionsTest.php
tests/FunctionsTest.php
+66
-0
No files found.
src/functions.php
View file @
e0908555
...
...
@@ -241,3 +241,46 @@ function recursive_copy($element) {
return
clone
$element
;
}
/**
* Creates a type map to apply to a field type
*
* This is used in the Aggregate, Distinct, and FindAndModify operations to
* apply the root-level type map to the document that will be returned. It also
* replaces the root type with object for consistency within these operations
*
* An existing type map for the given field path will not be overwritten
*
* @internal
* @param array $typeMap The existing typeMap
* @param string $fieldPath The field path to apply the root type to
* @return array
*/
function
create_field_path_type_map
(
array
$typeMap
,
$fieldPath
)
{
// If some field paths already exist, we prefix them with the field path we are assuming as the new root
if
(
isset
(
$typeMap
[
'fieldPaths'
])
&&
is_array
(
$typeMap
[
'fieldPaths'
]))
{
$fieldPaths
=
$typeMap
[
'fieldPaths'
];
$typeMap
[
'fieldPaths'
]
=
[];
foreach
(
$fieldPaths
as
$existingFieldPath
=>
$type
)
{
$typeMap
[
'fieldPaths'
][
$fieldPath
.
'.'
.
$existingFieldPath
]
=
$type
;
}
}
// If a root typemap was set, apply this to the field object
if
(
isset
(
$typeMap
[
'root'
]))
{
$typeMap
[
'fieldPaths'
][
$fieldPath
]
=
$typeMap
[
'root'
];
}
/* Special case if we want to convert an array, in which case we need to
* ensure that the field containing the array is exposed as an array,
* instead of the type given in the type map's array key. */
if
(
substr
(
$fieldPath
,
-
2
,
2
)
===
'.$'
)
{
$typeMap
[
'fieldPaths'
][
substr
(
$fieldPath
,
0
,
-
2
)]
=
'array'
;
}
$typeMap
[
'root'
]
=
'object'
;
return
$typeMap
;
}
tests/FunctionsTest.php
View file @
e0908555
...
...
@@ -135,4 +135,70 @@ class FunctionsTest extends TestCase
[
[
'replace'
=>
'collectionName'
],
false
],
];
}
/**
* @dataProvider provideTypeMapValues
*/
public
function
testCreateFieldPathTypeMap
(
array
$expected
,
array
$typeMap
,
$fieldPath
=
'field'
)
{
$this
->
assertEquals
(
$expected
,
\MongoDB\create_field_path_type_map
(
$typeMap
,
$fieldPath
));
}
public
function
provideTypeMapValues
()
{
return
[
'No root type'
=>
[
[
'document'
=>
'array'
,
'root'
=>
'object'
],
[
'document'
=>
'array'
],
],
'No field path'
=>
[
[
'root'
=>
'object'
,
'fieldPaths'
=>
[
'field'
=>
'array'
]],
[
'root'
=>
'array'
],
],
'Field path exists'
=>
[
[
'root'
=>
'object'
,
'fieldPaths'
=>
[
'field'
=>
'array'
,
'field.field'
=>
'object'
]],
[
'root'
=>
'array'
,
'fieldPaths'
=>
[
'field'
=>
'object'
]],
],
'Nested field path'
=>
[
[
'root'
=>
'object'
,
'fieldPaths'
=>
[
'field'
=>
'object'
,
'field.nested'
=>
'array'
]],
[
'root'
=>
'object'
,
'fieldPaths'
=>
[
'nested'
=>
'array'
]],
],
'Array field path converted to array'
=>
[
[
'root'
=>
'object'
,
'array'
=>
'MongoDB\Model\BSONArray'
,
'fieldPaths'
=>
[
'field'
=>
'array'
,
'field.$'
=>
'object'
,
'field.$.nested'
=>
'array'
,
]
],
[
'root'
=>
'object'
,
'array'
=>
'MongoDB\Model\BSONArray'
,
'fieldPaths'
=>
[
'nested'
=>
'array'
,
]
],
'field.$'
,
],
'Array field path without root key'
=>
[
[
'root'
=>
'object'
,
'array'
=>
'MongoDB\Model\BSONArray'
,
'fieldPaths'
=>
[
'field'
=>
'array'
,
'field.$.nested'
=>
'array'
,
]
],
[
'array'
=>
'MongoDB\Model\BSONArray'
,
'fieldPaths'
=>
[
'nested'
=>
'array'
,
]
],
'field.$'
,
],
];
}
}
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