Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 'not contains' operator to Comparison #365

Open
wants to merge 3 commits into
base: 2.1.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/en/expression-builder.rst
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,16 @@ contains

$collection->matching(new Criteria($expression));

notContains
--------

.. code-block:: php
$expressionBuilder = Criteria::expr();

$expression = $expressionBuilder->notContains('foo', 'value1');

$collection->matching(new Criteria($expression));

memberOf
--------

Expand Down
1 change: 1 addition & 0 deletions docs/en/expressions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ following operator constants:
- ``Comparison::IN``
- ``Comparison::NIN``
- ``Comparison::CONTAINS``
- ``Comparison::NCONTAINS``
- ``Comparison::MEMBER_OF``
- ``Comparison::STARTS_WITH``
- ``Comparison::ENDS_WITH``
Expand Down
1 change: 1 addition & 0 deletions src/Expr/ClosureExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ public function walkComparison(Comparison $comparison)
return ! in_array($fieldValue, $value, is_scalar($fieldValue));
},
Comparison::CONTAINS => static fn ($object): bool => str_contains((string) self::getObjectFieldValue($object, $field), (string) $value),
Comparison::NCONTAINS => static fn ($object): bool => ! str_contains((string) self::getObjectFieldValue($object, $field), (string) $value),
Comparison::MEMBER_OF => static function ($object) use ($field, $value): bool {
$fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field);

Expand Down
1 change: 1 addition & 0 deletions src/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Comparison implements Expression
final public const IN = 'IN';
final public const NIN = 'NIN';
final public const CONTAINS = 'CONTAINS';
final public const NCONTAINS = 'NCONTAINS';
final public const MEMBER_OF = 'MEMBER_OF';
final public const STARTS_WITH = 'STARTS_WITH';
final public const ENDS_WITH = 'ENDS_WITH';
Expand Down
6 changes: 6 additions & 0 deletions src/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,12 @@ public function contains(string $field, mixed $value)
return new Comparison($field, Comparison::CONTAINS, new Value($value));
}

/** @return Comparison */
public function notContains(string $field, mixed $value)
{
return new Comparison($field, Comparison::NCONTAINS, new Value($value));
}

/** @return Comparison */
public function memberOf(string $field, mixed $value)
{
Expand Down
8 changes: 8 additions & 0 deletions tests/Common/Collections/ExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,14 @@ public function testContains(): void
self::assertEquals(Comparison::CONTAINS, $expr->getOperator());
}

public function testNotContains(): void
{
$expr = $this->builder->notContains('a', 'b');

self::assertInstanceOf(Comparison::class, $expr);
self::assertEquals(Comparison::NCONTAINS, $expr->getOperator());
}

public function testMemberOf(): void
{
$expr = $this->builder->memberOf('b', ['a']);
Expand Down