From 37fa200fa78d8fac2b31623612af468348131d86 Mon Sep 17 00:00:00 2001 From: "Daniel J. Summers" Date: Tue, 23 Jul 2024 19:19:21 -0400 Subject: [PATCH] Use functional style in Parameters, update doc --- src/Parameters.php | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) diff --git a/src/Parameters.php b/src/Parameters.php index 5cea9d9..23799e7 100644 --- a/src/Parameters.php +++ b/src/Parameters.php @@ -59,21 +59,21 @@ class Parameters /** * Fill in parameter names for any fields missing one * - * @param array|Field[] $fields The fields for the query - * @return array|Field[] The fields, all with non-blank parameter names + * @param Field[] $fields The fields for the query + * @return Field[] The fields, all with non-blank parameter names */ public static function nameFields(array $fields): array { - for ($idx = 0; $idx < sizeof($fields); $idx++) { - if (empty($fields[$idx]->paramName)) $fields[$idx]->paramName = ":field$idx"; - } + array_walk($fields, function (Field $field, int $idx) { + if (empty($field->paramName)) $field->paramName =":field$idx"; + }); return $fields; } /** * Add field parameters to the given set of parameters * - * @param array|Field[] $fields The fields being compared in the query + * @param Field[] $fields The fields being compared in the query * @param array $parameters An associative array of parameters to which the fields should be added * @return array An associative array of parameter names and values with the fields added */ @@ -86,20 +86,17 @@ class Parameters * Create JSON field name parameters for the given field names to the given parameter * * @param string $paramName The name of the parameter for the field names - * @param array|string[] $fieldNames The names of the fields for the parameter + * @param string[] $fieldNames The names of the fields for the parameter * @return array An associative array of parameter/value pairs for the field names * @throws Exception If the database mode has not been set */ public static function fieldNames(string $paramName, array $fieldNames): array { $mode = Configuration::mode('generate field name parameters'); - - if ($mode === Mode::PgSQL) return [$paramName => "{" . implode(",", $fieldNames) . "}"]; - - // else SQLite - $it = []; - $idx = 0; - foreach ($fieldNames as $field) $it[$paramName . $idx++] = "$.$field"; - return $it; + return match ($mode) { + Mode::PgSQL => [$paramName => "{" . implode(",", $fieldNames) . "}"], + Mode::SQLite => array_combine(array_map(fn($idx) => $paramName . $idx, range(0, sizeof($fieldNames) - 1)), + array_map(fn($field) => "$.$field", $fieldNames)) + }; } }