From d7c06d2b48dd7505e44395959ce56e0c9be3da8c Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Tue, 18 Jul 2023 16:50:11 +0100 Subject: [PATCH 1/2] Adding mergeAndMapAll function on Mapper.php --- Tools/Mapper/Mapper.php | 53 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 52 insertions(+), 1 deletion(-) diff --git a/Tools/Mapper/Mapper.php b/Tools/Mapper/Mapper.php index 320515f2..24a0e4b1 100644 --- a/Tools/Mapper/Mapper.php +++ b/Tools/Mapper/Mapper.php @@ -158,6 +158,57 @@ public function mapAll($elements, $mappingName, $context = [], $disassociate = f return $res; } + /** + * @param array $elements + * @param string $mappingName + * @param array $toMerge + * @param array $context + * @param bool $disassociate + * + * @return array|mixed + */ + public function mergeAndMapAll($elements, $mappingName, $toMerge = [], $context = [], $disassociate = false) + { + if (!is_array($elements)) { + throw new \RuntimeException('mergeAndMapAll expected an array'); + } + + if (empty($elements)) { + return $elements; + } + + $res = []; + foreach ($elements as $key => $element) { + $valuesToMerge = []; + foreach ($toMerge as $keyToMerge) { + if (array_key_exists($keyToMerge, $element)) { + $valuesToMerge[$keyToMerge] = $element[$keyToMerge]; + unset($element[$keyToMerge]); + } + } + if (!empty($valuesToMerge)) { + $newElement = []; + foreach ($valuesToMerge as $keyToRemap => $valueToRemap) { + foreach($valueToRemap as $lineNum => $val) { + $newElement[$lineNum][$keyToRemap] = $val; + } + } + + foreach ($newElement as $newKey => $newValue) { + $newElement[$newKey] = array_merge($element, $newValue); + } + + $newElement = ($disassociate) ? ['key' => $key, 'value' => $newElement] : $newElement; + $res[$key] = $this->mapAll($newElement, $mappingName, $context); + } else { + $element = ($disassociate) ? ['key' => $key, 'value' => $element] : $element; + $res[$key] = $this->map($element, $mappingName, $context); + } + } + + return $res; + } + /** * Return true if the key exists in the given array. * @@ -281,7 +332,7 @@ protected function transformToSoapVar(&$item, $key) public function arrayToSoapVars(array $data) { array_walk_recursive($data, [$this, 'transformToSoapVar']); - + return $data; } From b40d4929bf839d1606c16983eae40d9ed65bca18 Mon Sep 17 00:00:00 2001 From: Paulo Lopes Date: Wed, 19 Jul 2023 15:46:48 +0100 Subject: [PATCH 2/2] Adding unit test for the mapper class, but it needs to be reviewed --- Tests/Functional/Tools/Mapper/MapperTest.php | 98 ++++++++++++++++++++ 1 file changed, 98 insertions(+) diff --git a/Tests/Functional/Tools/Mapper/MapperTest.php b/Tests/Functional/Tools/Mapper/MapperTest.php index 4092b94d..e22c9980 100644 --- a/Tests/Functional/Tools/Mapper/MapperTest.php +++ b/Tests/Functional/Tools/Mapper/MapperTest.php @@ -374,4 +374,102 @@ public function testMapAll(array $mappings, $mappingName, array $elements, $cont $this->assertEquals($expectedValue, $res); } + + public function dataProviderForMergeAndMapAll() + { + $dataProviderFromMapAll = $this->dataProviderForMapAll(); + $dataProviderForMergeAndMapAll = [ + 'Test mapping with array keys to merge' => [ + 'mappings' => [ + 'xy_to_xyz' => [ + 'x' => "obj.get('x') + 1", + 'y' => "obj.get('x') + 2", + 'z' => "obj.get('x') + 3", + ], + ], + 'mapping_name' => 'xy_to_xyz', + 'elements' => [ + 'x_10' => new SerializableArray(['x' => 10]), + 'x_11' => new SerializableArray(['x' => 11]), + 'x_12' => new SerializableArray(['x' => 12]), + 'x_13_14' => new SerializableArray(['x' => [13, 14]]), + 'x_15_16' => new SerializableArray(['x' => [15, 16]]), + ], + 'to_merge' => ['x_13_14', 'x_15_16'], + 'context' => [], + 'expected_value' => [ + [ + 0 => [ + 'x_10' => [ + 'x' => 11, + 'y' => 12, + 'z' => 13, + ], + 'x_11' => [ + 'x' => 12, + 'y' => 13, + 'z' => 14, + ], + 'x_12' => [ + 'x' => 13, + 'y' => 14, + 'z' => 15, + ], + 'x_13_14' => [ + 'x' => 1, //TODO array + 1 = 1, it needs to be correct regarding the scenario of merging + 'y' => 1, + 'z' => 1, + ], + ], + 1 => [ + 'x_10' => [ + 'x' => 11, + 'y' => 12, + 'z' => 13, + ], + 'x_11' => [ + 'x' => 12, + 'y' => 13, + 'z' => 14, + ], + 'x_12' => [ + 'x' => 13, + 'y' => 14, + 'z' => 15, + ], + 'x_15_16' => [ + 'x' => 1, + 'y' => 1, + 'z' => 1, + ], + ] + ] + ], + ], + ]; + + return array_merge($dataProviderFromMapAll, $dataProviderForMergeAndMapAll); + } + + /** + * @covers ::mergeAndMapAll + * @covers ::map + * @covers ::addMappings + * + * @dataProvider dataProviderForMergeAndMapAll + * + * @param array $mappings + * @param $mappingName + * @param array $toMerge + * @param array $elements + * @param array $expectedValue + */ + public function testMergeAndMapAll(array $mappings, $mappingName, array $elements, array $toMerge, $context, array $expectedValue) + { + $this->mapper->addMappings($mappings); + + $res = $this->mapper->mergeAndMapAll($elements, $mappingName, $context, $toMerge); + + $this->assertEquals($expectedValue, $res); + } }