Skip to content

Commit 88f69da

Browse files
Merge pull request #104 from neo4j-php/62-add-support-for-comparison-chains
2 parents d46b80d + 4be3155 commit 88f69da

17 files changed

Lines changed: 915 additions & 1 deletion

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ The format is based on [Keep a Changelog], and this project adheres to
1111
### Added
1212

1313
- Added support for operator chaining (e.g. `a > b > c`).
14+
- Added support for `shortestPath` and `allShortestPaths` pattern constructs.
15+
- Added support for shortest path constructs (`SHORTEST k`, `ALL SHORTEST`, `SHORTEST k GROUPS`, and `ANY`).
1416

1517
### Changed
1618

src/Patterns/AllShortest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
15+
/**
16+
* Represents the ALL SHORTEST construct.
17+
*
18+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/shortest-paths/
19+
*/
20+
final class AllShortest implements CompletePattern
21+
{
22+
use PatternTrait;
23+
24+
/**
25+
* @var CompletePattern The pattern to match
26+
*/
27+
private CompletePattern $pattern;
28+
29+
/**
30+
* @param CompletePattern $pattern The pattern to find all shortest paths for
31+
*/
32+
public function __construct(CompletePattern $pattern)
33+
{
34+
$this->pattern = $pattern;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function toQuery(): string
41+
{
42+
$cql = '';
43+
44+
if (isset($this->variable)) {
45+
$cql = $this->variable->toQuery() . ' = ';
46+
}
47+
48+
$cql .= sprintf("ALL SHORTEST (%s)", $this->pattern->toQuery());
49+
50+
return $cql;
51+
}
52+
}

src/Patterns/AllShortestPaths.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
15+
/**
16+
* Represents an allShortestPaths pattern.
17+
*
18+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/reference/#shortest-functions
19+
*/
20+
final class AllShortestPaths implements CompletePattern
21+
{
22+
use PatternTrait;
23+
24+
/**
25+
* @var CompletePattern The pattern to match
26+
*/
27+
private CompletePattern $pattern;
28+
29+
/**
30+
* @param CompletePattern $pattern The pattern to find all shortest paths for
31+
*/
32+
public function __construct(CompletePattern $pattern)
33+
{
34+
$this->pattern = $pattern;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function toQuery(): string
41+
{
42+
$cql = '';
43+
44+
if (isset($this->variable)) {
45+
$cql = $this->variable->toQuery() . ' = ';
46+
}
47+
48+
$cql .= sprintf("allShortestPaths(%s)", $this->pattern->toQuery());
49+
50+
return $cql;
51+
}
52+
}

src/Patterns/AnyPath.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
15+
/**
16+
* Represents the ANY construct.
17+
*
18+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/shortest-paths/
19+
*/
20+
final class AnyPath implements CompletePattern
21+
{
22+
use PatternTrait;
23+
24+
/**
25+
* @var CompletePattern The pattern to match
26+
*/
27+
private CompletePattern $pattern;
28+
29+
/**
30+
* @param CompletePattern $pattern The pattern to find any path for
31+
*/
32+
public function __construct(CompletePattern $pattern)
33+
{
34+
$this->pattern = $pattern;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function toQuery(): string
41+
{
42+
$cql = '';
43+
44+
if (isset($this->variable)) {
45+
$cql = $this->variable->toQuery() . ' = ';
46+
}
47+
48+
$cql .= sprintf("ANY (%s)", $this->pattern->toQuery());
49+
50+
return $cql;
51+
}
52+
}

src/Patterns/Shortest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\IntegerType;
15+
use WikibaseSolutions\CypherDSL\Utils\CastUtils;
16+
17+
/**
18+
* Represents the SHORTEST k construct.
19+
*
20+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/shortest-paths/
21+
*/
22+
final class Shortest implements CompletePattern
23+
{
24+
use PatternTrait;
25+
26+
/**
27+
* @var CompletePattern The pattern to match
28+
*/
29+
private CompletePattern $pattern;
30+
31+
/**
32+
* @var IntegerType The number of paths to match
33+
*/
34+
private IntegerType $k;
35+
36+
/**
37+
* @param CompletePattern $pattern The pattern to find the shortest path for
38+
* @param int|IntegerType $k The number of paths to match
39+
*/
40+
public function __construct(CompletePattern $pattern, int|IntegerType $k)
41+
{
42+
$this->pattern = $pattern;
43+
$this->k = CastUtils::toIntegerType($k);
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function toQuery(): string
50+
{
51+
$cql = '';
52+
53+
if (isset($this->variable)) {
54+
$cql = $this->variable->toQuery() . ' = ';
55+
}
56+
57+
$cql .= sprintf("SHORTEST %s (%s)", $this->k->toQuery(), $this->pattern->toQuery());
58+
59+
return $cql;
60+
}
61+
}

src/Patterns/ShortestGroups.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\IntegerType;
15+
use WikibaseSolutions\CypherDSL\Utils\CastUtils;
16+
17+
/**
18+
* Represents the SHORTEST k GROUPS construct.
19+
*
20+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/shortest-paths/
21+
*/
22+
final class ShortestGroups implements CompletePattern
23+
{
24+
use PatternTrait;
25+
26+
/**
27+
* @var CompletePattern The pattern to match
28+
*/
29+
private CompletePattern $pattern;
30+
31+
/**
32+
* @var IntegerType The number of groups to match
33+
*/
34+
private IntegerType $k;
35+
36+
/**
37+
* @param CompletePattern $pattern The pattern to find the shortest groups for
38+
* @param int|IntegerType $k The number of groups to match
39+
*/
40+
public function __construct(CompletePattern $pattern, int|IntegerType $k)
41+
{
42+
$this->pattern = $pattern;
43+
$this->k = CastUtils::toIntegerType($k);
44+
}
45+
46+
/**
47+
* @inheritDoc
48+
*/
49+
public function toQuery(): string
50+
{
51+
$cql = '';
52+
53+
if (isset($this->variable)) {
54+
$cql = $this->variable->toQuery() . ' = ';
55+
}
56+
57+
$cql .= sprintf("SHORTEST %s GROUPS (%s)", $this->k->toQuery(), $this->pattern->toQuery());
58+
59+
return $cql;
60+
}
61+
}

src/Patterns/ShortestPath.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php declare(strict_types=1);
2+
/*
3+
* This file is part of php-cypher-dsl.
4+
*
5+
* Copyright (C) Wikibase Solutions
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
11+
namespace WikibaseSolutions\CypherDSL\Patterns;
12+
13+
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PatternTrait;
14+
15+
/**
16+
* Represents a shortestPath pattern.
17+
*
18+
* @see https://neo4j.com/docs/cypher-manual/current/patterns/reference/#shortest-functions
19+
*/
20+
final class ShortestPath implements CompletePattern
21+
{
22+
use PatternTrait;
23+
24+
/**
25+
* @var CompletePattern The pattern to match
26+
*/
27+
private CompletePattern $pattern;
28+
29+
/**
30+
* @param CompletePattern $pattern The pattern to find the shortest path for
31+
*/
32+
public function __construct(CompletePattern $pattern)
33+
{
34+
$this->pattern = $pattern;
35+
}
36+
37+
/**
38+
* @inheritDoc
39+
*/
40+
public function toQuery(): string
41+
{
42+
$cql = '';
43+
44+
if (isset($this->variable)) {
45+
$cql = $this->variable->toQuery() . ' = ';
46+
}
47+
48+
$cql .= sprintf("shortestPath(%s)", $this->pattern->toQuery());
49+
50+
return $cql;
51+
}
52+
}

0 commit comments

Comments
 (0)