|
1 |
| -<?php |
2 |
| - |
| 1 | +<?php declare(strict_types=1); |
3 | 2 | /*
|
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. |
11 | 4 | *
|
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 |
16 | 6 | *
|
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. |
20 | 9 | */
|
21 |
| - |
22 | 10 | namespace WikibaseSolutions\CypherDSL\Clauses;
|
23 | 11 |
|
| 12 | +use WikibaseSolutions\CypherDSL\Expressions\Procedures\Procedure; |
24 | 13 | use WikibaseSolutions\CypherDSL\Expressions\Variable;
|
| 14 | +use WikibaseSolutions\CypherDSL\Traits\CastTrait; |
25 | 15 | use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
|
26 | 16 | use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
|
27 |
| -use WikibaseSolutions\CypherDSL\Types\AnyType; |
28 |
| -use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType; |
29 | 17 |
|
30 | 18 | /**
|
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. |
32 | 22 | *
|
33 | 23 | * @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 122)
|
34 | 24 | * @see https://neo4j.com/docs/cypher-manual/current/clauses/call/
|
35 | 25 | * @see Query::callProcedure() for a more convenient method to construct this class
|
36 | 26 | */
|
37 |
| -class CallProcedureClause extends Clause |
| 27 | +final class CallProcedureClause extends Clause |
38 | 28 | {
|
| 29 | + use CastTrait; |
39 | 30 | use EscapeTrait;
|
40 | 31 | use ErrorTrait;
|
41 | 32 |
|
42 | 33 | /**
|
43 |
| - * @var string|null The procedure to call |
| 34 | + * @var Procedure|null The procedure to call |
44 | 35 | */
|
45 |
| - private ?string $procedure = null; |
| 36 | + private ?Procedure $procedure = null; |
46 | 37 |
|
47 | 38 | /**
|
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 |
54 | 40 | */
|
55 | 41 | private array $yields = [];
|
56 | 42 |
|
57 | 43 | /**
|
58 |
| - * Sets the procedure to call. This can be for instance "apoc.load.json". |
| 44 | + * Sets the procedure to call. |
59 | 45 | *
|
60 |
| - * @param string $procedure The procedure to call |
| 46 | + * @param Procedure $procedure The procedure to call |
61 | 47 | * @return $this
|
62 | 48 | */
|
63 |
| - public function setProcedure(string $procedure): self |
| 49 | + public function setProcedure(Procedure $procedure): self |
64 | 50 | {
|
65 | 51 | $this->procedure = $procedure;
|
66 | 52 |
|
67 | 53 | return $this;
|
68 | 54 | }
|
69 | 55 |
|
70 | 56 | /**
|
71 |
| - * Sets the literal arguments to pass to this procedure call. This overwrites any previously passed |
72 |
| - * arguments. |
| 57 | + * Adds a variable to yield. |
73 | 58 | *
|
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 |
90 | 60 | *
|
91 |
| - * @param AnyType $argument The argument to pass to the procedure |
| 61 | + * @param Variable|string $yields The variable to yield |
92 | 62 | * @return $this
|
93 | 63 | */
|
94 |
| - public function addArgument(AnyType $argument): self |
| 64 | + public function addYield(...$yields): self |
95 | 65 | {
|
96 |
| - $this->arguments[] = $argument; |
| 66 | + $res = []; |
97 | 67 |
|
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); |
112 | 70 | }
|
113 | 71 |
|
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); |
133 | 73 |
|
134 | 74 | return $this;
|
135 | 75 | }
|
136 | 76 |
|
137 | 77 | /**
|
138 | 78 | * Returns the procedure to call.
|
139 | 79 | *
|
140 |
| - * @return string|null |
| 80 | + * @return Procedure |
141 | 81 | */
|
142 |
| - public function getProcedure(): ?string |
| 82 | + public function getProcedure(): ?Procedure |
143 | 83 | {
|
144 | 84 | return $this->procedure;
|
145 | 85 | }
|
146 | 86 |
|
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 |
| - |
157 | 87 | /**
|
158 | 88 | * Returns the variables to yield.
|
159 | 89 | *
|
@@ -181,27 +111,13 @@ protected function getSubject(): string
|
181 | 111 | return "";
|
182 | 112 | }
|
183 | 113 |
|
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)); |
203 | 119 | }
|
204 | 120 |
|
205 |
| - return sprintf("%s(%s)", $procedure, $arguments); |
| 121 | + return $subject; |
206 | 122 | }
|
207 | 123 | }
|
0 commit comments