Skip to content

Commit

Permalink
Adding missing support for callable in query methods and classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 17, 2024
1 parent 6134525 commit 1a31de9
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/JsonConverterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
6 changes: 5 additions & 1 deletion src/Query/Constraint/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 5 additions & 1 deletion src/Query/Constraint/Offset.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion src/Query/Constraint/TwoColumns.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
6 changes: 5 additions & 1 deletion src/Query/Ordering/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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,
Expand Down
24 changes: 12 additions & 12 deletions src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down

0 comments on commit 1a31de9

Please sign in to comment.