Skip to content

Commit c544202

Browse files
committed
added static union factory method for combining to queries
1 parent 651bdac commit c544202

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Clauses/UnionClause.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace WikibaseSolutions\CypherDSL\Clauses;
44

5+
use WikibaseSolutions\CypherDSL\Query;
6+
57
/**
68
* This class represents the union clause.
79
*
@@ -20,6 +22,30 @@ public function __construct(bool $all = false)
2022
$this->all = $all;
2123
}
2224

25+
/**
26+
* Combines two queries with a union.
27+
*
28+
* @param Query $left The query preceding the union clause.
29+
* @param Query $right The query after the union clause.
30+
* @param bool $all Whether the union should include all results or remove the duplicates instead.
31+
*/
32+
public static function union(Query $left, Query $right, bool $all = false): Query
33+
{
34+
$tbr = Query::new();
35+
36+
foreach ($left->getClauses() as $clause) {
37+
$tbr->addClause($clause);
38+
}
39+
40+
$tbr->addClause(new self($all));
41+
42+
foreach ($right->getClauses() as $clause) {
43+
$tbr->addClause($clause);
44+
}
45+
46+
return $tbr;
47+
}
48+
2349
/**
2450
* Returns whether the union includes all results or removes the duplicates instead.
2551
*

0 commit comments

Comments
 (0)