From 1a31de999a6f722e8272f667345e62e16abca97f Mon Sep 17 00:00:00 2001 From: ignace nyamagana butera Date: Sun, 17 Nov 2024 22:39:26 +0100 Subject: [PATCH] Adding missing support for callable in query methods and classes --- CHANGELOG.md | 1 + src/JsonConverterTest.php | 3 +-- src/Query/Constraint/Column.php | 6 +++++- src/Query/Constraint/Offset.php | 6 +++++- src/Query/Constraint/TwoColumns.php | 6 +++++- src/Query/Ordering/Column.php | 6 +++++- src/Statement.php | 24 ++++++++++++------------ 7 files changed, 34 insertions(+), 18 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f627f762..0e5b1cbb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All Notable changes to `Csv` will be documented in this file ### Fixed - `JsonConverter::withPrettyPrint` now accepts the `$identSize` as its unique parameter. +- Adding support for `callable` in the `Query\Constraint` namespace. ### Remove diff --git a/src/JsonConverterTest.php b/src/JsonConverterTest.php index 7a93901e..0f564406 100644 --- a/src/JsonConverterTest.php +++ b/src/JsonConverterTest.php @@ -94,8 +94,7 @@ public function it_fails_if_the_chunk_size_is_invalud(): void { $this->expectException(InvalidArgumentException::class); - JsonConverter::create()->chunkSize(0); - /* @phpstan-ignore-line */ + JsonConverter::create()->chunkSize(0); /* @phpstan-ignore-line */ } #[Test] diff --git a/src/Query/Constraint/Column.php b/src/Query/Constraint/Column.php index 8b9456a9..4f4b713f 100644 --- a/src/Query/Constraint/Column.php +++ b/src/Query/Constraint/Column.php @@ -46,13 +46,17 @@ private function __construct( */ public static function filterOn( string|int $column, - Comparison|Closure|string $operator, + Comparison|Closure|callable|string $operator, mixed $value = null, ): self { if ($operator instanceof Closure) { return new self($column, $operator, null); } + if (is_callable($operator)) { + return new self($column, Closure::fromCallable($operator), $value); + } + return new self( $column, is_string($operator) ? Comparison::fromOperator($operator) : $operator, diff --git a/src/Query/Constraint/Offset.php b/src/Query/Constraint/Offset.php index 96c59ac4..ddbd6156 100644 --- a/src/Query/Constraint/Offset.php +++ b/src/Query/Constraint/Offset.php @@ -43,13 +43,17 @@ private function __construct( * @throws Query\QueryException */ public static function filterOn( - Comparison|Closure|string $operator, + Comparison|Closure|callable|string $operator, mixed $value = null, ): self { if ($operator instanceof Closure) { return new self($operator, null); } + if (is_callable($operator)) { + return new self(Closure::fromCallable($operator), $value); + } + return new self( is_string($operator) ? Comparison::fromOperator($operator) : $operator, $value diff --git a/src/Query/Constraint/TwoColumns.php b/src/Query/Constraint/TwoColumns.php index 1923a44a..b6e000f3 100644 --- a/src/Query/Constraint/TwoColumns.php +++ b/src/Query/Constraint/TwoColumns.php @@ -60,13 +60,17 @@ private function __construct( */ public static function filterOn( string|int $firstColumn, - Comparison|Closure|string $operator, + Comparison|Closure|callable|string $operator, array|string|int $secondColumn ): self { if (is_string($operator)) { $operator = Comparison::fromOperator($operator); } + if (is_callable($operator)) { + return new self($firstColumn, Closure::fromCallable($operator), $secondColumn); + } + return new self($firstColumn, $operator, $secondColumn); } diff --git a/src/Query/Ordering/Column.php b/src/Query/Ordering/Column.php index ab178c6c..10ecb72a 100644 --- a/src/Query/Ordering/Column.php +++ b/src/Query/Ordering/Column.php @@ -54,7 +54,7 @@ private function __construct( public static function sortOn( string|int $column, string|int $direction, - ?Closure $callback = null + Closure|callable|null $callback = null ): self { $operator = match (true) { @@ -68,6 +68,10 @@ 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, diff --git a/src/Statement.php b/src/Statement.php index c370af98..71a7f8ba 100644 --- a/src/Statement.php +++ b/src/Statement.php @@ -132,62 +132,62 @@ final protected static function wrapSingleArgumentCallable(callable $where): cal }; } - public function andWhere(string|int $column, Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function andWhere(string|int $column, Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('and', Query\Constraint\Column::filterOn($column, $operator, $value)); } - public function orWhere(string|int $column, Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function orWhere(string|int $column, Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('or', Query\Constraint\Column::filterOn($column, $operator, $value)); } - public function whereNot(string|int $column, Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function whereNot(string|int $column, Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('not', Query\Constraint\Column::filterOn($column, $operator, $value)); } - public function xorWhere(string|int $column, Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function xorWhere(string|int $column, Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('xor', Query\Constraint\Column::filterOn($column, $operator, $value)); } - public function andWhereColumn(string|int $first, Query\Constraint\Comparison|string $operator, array|int|string $second): self + public function andWhereColumn(string|int $first, Query\Constraint\Comparison|callable|string $operator, array|int|string $second): self { return $this->appendWhere('and', Query\Constraint\TwoColumns::filterOn($first, $operator, $second)); } - public function orWhereColumn(string|int $first, Query\Constraint\Comparison|string $operator, array|int|string $second): self + public function orWhereColumn(string|int $first, Query\Constraint\Comparison|callable|string $operator, array|int|string $second): self { return $this->appendWhere('or', Query\Constraint\TwoColumns::filterOn($first, $operator, $second)); } - public function xorWhereColumn(string|int $first, Query\Constraint\Comparison|string $operator, array|int|string $second): self + public function xorWhereColumn(string|int $first, Query\Constraint\Comparison|callable|string $operator, array|int|string $second): self { return $this->appendWhere('xor', Query\Constraint\TwoColumns::filterOn($first, $operator, $second)); } - public function whereNotColumn(string|int $first, Query\Constraint\Comparison|string $operator, array|int|string $second): self + public function whereNotColumn(string|int $first, Query\Constraint\Comparison|callable|string $operator, array|int|string $second): self { return $this->appendWhere('not', Query\Constraint\TwoColumns::filterOn($first, $operator, $second)); } - public function andWhereOffset(Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function andWhereOffset(Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('and', Query\Constraint\Offset::filterOn($operator, $value)); } - public function orWhereOffset(Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function orWhereOffset(Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('or', Query\Constraint\Offset::filterOn($operator, $value)); } - public function xorWhereOffset(Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function xorWhereOffset(Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('xor', Query\Constraint\Offset::filterOn($operator, $value)); } - public function whereNotOffset(Query\Constraint\Comparison|Closure|string $operator, mixed $value = null): self + public function whereNotOffset(Query\Constraint\Comparison|Closure|callable|string $operator, mixed $value = null): self { return $this->appendWhere('not', Query\Constraint\Offset::filterOn($operator, $value)); }