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

Case insensitive like #123

Open
wants to merge 5 commits into
base: 2.0.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,11 @@ public function walkComparison(Comparison $comparison)
return false !== strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::ICONTAINS:
return function ($object) use ($field, $value) {
return false !== strpos(mb_strtolower(ClosureExpressionVisitor::getObjectFieldValue($object, $field)), mb_strtolower($value));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, although it's correct, using mb_strtolower introduces dependency on ext-mbstring, which is not bundled in PHP. It's not hard dependency (required only when ICONTAINS is used), but it should probably be added to suggest section in the composer.json.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Majkl578 Done (PR updated)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not using mb_stripos()?

};

case Comparison::MEMBER_OF:
return function ($object) use ($field, $value) : bool {
$fieldValues = ClosureExpressionVisitor::getObjectFieldValue($object, $field);
Expand Down
1 change: 1 addition & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class Comparison implements Expression
const IN = 'IN';
const NIN = 'NIN';
const CONTAINS = 'CONTAINS';
const ICONTAINS = 'ICONTAINS';
const MEMBER_OF = 'MEMBER_OF';
const STARTS_WITH = 'STARTS_WITH';
const ENDS_WITH = 'ENDS_WITH';
Expand Down
11 changes: 11 additions & 0 deletions lib/Doctrine/Common/Collections/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,17 @@ public function contains($field, $value)
return new Comparison($field, Comparison::CONTAINS, new Value($value));
}

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

/**
* @param string $field
* @param mixed $value
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,14 @@ public function testContains() : void
$this->assertEquals(Comparison::CONTAINS, $expr->getOperator());
}

public function testIContains() : void
{
$expr = $this->builder->iContains("a", "b");

$this->assertInstanceOf(Comparison::class, $expr);
$this->assertEquals(Comparison::ICONTAINS, $expr->getOperator());
}

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