Use functional style in Parameters, update doc

This commit is contained in:
Daniel J. Summers 2024-07-23 19:19:21 -04:00
parent adbe4e6614
commit 37fa200fa7

View File

@ -59,21 +59,21 @@ class Parameters
/** /**
* Fill in parameter names for any fields missing one * Fill in parameter names for any fields missing one
* *
* @param array|Field[] $fields The fields for the query * @param Field[] $fields The fields for the query
* @return array|Field[] The fields, all with non-blank parameter names * @return Field[] The fields, all with non-blank parameter names
*/ */
public static function nameFields(array $fields): array public static function nameFields(array $fields): array
{ {
for ($idx = 0; $idx < sizeof($fields); $idx++) { array_walk($fields, function (Field $field, int $idx) {
if (empty($fields[$idx]->paramName)) $fields[$idx]->paramName = ":field$idx"; if (empty($field->paramName)) $field->paramName =":field$idx";
} });
return $fields; return $fields;
} }
/** /**
* Add field parameters to the given set of parameters * 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 * @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 * @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 * 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 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 * @return array An associative array of parameter/value pairs for the field names
* @throws Exception If the database mode has not been set * @throws Exception If the database mode has not been set
*/ */
public static function fieldNames(string $paramName, array $fieldNames): array public static function fieldNames(string $paramName, array $fieldNames): array
{ {
$mode = Configuration::mode('generate field name parameters'); $mode = Configuration::mode('generate field name parameters');
return match ($mode) {
if ($mode === Mode::PgSQL) return [$paramName => "{" . implode(",", $fieldNames) . "}"]; Mode::PgSQL => [$paramName => "{" . implode(",", $fieldNames) . "}"],
Mode::SQLite => array_combine(array_map(fn($idx) => $paramName . $idx, range(0, sizeof($fieldNames) - 1)),
// else SQLite array_map(fn($field) => "$.$field", $fieldNames))
$it = []; };
$idx = 0;
foreach ($fieldNames as $field) $it[$paramName . $idx++] = "$.$field";
return $it;
} }
} }