From fadc2e11495f7f300c6606e6075c25874406b899 Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Tue, 10 Dec 2024 17:15:51 +0100 Subject: [PATCH] Improve formatter implementation --- src/HTMLConverter.php | 7 ++++--- src/JsonConverter.php | 6 +++--- src/Query/Constraint/Column.php | 2 +- src/Query/Ordering/Column.php | 16 +++++++--------- src/XMLConverter.php | 7 ++++--- 5 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/HTMLConverter.php b/src/HTMLConverter.php index 95fa1901..fcf9f657 100644 --- a/src/HTMLConverter.php +++ b/src/HTMLConverter.php @@ -13,6 +13,7 @@ namespace League\Csv; +use Closure; use Deprecated; use DOMDocument; use DOMElement; @@ -30,8 +31,8 @@ class HTMLConverter /** table id attribute value. */ protected string $id_value = ''; protected XMLConverter $xml_converter; - /** @var ?callable(array, array-key): array */ - protected mixed $formatter = null; + /** @var ?Closure(array, array-key): array */ + protected ?Closure $formatter = null; public static function create(): self { @@ -174,7 +175,7 @@ public function td(string $fieldname_attribute_name): self public function formatter(?callable $formatter): self { $clone = clone $this; - $clone->formatter = $formatter; + $clone->formatter = ($formatter instanceof Closure || null === $formatter) ? $formatter : $formatter(...); return $clone; } diff --git a/src/JsonConverter.php b/src/JsonConverter.php index 17bdadcb..53a6b6bf 100644 --- a/src/JsonConverter.php +++ b/src/JsonConverter.php @@ -104,8 +104,8 @@ final class JsonConverter public readonly int $depth; /** @var int<1, max> */ public readonly int $indentSize; - /** @var ?callable(T, array-key): mixed */ - public readonly mixed $formatter; + /** @var ?Closure(T, array-key): mixed */ + public readonly ?Closure $formatter; /** @var int<1, max> */ public readonly int $chunkSize; /** @var non-empty-string */ @@ -151,7 +151,7 @@ private function __construct(int $flags, int $depth, int $indentSize, ?callable $this->flags = $flags; $this->depth = $depth; $this->indentSize = $indentSize; - $this->formatter = $formatter; + $this->formatter = ($formatter instanceof Closure || null === $formatter) ? $formatter : $formatter(...); $this->chunkSize = $chunkSize; // Initialize settings and closure to use for conversion. diff --git a/src/Query/Constraint/Column.php b/src/Query/Constraint/Column.php index 4f4b713f..81ad21c5 100644 --- a/src/Query/Constraint/Column.php +++ b/src/Query/Constraint/Column.php @@ -54,7 +54,7 @@ public static function filterOn( } if (is_callable($operator)) { - return new self($column, Closure::fromCallable($operator), $value); + return new self($column, $operator(...), $value); } return new self( diff --git a/src/Query/Ordering/Column.php b/src/Query/Ordering/Column.php index 10ecb72a..aafe746d 100644 --- a/src/Query/Ordering/Column.php +++ b/src/Query/Ordering/Column.php @@ -68,15 +68,13 @@ public static function sortOn( default => throw new QueryException('Unknown or unsupported ordering operator value: '.$direction), }; - if (is_callable($callback)) { - $callback = Closure::fromCallable($callback); - } - - return new self( - $operator, - $column, - $callback ?? static fn (mixed $first, mixed $second): int => $first <=> $second - ); + $callback = match (true) { + null === $callback => static fn (mixed $first, mixed $second): int => $first <=> $second, + $callback instanceof Closure => $callback, + default => $callback(...), + }; + + return new self($operator, $column, $callback); } /** diff --git a/src/XMLConverter.php b/src/XMLConverter.php index be6146bd..2f69fcb7 100644 --- a/src/XMLConverter.php +++ b/src/XMLConverter.php @@ -13,6 +13,7 @@ namespace League\Csv; +use Closure; use DOMAttr; use DOMDocument; use DOMElement; @@ -34,8 +35,8 @@ class XMLConverter protected string $column_attr = ''; /** XML offset attribute name. */ protected string $offset_attr = ''; - /** @var ?callable(array, array-key): array */ - public mixed $formatter = null; + /** @var ?Closure(array, array-key): array */ + public ?Closure $formatter = null; public static function create(): self { @@ -161,7 +162,7 @@ public function rootElement(string $node_name): self public function formatter(?callable $formatter): self { $clone = clone $this; - $clone->formatter = $formatter; + $clone->formatter = ($formatter instanceof Closure || null === $formatter) ? $formatter : $formatter(...); return $clone; }