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

Comparison BIT_AND #238

wants to merge 3 commits into from

Conversation

najved
Copy link

@najved najved commented Jun 11, 2020

I was missing a "bit and" comparison for building criteria expression on bit mask fields.

e.g bit mask:

1: imported
2: canceled
4: deleted
8: ...

foo = 5 (1+4)

$expressionBuilder->bitAnd('foo', 1) // true
$expressionBuilder->bitAnd('foo', 5) // true
$expressionBuilder->bitAnd('foo', 2) // false

Implementation and tests included.

Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

Thank you for your PR. Can you please also elaborate which use-case you ran into that made you create this PR?

{
$expr = $this->builder->bitAnd('b', 4);

self::assertInstanceOf(Comparison::class, $expr);
Copy link
Member

Choose a reason for hiding this comment

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

The instance check shouldn't be needed in the test. It is a relict for past PHP versions when a return type couldn't be used.

self::assertTrue($closure(new TestObject('4')));
self::assertTrue($closure(new TestObject(5)));
self::assertTrue($closure(new TestObject('5')));
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.

@najved
Copy link
Author

najved commented Jun 17, 2020

Thank you for your PR. Can you please also elaborate which use-case you ran into that made you create this PR?

I have a collection of objects with a status field, which can hold different "combined values".

e.g.

class Item {
  const IS_EXPORTED = 1;
  const IS_PAYMENT = 2;
  const IS_CANCELED = 4;

  $status = 3;
  $statusAsArray = [1, 2];
}

$expressionBuilder->bitAnd('status', Item::IS_PAYMENT + Item::IS_CANCELED);
$expressionBuilder->in('statusAsArray', [Item::IS_PAYMENT, Item::IS_CANCELED]) ;

"bitAnd" would be similar to "in" and "bitNotAnd" to "notIn".

While writing these lines, i'm not sure if this is just an "edge"-use-case in my legacy code or a good addition for this project :-)

Copy link
Member

@SenseException SenseException left a comment

Choose a reason for hiding this comment

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

I don't know about the edge case, but I know that some doctrine/collections classes have a lot of methods which is why I don't know if it makes sense to introduce new ones or if the future of collections will need a different architecture. This needs more reviewers. @doctrine/doctrinecore

@morozov
Copy link
Member

morozov commented Jun 21, 2020

Instead of performing bitwise operations on a collection, why not have all bits as separate fields? This way, applied to a collection in the DB, the filter could use an index.

Base automatically changed from master to 2.0.x January 19, 2021 07:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants