Skip to content
This repository was archived by the owner on Nov 5, 2023. It is now read-only.
Merged
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
37 changes: 37 additions & 0 deletions bridge/doctrine-phpcr-odm/lib/ExpressionVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,27 @@ private function walkComparison(Comparison $comparison, AbstractNode $parentNode

case Comparison::GREATER_THAN_EQUAL:
return $parentNode->gte()->field($this->getField($field))->literal($value)->end();

case Comparison::IN:
return $this->getInConstraint($parentNode, $field, $value);

case Comparison::NOT_IN:
$node = $parentNode->not();
$this->getInConstraint($node, $field, $value);

return $node->end();

case Comparison::CONTAINS:
return $parentNode->like()->field($this->getField($field))->literal($value)->end();

case Comparison::NOT_CONTAINS:
return $parentNode->not()->like()->field($this->getField($field))->literal($value)->end()->end();

case Comparison::NULL:
return $parentNode->not()->fieldIsset($this->getField($field))->end();

case Comparison::NOT_NULL:
return $parentNode->fieldIsset($this->getField($field));
}

throw new \RuntimeException('Unknown comparator: ' . $comparison->getComparator());
Expand Down Expand Up @@ -113,4 +134,20 @@ private function getField($field): string
{
return $this->sourceAlias . '.' . $field;
}

/**
* @param AbstractNode $parentNode
* @param string $field
* @param array $values
*/
private function getInConstraint(AbstractNode $parentNode, $field, array $values)
{
$orNode = $parentNode->orx();

foreach ($values as $value) {
$orNode->eq()->field($this->getField($field))->literal($value);
}

$orNode->end();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
use Doctrine\ODM\PHPCR\Query\Builder\AbstractNode;
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintAndx;
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintComparison;
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintFieldIsset;
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintNot;
use Doctrine\ODM\PHPCR\Query\Builder\ConstraintOrx;
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\ExpressionVisitor;
use Psi\Bridge\ObjectAgent\Doctrine\PhpcrOdm\Tests\Functional\Model\Page;
Expand Down Expand Up @@ -64,6 +66,80 @@ public function provideComparator()
'lt',
'jcr.operator.less.than',
],
[
'contains',
'jcr.operator.like',
],
];
}

/**
* It should visit complex comparators.
*
* @dataProvider provideComplexComparator
*/
public function testComplexComparator(string $type, $value, \Closure $assertion)
{
$this->visitor->dispatch(Query::comparison($type, 'title', $value));
$query = $this->queryBuilder;
$children = $query->getChildrenOfType(AbstractNode::NT_WHERE);
$children = $children[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$assertion($children);
}

public function provideComplexComparator()
{
return [
[
'in',
[10, 20, 30],
function ($nodes) {
$this->assertInstanceOf(ConstraintOrx::class, $nodes[0]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$this->assertInstanceOf(ConstraintComparison::class, $nodes[0]);
$this->assertInstanceOf(ConstraintComparison::class, $nodes[1]);
$this->assertInstanceOf(ConstraintComparison::class, $nodes[2]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_OPERAND_STATIC);
$this->assertEquals(10, $nodes[0]->getValue());
},
],
[
'nin',
[10, 20, 30],
function ($nodes) {
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$this->assertInstanceOf(ConstraintOrx::class, $nodes[0]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$this->assertInstanceOf(ConstraintComparison::class, $nodes[1]);
$this->assertInstanceOf(ConstraintComparison::class, $nodes[2]);
},
],
[
'not_contains',
'hello',
function ($nodes) {
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$this->assertEquals('jcr.operator.like', $nodes[0]->getOperator());
},
],
[
'null',
null,
function ($nodes) {
$this->assertInstanceOf(ConstraintNot::class, $nodes[0]);
$nodes = $nodes[0]->getChildrenOfType(AbstractNode::NT_CONSTRAINT);
$this->assertInstanceOf(ConstraintFieldIsset::class, $nodes[0]);
},
],
[
'not_null',
null,
function ($nodes) {
$this->assertInstanceOf(ConstraintFieldIsset::class, $nodes[0]);
},
],
];
}

Expand Down
12 changes: 12 additions & 0 deletions lib/Query/Comparison.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ class Comparison implements Expression
const GREATER_THAN_EQUAL = 'gte';
const LESS_THAN = 'lt';
const LESS_THAN_EQUAL = 'lte';
const NULL = 'null';
const NOT_NULL = 'not_null';
const IN = 'in';
const NOT_IN = 'nin';
const CONTAINS = 'contains';
const NOT_CONTAINS = 'not_contains';

// TODO: Contains (like), NULL, NOT NULL, IN

Expand All @@ -22,6 +28,12 @@ class Comparison implements Expression
self::GREATER_THAN_EQUAL,
self::LESS_THAN,
self::LESS_THAN_EQUAL,
self::NULL,
self::NOT_NULL,
self::IN,
self::NOT_IN,
self::CONTAINS,
self::NOT_CONTAINS,
];

private $comparator;
Expand Down