Skip to content

Commit 4aa5ab2

Browse files
Add 'IN' operator (#1)
1 parent 879ba36 commit 4aa5ab2

File tree

6 files changed

+149
-1
lines changed

6 files changed

+149
-1
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "wikibase-solutions/php-cypher-dsl",
33
"description": "A PHP DSL for the Cypher Query Language",
4-
"version": "2.4.0",
4+
"version": "2.5.0",
55
"type": "library",
66
"keywords": [
77
"neo4j",

src/In.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?php
2+
3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
22+
namespace WikibaseSolutions\CypherDSL;
23+
24+
use WikibaseSolutions\CypherDSL\Traits\BooleanTypeTrait;
25+
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
26+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
27+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
28+
29+
/**
30+
* Represents the application of the "IN" operator.
31+
*
32+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/where/#where-in-operator
33+
*/
34+
class In extends BinaryOperator implements BooleanType
35+
{
36+
use BooleanTypeTrait;
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function __construct(PropertyType $left, ListType $right)
42+
{
43+
parent::__construct($left, $right);
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
protected function getOperator(): string
50+
{
51+
return "IN";
52+
}
53+
}

src/Traits/PropertyTypeTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@
2222
namespace WikibaseSolutions\CypherDSL\Traits;
2323

2424
use WikibaseSolutions\CypherDSL\Equality;
25+
use WikibaseSolutions\CypherDSL\In;
2526
use WikibaseSolutions\CypherDSL\Inequality;
27+
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
2628
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
2729

2830
/**
@@ -54,4 +56,15 @@ public function notEquals(PropertyType $right): Inequality
5456
{
5557
return new Inequality($this, $right);
5658
}
59+
60+
/**
61+
* Checks whether the element exists in the given list.
62+
*
63+
* @param ListType $right
64+
* @return In
65+
*/
66+
public function in(ListType $right): In
67+
{
68+
return new In($this, $right);
69+
}
5770
}

src/Types/PropertyTypes/PropertyType.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
namespace WikibaseSolutions\CypherDSL\Types\PropertyTypes;
44

55
use WikibaseSolutions\CypherDSL\Equality;
6+
use WikibaseSolutions\CypherDSL\In;
67
use WikibaseSolutions\CypherDSL\Inequality;
78
use WikibaseSolutions\CypherDSL\Types\AnyType;
9+
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
810

911
/**
1012
* Represents any property type in Cypher.
@@ -47,4 +49,12 @@ public function equals(PropertyType $right): Equality;
4749
* @return Inequality
4850
*/
4951
public function notEquals(PropertyType $right): Inequality;
52+
53+
/**
54+
* Checks whether the element exists in the given list.
55+
*
56+
* @param ListType $right
57+
* @return In
58+
*/
59+
public function in(ListType $right): In;
5060
}

tests/Unit/InTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
3+
/*
4+
* Cypher DSL
5+
* Copyright (C) 2021 Wikibase Solutions
6+
*
7+
* This program is free software; you can redistribute it and/or
8+
* modify it under the terms of the GNU General Public License
9+
* as published by the Free Software Foundation; either version 2
10+
* of the License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU General Public License
18+
* along with this program; if not, write to the Free Software
19+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20+
*/
21+
22+
namespace WikibaseSolutions\CypherDSL\Tests\Unit;
23+
24+
use PHPUnit\Framework\TestCase;
25+
use TypeError;
26+
use WikibaseSolutions\CypherDSL\In;
27+
use WikibaseSolutions\CypherDSL\Types\AnyType;
28+
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
29+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
30+
31+
/**
32+
* @covers \WikibaseSolutions\CypherDSL\In
33+
*/
34+
class InTest extends TestCase
35+
{
36+
use TestHelper;
37+
38+
public function testToQuery()
39+
{
40+
$inequality = new In($this->getQueryConvertableMock(PropertyType::class, "a"), $this->getQueryConvertableMock(ListType::class, "b"));
41+
42+
$this->assertSame("(a IN b)", $inequality->toQuery());
43+
44+
$inequality = new In($inequality, $this->getQueryConvertableMock(ListType::class, "c"));
45+
46+
$this->assertSame("((a IN b) IN c)", $inequality->toQuery());
47+
}
48+
49+
public function testDoesNotAcceptAnyTypeAsOperands()
50+
{
51+
$this->expectException(TypeError::class);
52+
53+
$inequality = new In($this->getQueryConvertableMock(AnyType::class, "a"), $this->getQueryConvertableMock(AnyType::class, "b"));
54+
55+
$inequality->toQuery();
56+
}
57+
}

tests/Unit/Traits/PropertyTypeTraitTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@
2424
use PHPUnit\Framework\MockObject\MockObject;
2525
use PHPUnit\Framework\TestCase;
2626
use WikibaseSolutions\CypherDSL\Equality;
27+
use WikibaseSolutions\CypherDSL\In;
2728
use WikibaseSolutions\CypherDSL\Inequality;
2829
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
30+
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
2931
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
3032

3133
/**
@@ -45,10 +47,16 @@ class PropertyTypeTraitTest extends TestCase
4547
*/
4648
private $b;
4749

50+
/**
51+
* @var MockObject|ListType
52+
*/
53+
private $list;
54+
4855
public function setUp(): void
4956
{
5057
$this->a = $this->getQueryConvertableMock(PropertyType::class, "10");
5158
$this->b = $this->getQueryConvertableMock(PropertyType::class, "15");
59+
$this->list = $this->getQueryConvertableMock(ListType::class, "['foobar']");
5260
}
5361

5462
public function testEquals()
@@ -64,4 +72,11 @@ public function testNotEquals()
6472

6573
$this->assertInstanceOf(Inequality::class, $notEquals);
6674
}
75+
76+
public function testIn()
77+
{
78+
$in = $this->a->in($this->list);
79+
80+
$this->assertInstanceOf(In::class, $in);
81+
}
6782
}

0 commit comments

Comments
 (0)