PHPLIB-242: Create helper to apply field paths for type maps

parent 01bf56b9
...@@ -241,3 +241,46 @@ function recursive_copy($element) { ...@@ -241,3 +241,46 @@ function recursive_copy($element) {
return clone $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;
}
...@@ -135,4 +135,70 @@ class FunctionsTest extends TestCase ...@@ -135,4 +135,70 @@ class FunctionsTest extends TestCase
[ ['replace' => 'collectionName'], false ], [ ['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.$',
],
];
}
} }
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