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 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
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,8 @@
"branch-alias": {
"dev-master": "1.6.x-dev"
}
},
"suggest": {
"ext-mbstring": "Only required when ICONTAINS comparison is used"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,11 @@ public function walkComparison(Comparison $comparison)
return strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value) !== false;
};

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
27 changes: 14 additions & 13 deletions lib/Doctrine/Common/Collections/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,20 @@
*/
class Comparison implements Expression
{
public const EQ = '=';
public const NEQ = '<>';
public const LT = '<';
public const LTE = '<=';
public const GT = '>';
public const GTE = '>=';
public const IS = '='; // no difference with EQ
public const IN = 'IN';
public const NIN = 'NIN';
public const CONTAINS = 'CONTAINS';
public const MEMBER_OF = 'MEMBER_OF';
public const STARTS_WITH = 'STARTS_WITH';
public const ENDS_WITH = 'ENDS_WITH';
public const EQ = '=';
public const NEQ = '<>';
public const LT = '<';
public const LTE = '<=';
public const GT = '>';
public const GTE = '>=';
public const IS = '='; // no difference with EQ
public const IN = 'IN';
public const NIN = 'NIN';
public const CONTAINS = 'CONTAINS';
public const ICONTAINS = 'ICONTAINS';
public const MEMBER_OF = 'MEMBER_OF';
public const STARTS_WITH = 'STARTS_WITH';
public const ENDS_WITH = 'ENDS_WITH';

/**
* @var string
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 @@ -145,6 +145,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 @@ -124,6 +124,14 @@ public function testContains() : void
self::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