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

Expressions over Embeddables #28

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

case Comparison::SIM:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) == $value;
};

case Comparison::NSIM:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) != $value;
};

case Comparison::LT:
return function ($object) use ($field, $value) {
return ClosureExpressionVisitor::getObjectFieldValue($object, $field) < $value;
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 @@ -34,6 +34,8 @@ class Comparison implements Expression
const GT = '>';
const GTE = '>=';
const IS = '='; // no difference with EQ
const SIM = '~'; // similarity: loose comparison;
Copy link
Member

Choose a reason for hiding this comment

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

Wondering if we can actually re-use CONTAINS here instead. I wouldn't introduce more operators than necessary.

Copy link
Author

Choose a reason for hiding this comment

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

How would the semantics of CONTAINS apply to comparing Value Objects?
Using CONTAINS for objects implies that you refer to internal properties, but that is something that should be avoided.
We want an operator that can say new Street("High Street 1") == new Street("High Street 1"). That is something else than, for instance, new Street("High Street 1") contains "High Street 1" if that's the behaviour you're thinking of.

const NSIM = '!~';
const IN = 'IN';
const NIN = 'NIN';
const CONTAINS = 'CONTAINS';
Expand Down
24 changes: 24 additions & 0 deletions lib/Doctrine/Common/Collections/ExpressionBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,30 @@ public function isNull($field)
return new Comparison($field, Comparison::EQ, new Value(null));
}

/**
* Defines similarity between objects
* @param string $field
* @param mixed $value
*
* @return Comparison
*/
public function sim($field, $value)
{
return new Comparison($field, Comparison::SIM, new Value($value));
}

/**
* Defines dissimilarity between objects
* @param string $field
* @param mixed $value
*
* @return Comparison
*/
public function nsim($field, $value)
{
return new Comparison($field, Comparison::NSIM, new Value($value));
}

/**
* @param string $field
* @param mixed $values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,54 @@ public function testWalkNotEqualsComparison()
$this->assertTrue($closure(new TestObject(2)));
}

/**
* eq() performs a strict comparison, hence objects are equal iff their references are equal.
* It follows that eq() cannot be used for Value Objects.
*/
public function testWalkEqualsComparisonForObjects()
{
$valueObject = new TestValueObject("foo");
$closure = $this->visitor->walkComparison($this->builder->eq("valueObject", $valueObject));

$this->assertTrue($closure(new TestEntity($valueObject)));
$this->assertFalse($closure(new TestEntity(new TestValueObject("foo"))));
}

/**
* neq() performs a strict comparison, hence objects are not equal iff their references are not equal.
* It follows that neq() cannot be used for Value Objects.
*/
public function testWalkNotEqualsComparisonForObjects()
{
$valueObject = new TestValueObject("foo");
$closure = $this->visitor->walkComparison($this->builder->neq("valueObject", $valueObject));

$this->assertFalse($closure(new TestEntity($valueObject)));
$this->assertTrue($closure(new TestEntity(new TestValueObject("foo"))));
}

/**
* sim() performs a loose comparison and can be used for Value Objects.
*/
public function testWalkSimilarToComparisonForValueObjects()
{
$closure = $this->visitor->walkComparison($this->builder->sim("valueObject", new TestValueObject("foo")));

$this->assertTrue($closure(new TestEntity(new TestValueObject("foo"))));
$this->assertFalse($closure(new TestEntity(new TestValueObject("bar"))));
}

/**
* nsim() performs a loose comparison and can be used for Value Objects.
*/
public function testWalkNotSimilarToComparisonForValueObjects()
{
$closure = $this->visitor->walkComparison($this->builder->nsim("valueObject", new TestValueObject("foo")));

$this->assertFalse($closure(new TestEntity(new TestValueObject("foo"))));
$this->assertTrue($closure(new TestEntity(new TestValueObject("bar"))));
}

public function testWalkLessThanComparison()
{
$closure = $this->visitor->walkComparison($this->builder->lt("foo", 1));
Expand Down Expand Up @@ -241,3 +289,27 @@ public function isBaz()
}
}

class TestEntity
{
private $valueObject;

function __construct(TestValueObject $valueObject)
{
$this->valueObject = $valueObject;
}

public function getValueObject()
{
return $this->valueObject;
}
}

class TestValueObject
{
private $value;

function __construct($value)
{
$this->value = $value;
}
}