Skip to content

Commit be393ee

Browse files
Merge pull request #30 from WikibaseSolutions/add-regex-operator
Add regex operator (#26)
2 parents 133e0f5 + 1ba51aa commit be393ee

File tree

5 files changed

+145
-0
lines changed

5 files changed

+145
-0
lines changed

src/Regex.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
27+
28+
/**
29+
* Represents the application of the regex operator.
30+
*
31+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-string
32+
*/
33+
class Regex extends BinaryOperator implements BooleanType
34+
{
35+
use BooleanTypeTrait;
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function __construct(StringType $left, StringType $right)
41+
{
42+
parent::__construct($left, $right);
43+
}
44+
45+
/**
46+
* @inheritDoc
47+
*/
48+
protected function getOperator(): string
49+
{
50+
return "=~";
51+
}
52+
}

src/Traits/StringTypeTrait.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use WikibaseSolutions\CypherDSL\Contains;
2525
use WikibaseSolutions\CypherDSL\EndsWith;
26+
use WikibaseSolutions\CypherDSL\Regex;
2627
use WikibaseSolutions\CypherDSL\StartsWith;
2728
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
2829

@@ -65,4 +66,15 @@ public function startsWith(StringType $right): StartsWith
6566
{
6667
return new StartsWith($this, $right);
6768
}
69+
70+
/**
71+
* Perform a regex comparison with the given expression.
72+
*
73+
* @param StringType $right
74+
* @return Regex
75+
*/
76+
public function regex(StringType $right): Regex
77+
{
78+
return new Regex($this, $right);
79+
}
6880
}

src/Types/PropertyTypes/StringType.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
use WikibaseSolutions\CypherDSL\Contains;
2525
use WikibaseSolutions\CypherDSL\EndsWith;
26+
use WikibaseSolutions\CypherDSL\Regex;
2627
use WikibaseSolutions\CypherDSL\StartsWith;
2728

2829
/**
@@ -53,4 +54,12 @@ public function endsWith(StringType $right): EndsWith;
5354
* @return StartsWith
5455
*/
5556
public function startsWith(StringType $right): StartsWith;
57+
58+
/**
59+
* Perform a regex comparison with the given expression.
60+
*
61+
* @param StringType $right
62+
* @return Regex
63+
*/
64+
public function regex(StringType $right): Regex;
5665
}

tests/Unit/RegexTest.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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\Contains;
27+
use WikibaseSolutions\CypherDSL\Regex;
28+
use WikibaseSolutions\CypherDSL\Types\AnyType;
29+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
30+
31+
/**
32+
* @covers \WikibaseSolutions\CypherDSL\Regex
33+
*/
34+
class RegexTest extends TestCase
35+
{
36+
use TestHelper;
37+
38+
public function testToQuery()
39+
{
40+
$regex = new Regex($this->getQueryConvertableMock(StringType::class, "a"), $this->getQueryConvertableMock(StringType::class, "b"));
41+
42+
$this->assertSame("(a =~ b)", $regex->toQuery());
43+
}
44+
45+
public function testCannotBeNested()
46+
{
47+
$regex = new Regex($this->getQueryConvertableMock(StringType::class, "a"), $this->getQueryConvertableMock(StringType::class, "b"));
48+
49+
$this->expectException(TypeError::class);
50+
51+
$regex = new Regex($regex, $regex);
52+
53+
$regex->toQuery();
54+
}
55+
56+
public function testDoesNotAcceptAnyTypeAsOperands()
57+
{
58+
$this->expectException(TypeError::class);
59+
60+
$regex = new Regex($this->getQueryConvertableMock(AnyType::class, "a"), $this->getQueryConvertableMock(AnyType::class, "b"));
61+
62+
$regex->toQuery();
63+
}
64+
}

tests/Unit/Traits/StringTypeTraitTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use PHPUnit\Framework\TestCase;
2626
use WikibaseSolutions\CypherDSL\Contains;
2727
use WikibaseSolutions\CypherDSL\EndsWith;
28+
use WikibaseSolutions\CypherDSL\Regex;
2829
use WikibaseSolutions\CypherDSL\StartsWith;
2930
use WikibaseSolutions\CypherDSL\Tests\Unit\TestHelper;
3031
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
@@ -72,4 +73,11 @@ public function testStartsWith()
7273

7374
$this->assertInstanceOf(StartsWith::class, $startsWith);
7475
}
76+
77+
public function testRegex()
78+
{
79+
$regex = $this->a->regex($this->b);
80+
81+
$this->assertInstanceOf(Regex::class, $regex);
82+
}
7583
}

0 commit comments

Comments
 (0)