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

Like or Not Like - that's where the question #50

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 4 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
36 changes: 36 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/ClosureExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,42 @@ public function walkComparison(Comparison $comparison)
return false !== strpos(ClosureExpressionVisitor::getObjectFieldValue($object, $field), $value);
};

case Comparison::LIKE:
case Comparison::NOTLIKE:
$like = $comparison->getOperator() === Comparison::LIKE;
$pattern = null;

// Replace the escaped characters to placeholder
$tmpValue = str_replace('\%', 'SQLWILDCARDESCAPEDMANY', $value);
$tmpValue = str_replace('\_', 'SQLWILDCARDESCAPEDONE', $tmpValue);

// Check whether we have a wildcard characters, and build the regular expression
if(strpos($tmpValue, '%') !== false || strpos($tmpValue, '_') !== false) {
// Build regexp
$pattern = preg_quote($tmpValue, '/');
$pattern = str_replace('%', '.*', $pattern);
$pattern = str_replace('_', '.{1}', $pattern);
$pattern = str_replace('SQLWILDCARDESCAPEDMANY', '\\%', $pattern);
$pattern = str_replace('SQLWILDCARDESCAPEDONE', '\\_', $pattern);
$pattern = '/^' . $pattern . '$/m';
}
else {
// Replace the escaped characters to normal one
$value = str_replace('\%', '%', $value);
$value = str_replace('\_', '_', $value);
}

return function ($object) use ($field, $value, $like, $pattern) {
$fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field);

if($pattern) {
return (bool) ($like ? preg_match($pattern, $fieldValue) : !preg_match($pattern, $fieldValue));
}
Copy link
Member

Choose a reason for hiding this comment

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

instead of checking whether a preg_match or an equality should be used each time the function is called, you could check it outside, returning different functions

Copy link
Author

Choose a reason for hiding this comment

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

yes, good idea


// The wildcard characters not exist, use regular comparison
return ($like ? $fieldValue === $value : $fieldValue !== $value);
};

default:
throw new \RuntimeException("Unknown comparison operator: " . $comparison->getOperator());
}
Expand Down
2 changes: 2 additions & 0 deletions lib/Doctrine/Common/Collections/Expr/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ class Comparison implements Expression
const IN = 'IN';
const NIN = 'NIN';
const CONTAINS = 'CONTAINS';
const LIKE = 'LIKE';
const NOTLIKE = 'NOTLIKE';

/**
* @var string
Expand Down
22 changes: 22 additions & 0 deletions lib/Doctrine/Common/Collections/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,4 +163,26 @@ public function contains($field, $value)
{
return new Comparison($field, Comparison::CONTAINS, new Value($value));
}

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

/**
* @param string $field
* @param mixed $value
*
* @return Comparison
*/
public function notLike($field, $value)
{
return new Comparison($field, Comparison::NOTLIKE, new Value($value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,22 @@ public function testWalkContainsComparison()
$this->assertFalse($closure(new TestObject('world')));
}

public function testWalkLikeComparison()
{
$closure = $this->visitor->walkComparison($this->builder->like('foo', 'H_llo! I%Robot!'));

$this->assertTrue($closure(new TestObject('Hello! I am Robot!')));
$this->assertFalse($closure(new TestObject('Hello! I am Robot?')));
}

public function testWalkNotLikeComparison()
{
$closure = $this->visitor->walkComparison($this->builder->notLike('foo', '%I a_ Robot?'));

$this->assertTrue($closure(new TestObject('Hello! I am Robot!')));
$this->assertFalse($closure(new TestObject('Hello! I am Robot?')));
}

public function testWalkAndCompositeExpression()
{
$closure = $this->visitor->walkCompositeExpression(
Expand Down
17 changes: 17 additions & 0 deletions tests/Doctrine/Tests/Common/Collections/ExpressionBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,21 @@ public function testContains()
$this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr);
$this->assertEquals(Comparison::CONTAINS, $expr->getOperator());
}

public function testLike()
{
$expr = $this->builder->like("a", "b");

$this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr);
$this->assertEquals(Comparison::LIKE, $expr->getOperator());
}

public function testNotLike()
{
$expr = $this->builder->notLike("a", "b");

$this->assertInstanceOf('Doctrine\Common\Collections\Expr\Comparison', $expr);
$this->assertEquals(Comparison::NOTLIKE, $expr->getOperator());
}

}