Skip to content

Commit 107061d

Browse files
Rewrite additional clauses and unit tests
1 parent 0b6e8e7 commit 107061d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+778
-925
lines changed

.github/SECURITY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
If you discover a security vulnerability within php-cypher-dsl, please send an e-mail to Marijn van Wezel at [email protected]. All security vulnerabilities will be promptly addressed.
88

9-
You may optionally encrypt your report with GPG, using the following key (fingerprint: `98ED 96AA 2260 D7E3`, proof: [keybase.io](https://keybase.io/01101101)):
9+
You may optionally encrypt your report with PGP, using the following key (fingerprint: `98ED96AA2260D7E3`, proof: [keybase.io](https://keybase.io/01101101)):
1010

1111
```text
1212
-----BEGIN PGP PUBLIC KEY BLOCK-----

src/Clauses/CallClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* This class represents a CALL {} (subquery) clause. The CALL {} clause evaluates a subquery that returns
2020
* some values.
2121
*
22-
* @note This feature is not officially part of the openCypher standard. For more information, see https://github.com/opencypher/openCypher/blob/a507292d35280aca9e37bf938cdec4fdd1e64ba9/docs/standardisation-scope.adoc.
22+
* @note This feature is not part of the openCypher standard. For more information, see https://github.com/opencypher/openCypher/blob/a507292d35280aca9e37bf938cdec4fdd1e64ba9/docs/standardisation-scope.adoc.
2323
*
2424
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call-subquery/
2525
* @see Query::call() for a more convenient method to construct this class

src/Clauses/CallProcedureClause.php

Lines changed: 34 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -1,159 +1,89 @@
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\Expressions\Procedures\Procedure;
2413
use WikibaseSolutions\CypherDSL\Expressions\Variable;
14+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2515
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2616
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
27-
use WikibaseSolutions\CypherDSL\Types\AnyType;
28-
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
2917

3018
/**
31-
* This class represents a CALL procedure clause. The CALL clause is used to call a procedure deployed in the database.
19+
* This class represents a CALL procedure clause.
20+
*
21+
* The CALL clause is used to call a procedure deployed in the database.
3222
*
3323
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 122)
3424
* @see https://neo4j.com/docs/cypher-manual/current/clauses/call/
3525
* @see Query::callProcedure() for a more convenient method to construct this class
3626
*/
37-
class CallProcedureClause extends Clause
27+
final class CallProcedureClause extends Clause
3828
{
29+
use CastTrait;
3930
use EscapeTrait;
4031
use ErrorTrait;
4132

4233
/**
43-
* @var string|null The procedure to call
34+
* @var Procedure|null The procedure to call
4435
*/
45-
private ?string $procedure = null;
36+
private ?Procedure $procedure = null;
4637

4738
/**
48-
* @var PropertyType[] The arguments passed to the procedure
49-
*/
50-
private array $arguments = [];
51-
52-
/**
53-
* @var Variable[] The result fields that will be returned
39+
* @var Variable[] The result fields that yielded
5440
*/
5541
private array $yields = [];
5642

5743
/**
58-
* Sets the procedure to call. This can be for instance "apoc.load.json".
44+
* Sets the procedure to call.
5945
*
60-
* @param string $procedure The procedure to call
46+
* @param Procedure $procedure The procedure to call
6147
* @return $this
6248
*/
63-
public function setProcedure(string $procedure): self
49+
public function setProcedure(Procedure $procedure): self
6450
{
6551
$this->procedure = $procedure;
6652

6753
return $this;
6854
}
6955

7056
/**
71-
* Sets the literal arguments to pass to this procedure call. This overwrites any previously passed
72-
* arguments.
57+
* Adds a variable to yield.
7358
*
74-
* @param AnyType[] $arguments The arguments to pass to the procedure
75-
* @return $this
76-
*/
77-
public function setArguments(array $arguments): self
78-
{
79-
foreach ($arguments as $argument) {
80-
$this->assertClass('arguments', AnyType::class, $argument);
81-
}
82-
83-
$this->arguments = $arguments;
84-
85-
return $this;
86-
}
87-
88-
/**
89-
* Add a literal argument to pass to this procedure call.
59+
* TODO: Allow variables to be aliased
9060
*
91-
* @param AnyType $argument The argument to pass to the procedure
61+
* @param Variable|string $yields The variable to yield
9262
* @return $this
9363
*/
94-
public function addArgument(AnyType $argument): self
64+
public function addYield(...$yields): self
9565
{
96-
$this->arguments[] = $argument;
66+
$res = [];
9767

98-
return $this;
99-
}
100-
101-
/**
102-
* Used to explicitly select which available result fields are returned as newly-bound
103-
* variables. If a key is non-numerical, it will be used as an alias.
104-
*
105-
* @param Variable[] $variables
106-
* @return $this
107-
*/
108-
public function setYields(array $variables): self
109-
{
110-
foreach ($variables as $variable) {
111-
$this->assertClass('variables', Variable::class, $variable);
68+
foreach ($yields as $yield) {
69+
$res[] = self::toName($yield);
11270
}
11371

114-
$this->yields = $variables;
115-
116-
return $this;
117-
}
118-
119-
/**
120-
* Adds a variable to yield.
121-
*
122-
* @param Variable $variable The variable to yield
123-
* @param string|null $alias Optionally the alias to use for the variable
124-
* @return $this
125-
*/
126-
public function addYield(Variable $variable, ?string $alias = null): self
127-
{
128-
if ($alias !== null) {
129-
$this->yields[$alias] = $variable;
130-
} else {
131-
$this->yields[] = $variable;
132-
}
72+
$this->yields = array_merge($this->yields, $res);
13373

13474
return $this;
13575
}
13676

13777
/**
13878
* Returns the procedure to call.
13979
*
140-
* @return string|null
80+
* @return Procedure
14181
*/
142-
public function getProcedure(): ?string
82+
public function getProcedure(): ?Procedure
14383
{
14484
return $this->procedure;
14585
}
14686

147-
/**
148-
* Returns the arguments of the procedure.
149-
*
150-
* @return AnyType[]
151-
*/
152-
public function getArguments(): array
153-
{
154-
return $this->arguments;
155-
}
156-
15787
/**
15888
* Returns the variables to yield.
15989
*
@@ -181,27 +111,13 @@ protected function getSubject(): string
181111
return "";
182112
}
183113

184-
$procedure = implode(
185-
'.',
186-
array_map(fn (string $part): string => $this->escape($part), explode('.', $this->procedure))
187-
);
188-
189-
$arguments = implode(
190-
", ",
191-
array_map(fn (AnyType $pattern): string => $pattern->toQuery(), $this->arguments)
192-
);
193-
194-
if (count($this->yields) > 0) {
195-
$yieldParameters = [];
196-
foreach ($this->yields as $alias => $yieldVariable) {
197-
$yieldParameters[] = is_int($alias) ?
198-
$yieldVariable->toQuery() :
199-
sprintf("%s AS %s", $yieldVariable->toQuery(), $this->escape($alias));
200-
}
201-
202-
return sprintf("%s(%s) YIELD %s", $procedure, $arguments, implode(", ", $yieldParameters));
114+
$subject = $this->procedure->toQuery();
115+
116+
if (!empty($this->yields)) {
117+
$yields = array_map(fn (Variable $variable): string => $variable->toQuery(), $this->yields);
118+
$subject .= sprintf(" YIELD %s", implode(", ", $yields));
203119
}
204120

205-
return sprintf("%s(%s)", $procedure, $arguments);
121+
return $subject;
206122
}
207123
}

src/Clauses/CreateClause.php

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,53 @@
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

24-
use WikibaseSolutions\CypherDSL\Patterns\Node;
25-
use WikibaseSolutions\CypherDSL\Patterns\Path;
12+
use WikibaseSolutions\CypherDSL\Patterns\CompletePattern;
2613
use WikibaseSolutions\CypherDSL\Query;
2714
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2815

2916
/**
30-
* This class represents a CREATE clause. The CREATE clause is used to create graph elements.
17+
* This class represents a CREATE clause.
18+
*
19+
* The CREATE clause is used to create graph elements — nodes and relationships.
3120
*
3221
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 99)
3322
* @see https://neo4j.com/docs/cypher-manual/current/clauses/create/
3423
* @see Query::create() for a more convenient method to construct this class
3524
*/
36-
class CreateClause extends Clause
25+
final class CreateClause extends Clause
3726
{
3827
use ErrorTrait;
3928

4029
/**
41-
* @var Path[]|Node[] The patterns to create
30+
* @var CompletePattern[] The patterns to create
4231
*/
4332
private array $patterns = [];
4433

4534
/**
46-
* Sets the pattern to create. This overwrites any previously added patterns.
47-
*
48-
* @param Path[]|Node[] $patterns The patterns to put in the CREATE clause
49-
* @return $this
50-
*/
51-
public function setPatterns(array $patterns): self
52-
{
53-
foreach ($patterns as $pattern) {
54-
$this->assertClass('patterns', [Path::class, Node::class], $pattern);
55-
}
56-
57-
$this->patterns = $patterns;
58-
59-
return $this;
60-
}
61-
62-
/**
63-
* Add a pattern to create.
35+
* Add one or more patterns to create.
6436
*
65-
* @param Path|Node $pattern The pattern to add to the CREATE clause
37+
* @param CompletePattern ...$pattern The patterns to add to the CREATE clause
6638
* @return $this
6739
*/
68-
public function addPattern($pattern): self
40+
public function addPattern(CompletePattern ...$pattern): self
6941
{
70-
$this->assertClass('pattern', [Path::class, Node::class], $pattern);
71-
$this->patterns[] = $pattern;
42+
$this->patterns = array_merge($this->patterns, $pattern);
7243

7344
return $this;
7445
}
7546

7647
/**
7748
* Returns the patterns of the CREATE clause.
7849
*
79-
* @return Path[]|Node[]
50+
* @return CompletePattern[]
8051
*/
8152
public function getPatterns(): array
8253
{
@@ -96,6 +67,9 @@ protected function getClause(): string
9667
*/
9768
protected function getSubject(): string
9869
{
99-
return implode(", ", array_map(fn ($pattern): string => $pattern->toQuery(), $this->patterns));
70+
return implode(
71+
", ",
72+
array_map(fn (CompletePattern $pattern): string => $pattern->toQuery(), $this->patterns)
73+
);
10074
}
10175
}

src/Clauses/LimitClause.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
/**
1717
* This class represents a LIMIT clause.
1818
*
19+
* LIMIT constrains the number of records in the output.
20+
*
1921
* @see https://neo4j.com/docs/cypher-manual/current/clauses/limit/
2022
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 98)
2123
* @see Query::limit() for a more convenient method to construct this class

0 commit comments

Comments
 (0)