From e9387874953e358a99907027ddc41b55a7b20aaf Mon Sep 17 00:00:00 2001 From: Roj Vroemen Date: Wed, 11 Sep 2024 17:45:29 +0200 Subject: [PATCH 1/3] Add support for named and lazy values --- src/Matrix.php | 15 ++++++++++++--- src/MatrixTest.php | 26 ++++++++++++++++---------- src/Value.php | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 13 deletions(-) create mode 100644 src/Value.php diff --git a/src/Matrix.php b/src/Matrix.php index 1868375..f15a123 100644 --- a/src/Matrix.php +++ b/src/Matrix.php @@ -19,15 +19,20 @@ public static function create(array $matrix): Generator foreach (self::recurse($matrix, []) as $result) { $key = implode(' ', array_map( - fn (mixed $value, int $i) => sprintf( + fn (Value $value, int $i) => sprintf( '%s=%s', $names[$i], - json_encode($value), + $value->label, ), $result, array_keys($result), )); + $result = array_map( + fn (Value $value) => $value->resolve(), + $result, + ); + yield $key => $result; } } @@ -35,7 +40,7 @@ public static function create(array $matrix): Generator /** * @param array $matrix * @param array $head - * @return Generator> + * @return Generator> */ private static function recurse(array $matrix, array $head): Generator { @@ -46,6 +51,10 @@ private static function recurse(array $matrix, array $head): Generator } foreach (array_shift($matrix) as $value) { + if (! $value instanceof Value) { + $value = Value::wrap($value); + } + foreach (self::recurse($matrix, [...$head, $value]) as $result) { yield $result; } diff --git a/src/MatrixTest.php b/src/MatrixTest.php index bba3772..bf405b4 100644 --- a/src/MatrixTest.php +++ b/src/MatrixTest.php @@ -14,19 +14,25 @@ public function cases(): void { $result = Matrix::create([ 'php' => [8.3, 8.4], - 'node' => [18, 20], - 'postgres' => ['v15', 'v16'], + 'node' => [ + Value::of('18', 18), + Value::of('20', fn () => 20), + ], + 'postgres' => [ + Value::of('v15', '15.0'), + Value::lazy('v16', fn () => '16.0'), + ], ]); $this->assertSame([ - 'php=8.3 node=18 postgres="v15"' => [8.3, 18, 'v15'], - 'php=8.3 node=18 postgres="v16"' => [8.3, 18, 'v16'], - 'php=8.3 node=20 postgres="v15"' => [8.3, 20, 'v15'], - 'php=8.3 node=20 postgres="v16"' => [8.3, 20, 'v16'], - 'php=8.4 node=18 postgres="v15"' => [8.4, 18, 'v15'], - 'php=8.4 node=18 postgres="v16"' => [8.4, 18, 'v16'], - 'php=8.4 node=20 postgres="v15"' => [8.4, 20, 'v15'], - 'php=8.4 node=20 postgres="v16"' => [8.4, 20, 'v16'], + 'php=8.3 node=18 postgres=v15' => [8.3, 18, '15.0'], + 'php=8.3 node=18 postgres=v16' => [8.3, 18, '16.0'], + 'php=8.3 node=20 postgres=v15' => [8.3, 20, '15.0'], + 'php=8.3 node=20 postgres=v16' => [8.3, 20, '16.0'], + 'php=8.4 node=18 postgres=v15' => [8.4, 18, '15.0'], + 'php=8.4 node=18 postgres=v16' => [8.4, 18, '16.0'], + 'php=8.4 node=20 postgres=v15' => [8.4, 20, '15.0'], + 'php=8.4 node=20 postgres=v16' => [8.4, 20, '16.0'], ], iterator_to_array($result)); } } diff --git a/src/Value.php b/src/Value.php new file mode 100644 index 0000000..d0728a5 --- /dev/null +++ b/src/Value.php @@ -0,0 +1,40 @@ + $value, + ); + } + + public static function wrap(mixed $value): self + { + return self::of(json_encode($value), $value); + } + + public static function lazy(string $label, callable $resolve): self + { + return new self($label, $resolve(...)); + } + + public function resolve(): mixed + { + return ($this->resolve)(); + } +} From 54e95669a6bee88530507134b16d6494016af480 Mon Sep 17 00:00:00 2001 From: Roj Vroemen Date: Wed, 11 Sep 2024 17:49:46 +0200 Subject: [PATCH 2/3] Always wrap in short closure --- src/Value.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/Value.php b/src/Value.php index d0728a5..1944964 100644 --- a/src/Value.php +++ b/src/Value.php @@ -17,9 +17,7 @@ public static function of(string $label, mixed $value): self { return new self( $label, - is_callable($value) - ? $value(...) - : fn () => $value, + fn () => $value, ); } From 735753d1ae987704e6f18200b6b7554cadfe7b78 Mon Sep 17 00:00:00 2001 From: Roj Vroemen Date: Wed, 11 Sep 2024 17:51:09 +0200 Subject: [PATCH 3/3] Fix test --- src/MatrixTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MatrixTest.php b/src/MatrixTest.php index bf405b4..9ad8d73 100644 --- a/src/MatrixTest.php +++ b/src/MatrixTest.php @@ -16,7 +16,7 @@ public function cases(): void 'php' => [8.3, 8.4], 'node' => [ Value::of('18', 18), - Value::of('20', fn () => 20), + Value::of('20', 20), ], 'postgres' => [ Value::of('v15', '15.0'),