diff --git a/src/core/etl/src/Flow/ETL/DSL/functions.php b/src/core/etl/src/Flow/ETL/DSL/functions.php index 60ae72b11..375db83ac 100644 --- a/src/core/etl/src/Flow/ETL/DSL/functions.php +++ b/src/core/etl/src/Flow/ETL/DSL/functions.php @@ -102,6 +102,11 @@ function array_sort(Expression $expression, string $function = null, int $flags return new Expression\ArraySort($expression, $function ? Sort::fromString($function) : Sort::sort, $flags, $recursive); } +function array_reverse(Expression $expression, bool $preserveKeys = false) : Expression +{ + return new Expression\ArrayReverse($expression, $preserveKeys); +} + function now(\DateTimeZone $time_zone = new \DateTimeZone('UTC')) : Expression { return new Expression\Now($time_zone); diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayExistsTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayExistsTest.php new file mode 100644 index 000000000..1ac5da572 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayExistsTest.php @@ -0,0 +1,43 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('has_array', array_exists(ref('array'), 'a')) + ->drop('row', 'array') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'has_array' => true], + ['id' => 2, 'has_array' => false], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetCollectionTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetCollectionTest.php new file mode 100644 index 000000000..1129f2a4c --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetCollectionTest.php @@ -0,0 +1,47 @@ +read( + From::array( + [ + ['id' => 1, 'array' => [ + ['a' => 1, 'b' => 2, 'c' => 3], + ['a' => 1, 'b' => 2, 'c' => 3], + ['a' => 1, 'b' => 2, 'c' => 3], + ]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('result', array_get_collection(ref('array'), 'a', 'c')) + ->drop('row', 'array') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'result' => [['a' => 1, 'c' => 3], ['a' => 1, 'c' => 3], ['a' => 1, 'c' => 3]]], + ['id' => 2, 'result' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetTest.php new file mode 100644 index 000000000..63a2847c6 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayGetTest.php @@ -0,0 +1,43 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('result', array_get(ref('array'), 'b')) + ->drop('row', 'array') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'result' => 2], + ['id' => 2, 'result' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeyRenameTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeyRenameTest.php new file mode 100644 index 000000000..a4b841f86 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeyRenameTest.php @@ -0,0 +1,43 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('array', array_key_rename(ref('array'), 'a', 'd')) + ->drop('row') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['b' => 2, 'c' => 3, 'd' => 1]], + ['id' => 2, 'array' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeysStyleConvertTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeysStyleConvertTest.php new file mode 100644 index 000000000..9789956ca --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayKeysStyleConvertTest.php @@ -0,0 +1,41 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['camelCased' => 1, 'snake_cased' => 2, 'space word' => 3]], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('array', array_keys_style_convert(ref('array'), 'kebab')) + ->drop('row') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['camel-cased' => 1, 'snake-cased' => 2, 'space-word' => 3]], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeCollectionTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeCollectionTest.php new file mode 100644 index 000000000..170a9eb95 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeCollectionTest.php @@ -0,0 +1,47 @@ +read( + From::array( + [ + ['id' => 1, 'array' => [ + ['a' => 1], + ['b' => 2], + ['c' => 3], + ]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('result', array_merge_collection(ref('array'))) + ->drop('row', 'array') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'result' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2, 'result' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeTest.php new file mode 100644 index 000000000..6ee120055 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayMergeTest.php @@ -0,0 +1,43 @@ +read( + From::array( + [ + ['id' => 1, 'first' => ['a' => 1, 'b' => 2], 'second' => ['c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->withEntry('array', array_merge(ref('first'), ref('second'))) + ->drop('row', 'first', 'second') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2, 'array' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayReverseTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayReverseTest.php new file mode 100644 index 000000000..2f9afb6d8 --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayReverseTest.php @@ -0,0 +1,42 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('array', ref('array')->arrayReverse()) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['c' => 3, 'b' => 2, 'a' => 1]], + ['id' => 2, 'array' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArraySortTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArraySortTest.php new file mode 100644 index 000000000..fa22de87c --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArraySortTest.php @@ -0,0 +1,42 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'c' => 3, 'b' => 2]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->withEntry('array', ref('array')->arraySort('ksort')) + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2, 'array' => null], + ], + $memory->data + ); + } +} diff --git a/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayUnpackTest.php b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayUnpackTest.php new file mode 100644 index 000000000..06b40ce0b --- /dev/null +++ b/src/core/etl/tests/Flow/ETL/Tests/Integration/Row/Reference/Expression/ArrayUnpackTest.php @@ -0,0 +1,41 @@ +read( + From::array( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ] + ) + ) + ->withEntry('row', ref('row')->unpack()) + ->renameAll('row.', '') + ->drop('row') + ->write(To::memory($memory = new ArrayMemory())) + ->run(); + + $this->assertSame( + [ + ['id' => 1, 'array' => ['a' => 1, 'b' => 2, 'c' => 3]], + ['id' => 2], + ], + $memory->data + ); + } +}