Skip to content

Commit df91a59

Browse files
Merge pull request #7 from WikibaseSolutions/add-not-operator
Add "NOT" operator (#2)
2 parents 1f5a649 + 43d19aa commit df91a59

File tree

8 files changed

+145
-3
lines changed

8 files changed

+145
-3
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.7.0",
4+
"version": "2.8.0",
55
"type": "library",
66
"keywords": [
77
"neo4j",

src/Not.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
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\PropertyTypes\BooleanType;
26+
27+
/**
28+
* Represents the application of the negation (NOT) operator.
29+
*
30+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-boolean
31+
*/
32+
class Not implements BooleanType
33+
{
34+
use BooleanTypeTrait;
35+
36+
/**
37+
* @var BooleanType The expression to negate
38+
*/
39+
private BooleanType $expression;
40+
41+
/**
42+
* Not constructor.
43+
*
44+
* @param BooleanType $expression The expression to negate
45+
*/
46+
public function __construct(BooleanType $expression)
47+
{
48+
$this->expression = $expression;
49+
}
50+
51+
/**
52+
* @inheritDoc
53+
*/
54+
public function toQuery(): string
55+
{
56+
return sprintf("(NOT %s)", $this->expression->toQuery());
57+
}
58+
}

src/Traits/BooleanTypeTrait.php

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

2424
use WikibaseSolutions\CypherDSL\AndOperator;
25+
use WikibaseSolutions\CypherDSL\Minus;
26+
use WikibaseSolutions\CypherDSL\Not;
2527
use WikibaseSolutions\CypherDSL\OrOperator;
2628
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
2729
use WikibaseSolutions\CypherDSL\XorOperator;
@@ -65,4 +67,14 @@ public function xor(BooleanType $right): XorOperator
6567
{
6668
return new XorOperator($this, $right);
6769
}
70+
71+
/**
72+
* Negate this expression (using the NOT operator).
73+
*
74+
* @return Not
75+
*/
76+
public function not(): Not
77+
{
78+
return new Not($this);
79+
}
6880
}

src/Traits/NumeralTypeTrait.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ public function minus(NumeralType $right): Subtraction
152152
}
153153

154154
/**
155-
* Negate this expression.
155+
* Negate this expression (negate the numeral using "-").
156156
*
157157
* @return Minus
158158
*/

src/Types/PropertyTypes/BooleanType.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Types\PropertyTypes;
2323

2424
use WikibaseSolutions\CypherDSL\AndOperator;
25+
use WikibaseSolutions\CypherDSL\Not;
2526
use WikibaseSolutions\CypherDSL\OrOperator;
2627
use WikibaseSolutions\CypherDSL\XorOperator;
2728

@@ -53,4 +54,11 @@ public function or(BooleanType $right): OrOperator;
5354
* @return XorOperator
5455
*/
5556
public function xor(BooleanType $right): XorOperator;
57+
58+
/**
59+
* Negate this expression (using the NOT operator).
60+
*
61+
* @return Not
62+
*/
63+
public function not(): Not;
5664
}

src/Types/PropertyTypes/NumeralType.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ public function times(NumeralType $right): Multiplication;
119119
public function minus(NumeralType $right): Subtraction;
120120

121121
/**
122-
* Negate this expression.
122+
* Negate this expression (negate the numeral using "0").
123123
*
124124
* @return Minus
125125
*/

tests/Unit/NotTest.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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\Not;
27+
use WikibaseSolutions\CypherDSL\Types\AnyType;
28+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
29+
30+
/**
31+
* @covers \WikibaseSolutions\CypherDSL\Not
32+
*/
33+
class NotTest extends TestCase
34+
{
35+
use TestHelper;
36+
37+
public function testToQuery()
38+
{
39+
$not = new Not($this->getQueryConvertableMock(BooleanType::class, "true"));
40+
41+
$this->assertSame("(NOT true)", $not->toQuery());
42+
43+
$not = new Not($not);
44+
45+
$this->assertSame("(NOT (NOT true))", $not->toQuery());
46+
}
47+
48+
public function testDoesNotAcceptAnyTypeAsOperands()
49+
{
50+
$this->expectException(TypeError::class);
51+
52+
$and = new Not($this->getQueryConvertableMock(AnyType::class, "true"));
53+
54+
$and->toQuery();
55+
}
56+
}

tests/Unit/Traits/BooleanTypeTraitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use PHPUnit\Framework\MockObject\MockObject;
2525
use PHPUnit\Framework\TestCase;
2626
use WikibaseSolutions\CypherDSL\AndOperator;
27+
use WikibaseSolutions\CypherDSL\Not;
2728
use WikibaseSolutions\CypherDSL\OrOperator;
2829
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
2930
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
@@ -72,4 +73,11 @@ public function testXor()
7273

7374
$this->assertInstanceOf(XorOperator::class, $xor);
7475
}
76+
77+
public function testNot()
78+
{
79+
$not = $this->a->not();
80+
81+
$this->assertInstanceOf(Not::class, $not);
82+
}
7583
}

0 commit comments

Comments
 (0)