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..9ad8d73 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', 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..1944964 --- /dev/null +++ b/src/Value.php @@ -0,0 +1,38 @@ + $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)(); + } +}