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

Comparison BIT_AND #238

Open
wants to merge 3 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
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@ public function walkComparison(Comparison $comparison)
return static function ($object) use ($field, $value) : bool {
return $value === substr(ClosureExpressionVisitor::getObjectFieldValue($object, $field), -strlen($value));
};
case Comparison::BIT_AND:
return static function ($object) use ($field, $value) : bool {
$fieldValue = ClosureExpressionVisitor::getObjectFieldValue($object, $field);

return ($fieldValue & $value) === $value;
};
default:
throw new RuntimeException('Unknown comparison operator: ' . $comparison->getOperator());
}
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 @@ -22,6 +22,7 @@ class Comparison implements Expression
public const MEMBER_OF = 'MEMBER_OF';
public const STARTS_WITH = 'STARTS_WITH';
public const ENDS_WITH = 'ENDS_WITH';
public const BIT_AND = 'BIT_AND';

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

/**
* @param mixed $value
*/
public function bitAnd(string $field, $value) : Comparison
{
return new Comparison($field, Comparison::BIT_AND, new Value($value));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,37 @@ public function testWalkEndsWithComparison() : void
self::assertFalse($closure(new TestObject('hello')));
}

public function testWalkBitAndComparison() : void
{
$closure = $this->visitor->walkComparison($this->builder->bitAnd('foo', 4));

self::assertTrue($closure(new TestObject(4)));
self::assertTrue($closure(new TestObject('4')));
self::assertTrue($closure(new TestObject(5)));
self::assertTrue($closure(new TestObject('5')));
self::assertTrue($closure(new TestObject(12)));
self::assertTrue($closure(new TestObject('12')));
self::assertFalse($closure(new TestObject(2)));
Copy link
Member

Choose a reason for hiding this comment

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

Please add a few more false cases like 8 or 11. Be sure that there are also false-cases with a string-number like in your true-cases.

self::assertFalse($closure(new TestObject('2')));
self::assertFalse($closure(new TestObject(8)));
self::assertFalse($closure(new TestObject('8')));
self::assertFalse($closure(new TestObject(11)));
self::assertFalse($closure(new TestObject('11')));

$closure = $this->visitor->walkComparison($this->builder->bitAnd('foo', 6));

self::assertTrue($closure(new TestObject(6)));
self::assertTrue($closure(new TestObject('6')));
self::assertTrue($closure(new TestObject(7)));
self::assertTrue($closure(new TestObject('7')));
self::assertFalse($closure(new TestObject(4)));
self::assertFalse($closure(new TestObject('4')));
self::assertFalse($closure(new TestObject(5)));
self::assertFalse($closure(new TestObject('5')));
self::assertFalse($closure(new TestObject(24)));
self::assertFalse($closure(new TestObject('24')));
}

public function testWalkAndCompositeExpression() : void
{
$closure = $this->visitor->walkCompositeExpression(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,4 +141,11 @@ public function testEndsWith() : void
self::assertInstanceOf(Comparison::class, $expr);
self::assertEquals(Comparison::ENDS_WITH, $expr->getOperator());
}

public function testBitAnd() : void
{
$expr = $this->builder->bitAnd('b', 4);

self::assertEquals(Comparison::BIT_AND, $expr->getOperator());
}
}