From 9289bff6bf926fc75e751406530a7016fd28de86 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Fri, 8 Nov 2024 13:28:32 +0900 Subject: [PATCH 1/2] feat: consolidate schema options formatting --- CHANGELOG.md | 3 ++- src/Concerns/SharedGrammarCalls.php | 29 +++++++++++++++++++++++++++++ src/Schema/Grammar.php | 22 ++++------------------ 3 files changed, 35 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a19be08..f7d0049 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ -# v8.3.0 (2024-09-02) +# v8.3.0 (2024-11-08) +- consolidate schema options formatting (#241) - add support for invisible columns (#240) - add support for change streams using Blueprint (#230) - add support for snapshot queries (#215) diff --git a/src/Concerns/SharedGrammarCalls.php b/src/Concerns/SharedGrammarCalls.php index a22a585..2e38084 100644 --- a/src/Concerns/SharedGrammarCalls.php +++ b/src/Concerns/SharedGrammarCalls.php @@ -17,7 +17,10 @@ namespace Colopl\Spanner\Concerns; +use BackedEnum; use Illuminate\Database\Grammar; +use Illuminate\Support\Arr; +use Illuminate\Support\Str; trait SharedGrammarCalls { @@ -42,4 +45,30 @@ protected function wrapValue($value) return '`' . str_replace('`', '``', $value) . '`'; } + /** + * @param array $options + * @param string $delimiter + * @return string + */ + protected function formatOptions(array $options, string $delimiter = '='): string + { + $mapped = Arr::map($options, function (mixed $v, string $k) use ($delimiter): string { + return Str::snake($k) . $delimiter . $this->formatOptionValue($v); + }); + return implode(', ', $mapped); + } + + /** + * @param scalar|BackedEnum $value + * @return string + */ + protected function formatOptionValue(mixed $value): string + { + return match (true) { + is_bool($value) => $value ? 'true' : 'false', + is_string($value) => $this->quoteString($value), + $value instanceof BackedEnum => $this->formatOptionValue($value->value), + default => (string) $value, + }; + } } diff --git a/src/Schema/Grammar.php b/src/Schema/Grammar.php index df64643..247cf20 100644 --- a/src/Schema/Grammar.php +++ b/src/Schema/Grammar.php @@ -262,10 +262,7 @@ public function compileDropSequenceIfExists(Blueprint $blueprint, object $comman */ protected function formatSequenceOptions(mixed $definition): string { - $optionAsStrings = Arr::map($definition->getOptions(), function (mixed $v, string $k): string { - return Str::snake($k) . '=' . (is_string($v) ? $this->quoteString($v) : $v); - }); - return 'options (' . implode(', ', $optionAsStrings) . ')'; + return 'options (' . $this->formatOptions($definition->getOptions()) . ')'; } /** @@ -330,20 +327,9 @@ protected function formatChangeStreamOptions(ChangeStreamDefinition $definition) { $options = $definition->getOptions(); - if ($options === []) { - return ''; - } - - $optionAsStrings = Arr::map($options, function (mixed $v, string $k): string { - return Str::snake($k) . '=' . match (true) { - $v === true => 'true', - $v === false => 'false', - is_string($v) => $this->quoteString($v), - $v instanceof ChangeStreamValueCaptureType => $this->quoteString($v->value), - default => throw new LogicException('Unsupported option value: ' . $v), - }; - }); - return 'options (' . implode(', ', $optionAsStrings) . ')'; + return $options !== [] + ? 'options (' . $this->formatOptions($options) . ')' + : ''; } /** From bff2c79aa3199b1129eda4cb70a12380ab2c1af1 Mon Sep 17 00:00:00 2001 From: Takayasu Oyama Date: Fri, 8 Nov 2024 13:50:49 +0900 Subject: [PATCH 2/2] fix stan error --- src/Concerns/SharedGrammarCalls.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Concerns/SharedGrammarCalls.php b/src/Concerns/SharedGrammarCalls.php index 2e38084..83cbc1e 100644 --- a/src/Concerns/SharedGrammarCalls.php +++ b/src/Concerns/SharedGrammarCalls.php @@ -52,7 +52,7 @@ protected function wrapValue($value) */ protected function formatOptions(array $options, string $delimiter = '='): string { - $mapped = Arr::map($options, function (mixed $v, string $k) use ($delimiter): string { + $mapped = Arr::map($options, function (int|float|bool|string|BackedEnum $v, string $k) use ($delimiter): string { return Str::snake($k) . $delimiter . $this->formatOptionValue($v); }); return implode(', ', $mapped);