Skip to content

Commit

Permalink
Improve formatter implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 10, 2024
1 parent 114a2b2 commit fadc2e1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 19 deletions.
7 changes: 4 additions & 3 deletions src/HTMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Csv;

use Closure;
use Deprecated;
use DOMDocument;
use DOMElement;
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down
6 changes: 3 additions & 3 deletions src/JsonConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion src/Query/Constraint/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
16 changes: 7 additions & 9 deletions src/Query/Ordering/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
7 changes: 4 additions & 3 deletions src/XMLConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

namespace League\Csv;

use Closure;
use DOMAttr;
use DOMDocument;
use DOMElement;
Expand All @@ -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
{
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit fadc2e1

Please sign in to comment.