createConvertValue($convertEmptyStringToNull, $rightTrimString); $convertNumeric = $this->createConvertRow($convertValue, null); $convertAssociative = $this->createConvertRow($convertValue, $case); $this->convertNumeric = $this->createConvert($convertNumeric, $id); $this->convertAssociative = $this->createConvert($convertAssociative, $id); $this->convertOne = $this->createConvert($convertValue, $id); $this->convertAllNumeric = $this->createConvertAll($convertNumeric, $id); $this->convertAllAssociative = $this->createConvertAll($convertAssociative, $id); $this->convertFirstColumn = $this->createConvertAll($convertValue, $id); } /** * @param array|false $row * * @return list|false */ public function convertNumeric($row) { return ($this->convertNumeric)($row); } /** * @param array|false $row * * @return array|false */ public function convertAssociative($row) { return ($this->convertAssociative)($row); } /** * @param mixed|false $value * * @return mixed|false */ public function convertOne($value) { return ($this->convertOne)($value); } /** * @param list> $data * * @return list> */ public function convertAllNumeric(array $data): array { return ($this->convertAllNumeric)($data); } /** * @param list> $data * * @return list> */ public function convertAllAssociative(array $data): array { return ($this->convertAllAssociative)($data); } /** * @param list $data * * @return list */ public function convertFirstColumn(array $data): array { return ($this->convertFirstColumn)($data); } /** * Creates a function that will convert each individual value retrieved from the database * * @param bool $convertEmptyStringToNull Whether each empty string should be converted to NULL * @param bool $rightTrimString Whether each string should right-trimmed * * @return callable|null The resulting function or NULL if no conversion is needed */ private function createConvertValue(bool $convertEmptyStringToNull, bool $rightTrimString): ?callable { $functions = []; if ($convertEmptyStringToNull) { $functions[] = static function ($value) { if ($value === '') { return null; } return $value; }; } if ($rightTrimString) { $functions[] = static function ($value) { if (! is_string($value)) { return $value; } return rtrim($value); }; } return $this->compose(...$functions); } /** * Creates a function that will convert each array-row retrieved from the database * * @param callable|null $function The function that will convert each value * @param int|null $case Column name case * * @return callable|null The resulting function or NULL if no conversion is needed */ private function createConvertRow(?callable $function, ?int $case): ?callable { $functions = []; if ($function !== null) { $functions[] = $this->createMapper($function); } if ($case !== null) { $functions[] = static function (array $row) use ($case): array { return array_change_key_case($row, $case); }; } return $this->compose(...$functions); } /** * Creates a function that will be applied to the return value of Statement::fetch*() * or an identity function if no conversion is needed * * @param callable|null $function The function that will convert each tow * @param callable $id Identity function */ private function createConvert(?callable $function, callable $id): callable { if ($function === null) { return $id; } return static function ($value) use ($function) { if ($value === false) { return false; } return $function($value); }; } /** * Creates a function that will be applied to the return value of Statement::fetchAll*() * or an identity function if no transformation is required * * @param callable|null $function The function that will transform each value * @param callable $id Identity function */ private function createConvertAll(?callable $function, callable $id): callable { if ($function === null) { return $id; } return $this->createMapper($function); } /** * Creates a function that maps each value of the array using the given function * * @param callable $function The function that maps each value of the array */ private function createMapper(callable $function): callable { return static function (array $array) use ($function): array { return array_map($function, $array); }; } /** * Creates a composition of the given set of functions * * @param callable ...$functions The functions to compose * * @return callable|null The composition or NULL if an empty set is provided */ private function compose(callable ...$functions): ?callable { return array_reduce($functions, static function (?callable $carry, callable $item): callable { if ($carry === null) { return $item; } return static function ($value) use ($carry, $item) { return $item($carry($value)); }; }); } }