Skip to content

Commit

Permalink
Adding Statement::when conditionable
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Nov 25, 2024
1 parent 07cf24b commit 6828d35
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All Notable changes to `Csv` will be documented in this file
### Added

- `JsonConverter::withPrettyPrint` now accepts an optional `$identSize` parameter as its unique parameter.
- `Statement::when` to enable conditionable query building.

### Deprecated

Expand Down
21 changes: 20 additions & 1 deletion src/Statement.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,26 @@ public function limit(int $limit): self
return $clone;
}

/**
* Apply the callback if the given "condition" is (or resolves to) true.
*
* @param (callable($this): bool)|bool $condition
* @param callable($this): ($this|null) $callback
* @param ?callable($this): ($this|null) $default
*/
public function when(callable|bool $condition, callable $callback, ?callable $default = null): self
{
if (!is_bool($condition)) {
$condition = $condition($this);
}

return match (true) {
$condition => $callback($this),
null !== $default => $default($this),
default => $this,
} ?? $this;
}

/**
* Executes the prepared Statement on the {@link TabularDataReader} object.
*
Expand Down Expand Up @@ -437,7 +457,6 @@ protected function buildOrderBy(Iterator $iterator): Iterator
return $cmp ?? 0;
};


$class = new class () extends ArrayIterator {
public function seek(int $offset): void
{
Expand Down

0 comments on commit 6828d35

Please sign in to comment.