|
| 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 | +} |
0 commit comments