Skip to content

Commit 6b2e727

Browse files
Work on rewriting traits to new DSL
1 parent e5abdb2 commit 6b2e727

File tree

11 files changed

+234
-344
lines changed

11 files changed

+234
-344
lines changed

src/Clauses/CallProcedureClause.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use WikibaseSolutions\CypherDSL\Expressions\Procedures\Procedure;
1313
use WikibaseSolutions\CypherDSL\Expressions\Variable;
14+
use WikibaseSolutions\CypherDSL\Syntax\Alias;
1415
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
1516
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
1617
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
@@ -56,17 +57,15 @@ public function setProcedure(Procedure $procedure): self
5657
/**
5758
* Adds a variable to yield.
5859
*
59-
* TODO: Allow variables to be aliased
60-
*
61-
* @param Variable|string $yields The variable to yield
60+
* @param Variable|Alias|string $yields The variable to yield
6261
* @return $this
6362
*/
6463
public function addYield(...$yields): self
6564
{
6665
$res = [];
6766

6867
foreach ($yields as $yield) {
69-
$res[] = self::toName($yield);
68+
$res[] = $yield instanceof Alias ? $yield : self::toName($yield);
7069
}
7170

7271
$this->yields = array_merge($this->yields, $res);

src/Clauses/LimitClause.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use WikibaseSolutions\CypherDSL\Query;
1313
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
14+
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\IntegerType;
1415
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\NumeralType;
1516

1617
/**
@@ -29,31 +30,29 @@ final class LimitClause extends Clause
2930
/**
3031
* The expression of the LIMIT statement.
3132
*
32-
* @var NumeralType|null $limit
33+
* @var IntegerType|null $limit
3334
*/
34-
private ?NumeralType $limit = null;
35+
private ?IntegerType $limit = null;
3536

3637
/**
3738
* Sets the expression that returns the limit.
3839
*
39-
* TODO: Rewrite this to only accept IntegerType (this also requires work in other parts of the DSL)
40-
*
41-
* @param NumeralType|int $limit The limit
40+
* @param IntegerType|int $limit The limit
4241
* @return $this
4342
*/
4443
public function setLimit($limit): self
4544
{
46-
$this->limit = self::toNumeralType($limit);
45+
$this->limit = self::toIntegerType($limit);
4746

4847
return $this;
4948
}
5049

5150
/**
5251
* Returns the limit of the clause.
5352
*
54-
* @return NumeralType|null
53+
* @return IntegerType|null
5554
*/
56-
public function getLimit(): ?NumeralType
55+
public function getLimit(): ?IntegerType
5756
{
5857
return $this->limit;
5958
}

src/Clauses/RemoveClause.php

Lines changed: 13 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
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.
3+
* This file is part of php-cypher-dsl.
114
*
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.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
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.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

2412
use WikibaseSolutions\CypherDSL\Expressions\Label;
@@ -30,8 +18,10 @@
3018
* This class represents a REMOVE clause.
3119
*
3220
* @see https://neo4j.com/docs/cypher-manual/current/clauses/remove/
21+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 113)
22+
* @see Query::remove() for a more convenient method to construct this class
3323
*/
34-
class RemoveClause extends Clause
24+
final class RemoveClause extends Clause
3525
{
3626
use ErrorTrait;
3727

@@ -41,32 +31,15 @@ class RemoveClause extends Clause
4131
private array $expressions = [];
4232

4333
/**
44-
* Sets the expressions of the REMOVE clause. This overwrites any previously set expressions.
45-
*
46-
* @param (Property|Label)[] $expressions The expressions to remove
47-
* @return RemoveClause
48-
*/
49-
public function setExpressions(array $expressions): self
50-
{
51-
foreach ($expressions as $expression) {
52-
$this->assertClass('expression', [Property::class, Label::class], $expression);
53-
}
54-
55-
$this->expressions = $expressions;
56-
57-
return $this;
58-
}
59-
60-
/**
61-
* Add an expression to the REMOVE clause. This expression usually returns a property (a.b) or a label (a:b).
34+
* Add one or more expressions to the REMOVE clause.
6235
*
63-
* @param Property|Label $expression The expression to add
36+
* @param Property|Label ...$expressions The expressions to add
6437
* @return RemoveClause
6538
*/
66-
public function addExpression($expression): self
39+
public function addExpression(...$expressions): self
6740
{
68-
$this->assertClass('expression', [Property::class, Label::class], $expression);
69-
$this->expressions[] = $expression;
41+
self::assertClassArray('expressions', [Property::class, Label::class], $expressions);
42+
$this->expressions = array_merge($this->expressions, $expressions);
7043

7144
return $this;
7245
}

src/Clauses/ReturnClause.php

Lines changed: 29 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,31 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
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.
3+
* This file is part of php-cypher-dsl.
114
*
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.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
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.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

12+
use WikibaseSolutions\CypherDSL\Patterns\Pattern;
13+
use WikibaseSolutions\CypherDSL\QueryConvertible;
14+
use WikibaseSolutions\CypherDSL\Syntax\Alias;
15+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2416
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
25-
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
2617
use WikibaseSolutions\CypherDSL\Types\AnyType;
2718

2819
/**
2920
* This class represents a RETURN clause.
3021
*
3122
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/
23+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 74)
24+
* @see Query::returning() for a more convenient method to construct this class
3225
*/
33-
class ReturnClause extends Clause
26+
final class ReturnClause extends Clause
3427
{
35-
use EscapeTrait;
28+
use CastTrait;
3629
use ErrorTrait;
3730

3831
/**
@@ -41,57 +34,40 @@ class ReturnClause extends Clause
4134
private bool $distinct = false;
4235

4336
/**
44-
* @var AnyType[] The expressions to return
37+
* @var AnyType[]|Alias[] The expressions to return
4538
*/
4639
private array $columns = [];
4740

4841
/**
49-
* Sets the columns of this RETURN clause. This overwrites any previously set columns.
42+
* Add a new column to this RETURN clause.
5043
*
51-
* @param AnyType[] $columns The columns; if the key is non-numerical, it will be used as the alias
44+
* @param AnyType|Alias|Pattern|int|float|string|bool|array ...$columns The values to return
5245
*
5346
* @return $this
5447
*
5548
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
5649
*/
57-
public function setColumns(array $columns): self
50+
public function addColumn(...$columns): self
5851
{
52+
$res = [];
53+
5954
foreach ($columns as $column) {
60-
$this->assertClass('column', AnyType::class, $column);
55+
$res[] = $column instanceof Alias ? $column : self::toAnyType($column);
6156
}
6257

63-
$this->columns = $columns;
58+
$this->columns = array_merge($this->columns, $res);
6459

6560
return $this;
6661
}
6762

6863
/**
69-
* Add a new column to this RETURN clause.
64+
* Sets this query to only retrieve unique rows.
7065
*
71-
* @param AnyType $column The expression to return
72-
* @param string $alias The alias of this column
66+
* @param bool $distinct
7367
*
7468
* @return $this
7569
*
76-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
77-
*/
78-
public function addColumn(AnyType $column, string $alias = ""): self
79-
{
80-
if ($alias !== "") {
81-
$this->columns[$alias] = $column;
82-
} else {
83-
$this->columns[] = $column;
84-
}
85-
86-
return $this;
87-
}
88-
89-
/**
90-
* Sets this query to only retrieve unique rows.
91-
*
92-
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-unique-results
93-
* @param bool $distinct
94-
* @return ReturnClause
70+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-unique-results
9571
*/
9672
public function setDistinct(bool $distinct = true): self
9773
{
@@ -103,7 +79,7 @@ public function setDistinct(bool $distinct = true): self
10379
/**
10480
* Returns the columns to return. Aliased columns have string keys instead of integers.
10581
*
106-
* @return AnyType[]
82+
* @return AnyType[]|Alias[]
10783
*/
10884
public function getColumns(): array
10985
{
@@ -135,15 +111,9 @@ protected function getClause(): string
135111
*/
136112
protected function getSubject(): string
137113
{
138-
$expressions = [];
139-
140-
foreach ($this->columns as $alias => $expression) {
141-
$expressionQuery = $expression->toQuery();
142-
$expressions[] = is_int($alias) ?
143-
$expressionQuery :
144-
sprintf("%s AS %s", $expressionQuery, $this->escape($alias));
145-
}
146-
147-
return implode(", ", $expressions);
114+
return implode(
115+
", ",
116+
array_map(fn (QueryConvertible $column) => $column->toQuery(), $this->columns)
117+
);
148118
}
149119
}

src/Clauses/SetClause.php

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
<?php
2-
1+
<?php declare(strict_types=1);
32
/*
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.
3+
* This file is part of php-cypher-dsl.
114
*
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.
5+
* Copyright (C) 2021- Wikibase Solutions
166
*
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.
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
209
*/
21-
2210
namespace WikibaseSolutions\CypherDSL\Clauses;
2311

2412
use WikibaseSolutions\CypherDSL\Expressions\Label;
@@ -30,8 +18,10 @@
3018
* This class represents a SET clause.
3119
*
3220
* @see https://neo4j.com/docs/cypher-manual/current/clauses/set/
21+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 107)
22+
* @see Query::set() for a more convenient method to construct this class
3323
*/
34-
class SetClause extends Clause
24+
final class SetClause extends Clause
3525
{
3626
use ErrorTrait;
3727

@@ -43,7 +33,7 @@ class SetClause extends Clause
4333
/**
4434
* Sets the subjects of this SET clause. This will overwrite any previously added expressions.
4535
*
46-
* @param (PropertyReplacement|Label)[] $expressions The expressions to set
36+
* @param PropertyReplacement[]|Label[] $expressions The expressions to set
4737
* @return $this
4838
*/
4939
public function setExpressions(array $expressions): self
@@ -58,23 +48,23 @@ public function setExpressions(array $expressions): self
5848
}
5949

6050
/**
61-
* Add an expression.
51+
* Add one or more expressions to this SET clause.
6252
*
63-
* @param PropertyReplacement|Label $expression The expression to add to this set clause
53+
* @param PropertyReplacement|Label $expressions The expressions to add to this set clause
6454
* @return $this
6555
*/
66-
public function add($expression): self
56+
public function add(...$expressions): self
6757
{
68-
$this->assertClass('expression', [PropertyReplacement::class, Label::class], $expression);
69-
$this->expressions[] = $expression;
58+
$this->assertClassArray('expression', [PropertyReplacement::class, Label::class], $expressions);
59+
$this->expressions = array_merge($this->expressions, $expressions);
7060

7161
return $this;
7262
}
7363

7464
/**
7565
* Returns the expressions to SET.
7666
*
77-
* @return (PropertyReplacement|Label)[]
67+
* @return PropertyReplacement[]|Label[]
7868
*/
7969
public function getExpressions(): array
8070
{

0 commit comments

Comments
 (0)