diff --git a/README.md b/README.md index 62eaf0cc1c3ec6f791a2bd7918d54b7b52c10966..9458752e68445106e0f76a1c7478a1faa181eddf 100644 --- a/README.md +++ b/README.md @@ -123,6 +123,15 @@ $rows = $table->findBy([ ]); ``` +Also, the `is()` method defaults to equality check, so you can omit the `equalTo()` and pass the value directly: + +```php +$rows = $table->findBy([ + $table->published()->is(true), + $table->createdAt()->is(lesserThanOrEqualTo(Instant::now())), +]); +``` + This package provides a `Composite` condition that lets you compose the most complex trees of boolean logic together, and a set of most common conditions such as equality, comparison, and null-checks. For a complete list, look into the [`Conditions/functions.php`](../src/Conditions/functions.php) file. In addition to these, you can also write your own conditions by implementing the `Condition` interface. It defines the sole method `toSql()` which is expected to return an array compatible with [Dibi](https://github.com/dg/dibi). diff --git a/src/ExpressionWithShorthands.php b/src/ExpressionWithShorthands.php index 7a691fc789197b3b4570a5831633e48f4db731ad..42e17a2206bbda5eae85c1cd49fa5c75615cd5f9 100644 --- a/src/ExpressionWithShorthands.php +++ b/src/ExpressionWithShorthands.php @@ -5,6 +5,8 @@ declare(strict_types=1); namespace Grifart\Tables; use Grifart\Tables\Conditions\Condition; +use Grifart\Tables\Conditions\IsEqualTo; +use Grifart\Tables\Conditions\IsNull; use Grifart\Tables\OrderBy\OrderBy; use Grifart\Tables\OrderBy\OrderByDirection; @@ -15,11 +17,19 @@ use Grifart\Tables\OrderBy\OrderByDirection; abstract class ExpressionWithShorthands implements Expression { /** - * @param \Closure(Expression<ValueType>): Condition $conditionFactory + * @param (\Closure(Expression<ValueType>): Condition)|ValueType|null $conditionFactory */ - public function is(\Closure $conditionFactory): Condition + public function is(mixed $conditionFactory): Condition { - return $conditionFactory($this); + if ($conditionFactory instanceof \Closure) { + return $conditionFactory($this); + } + + if ($conditionFactory === null) { + return new IsNull($this); + } + + return new IsEqualTo($this, $conditionFactory); } public function ascending(): OrderBy diff --git a/tests/TableTest.php b/tests/TableTest.php index 8ed8115b857ce1c50812890ee566b1b09afc8c5f..91b9406c6cb0c6fc8fc485d6a158b2fbde3a994b 100644 --- a/tests/TableTest.php +++ b/tests/TableTest.php @@ -89,7 +89,11 @@ $filteredByAnotherExpression = $table->findBy($startsWith($table->details(), ' Assert::count(1, $filteredByAnotherExpression); Assert::same(-5, $filteredByAnotherExpression[0]->getScore()); -$unique = $table->getBy($table->score()->is(equalTo(42))); +$nullDetails = $table->findBy($table->details()->is(null)); +Assert::count(1, $nullDetails); +Assert::same(0, $nullDetails[0]->getScore()); + +$unique = $table->getBy($table->score()->is(42)); Assert::same(42, $unique->getScore()); $zero = $table->get(TestPrimaryKey::from(new Uuid('2bec3f23-a210-455c-b907-bb69a99d07b2')));