Skip to content

Commit e5abdb2

Browse files
Rewrite DeleteClause
1 parent 107061d commit e5abdb2

File tree

4 files changed

+56
-70
lines changed

4 files changed

+56
-70
lines changed

src/Clauses/DeleteClause.php

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +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

24-
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
12+
use WikibaseSolutions\CypherDSL\Patterns\Pattern;
13+
use WikibaseSolutions\CypherDSL\Query;
14+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2515
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2616

2717
/**
2818
* This class represents a DELETE clause.
2919
*
20+
* The DELETE clause is used to delete graph element — nodes, relationships and paths.
21+
*
3022
* @see https://neo4j.com/docs/cypher-manual/current/clauses/delete/
23+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 105)
24+
* @see Query::delete() for a more convenient method to construct this class
3125
*/
32-
class DeleteClause extends Clause
26+
final class DeleteClause extends Clause
3327
{
34-
use ErrorTrait;
28+
use CastTrait;
3529

3630
/**
3731
* Whether the DETACH modifier is needed.
@@ -60,31 +54,20 @@ public function setDetach(bool $detach = true): self
6054
}
6155

6256
/**
63-
* Sets the structures to be deleted. This overwrites previous structures if they exist.
57+
* Add one or more structures to delete.
6458
*
65-
* @param StructuralType[] $structures The structures to delete
59+
* @param StructuralType|Pattern $structures The structures to delete
6660
* @return $this
6761
*/
68-
public function setStructures(array $structures): self
62+
public function addStructure(...$structures): self
6963
{
64+
$res = [];
65+
7066
foreach ($structures as $structure) {
71-
$this->assertClass('structure', StructuralType::class, $structure);
67+
$res[] = self::toStructuralType($structure);
7268
}
7369

74-
$this->structures = $structures;
75-
76-
return $this;
77-
}
78-
79-
/**
80-
* Add a structure to be deleted.
81-
*
82-
* @param StructuralType $structure The structure that should be deleted
83-
* @return $this
84-
*/
85-
public function addStructure(StructuralType $structure): self
86-
{
87-
$this->structures[] = $structure;
70+
$this->structures = array_merge($this->structures, $res);
8871

8972
return $this;
9073
}
@@ -104,7 +87,7 @@ public function detachesDeletion(): bool
10487
*
10588
* @return StructuralType[]
10689
*/
107-
public function getStructures(): array
90+
public function getStructural(): array
10891
{
10992
return $this->structures;
11093
}

src/Query.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
6666
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
6767
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\RelationshipType;
68+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
6869

6970
/**
7071
* Builder class for building complex Cypher queries.
@@ -475,34 +476,23 @@ public function create($patterns): self
475476
/**
476477
* Creates the DELETE clause.
477478
*
478-
* @param string|Variable|HasVariable|(string|Variable|HasVariable)[] $variables The nodes to delete
479+
* @param StructuralType|Pattern|StructuralType[]|Pattern[] $structures The structures to delete
479480
* @param bool $detach Whether to DETACH DELETE
480481
*
481482
* @return $this
482483
* @see https://neo4j.com/docs/cypher-manual/current/clauses/delete/
483-
*
484+
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 105)
484485
*/
485-
public function delete($variables, bool $detach = false): self
486+
public function delete($structures, bool $detach = false): self
486487
{
487-
$deleteClause = new DeleteClause();
488-
$deleteClause->setDetach($detach);
489-
490-
if (!is_array($variables)) {
491-
$variables = [$variables];
492-
}
493-
494-
foreach ($variables as $variable) {
495-
$this->assertClass('variable', ['string', Variable::class, HasVariable::class], $variable);
496-
497-
if (is_string($variable)) {
498-
$variable = Query::variable($variable);
499-
} elseif ($variable instanceof HasVariable) {
500-
$variable = $variable->getVariable();
501-
}
502-
503-
$deleteClause->addVariable($variable);
488+
if (!is_array($structures)) {
489+
$structures = [$structures];
504490
}
505491

492+
$deleteClause = new DeleteClause();
493+
$deleteClause->setDetach($detach);
494+
$deleteClause->addStructure(...$structures);
495+
506496
$this->clauses[] = $deleteClause;
507497

508498
return $this;

src/Traits/CastTrait.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\NumeralType;
1313
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\PropertyType;
1414
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\StringType;
15+
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
1516

1617
/**
1718
* Helper trait for casting native PHP types to Cypher-DSL types. Casts are added to this class on an as-needed basis.
@@ -94,6 +95,18 @@ private static function toPropertyType($property): PropertyType
9495
return $property instanceof PropertyType ? $property : Literal::literal($property);
9596
}
9697

98+
/**
99+
* Casts the given value to a StructuralType.
100+
*
101+
* @param StructuralType|Pattern $structure
102+
* @return StructuralType
103+
*/
104+
private static function toStructuralType($structure): StructuralType
105+
{
106+
self::assertClass('structure', [Pattern::class, StructuralType::class], $structure);
107+
return $structure instanceof StructuralType ? $structure : $structure->getVariable();
108+
}
109+
97110
/**
98111
* Casts the given value to a Variable.
99112
*

tests/Unit/Clauses/DeleteClauseTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testEmptyClause(): void
4040
$delete = new DeleteClause();
4141

4242
$this->assertSame("", $delete->toQuery());
43-
$this->assertEquals([], $delete->getStructures());
43+
$this->assertEquals([], $delete->getStructural());
4444
$this->assertFalse($delete->detachesDeletion());
4545
}
4646

@@ -52,7 +52,7 @@ public function testSingleVariable(): void
5252
$delete->addStructure($variable);
5353

5454
$this->assertSame("DELETE a", $delete->toQuery());
55-
$this->assertEquals([$variable], $delete->getStructures());
55+
$this->assertEquals([$variable], $delete->getStructural());
5656
$this->assertFalse($delete->detachesDeletion());
5757
}
5858

@@ -67,7 +67,7 @@ public function testMultipleVariables(): void
6767
$delete->addStructure($b);
6868

6969
$this->assertSame("DELETE a, b", $delete->toQuery());
70-
$this->assertEquals([$a, $b], $delete->getStructures());
70+
$this->assertEquals([$a, $b], $delete->getStructural());
7171
$this->assertFalse($delete->detachesDeletion());
7272
}
7373

@@ -80,7 +80,7 @@ public function testDetachDelete(): void
8080
$delete->setDetach(true);
8181

8282
$this->assertSame("DETACH DELETE a", $delete->toQuery());
83-
$this->assertEquals([$variable], $delete->getStructures());
83+
$this->assertEquals([$variable], $delete->getStructural());
8484
$this->assertTrue($delete->detachesDeletion());
8585
}
8686

@@ -91,7 +91,7 @@ public function testAcceptsVariable(): void
9191

9292
$delete->addStructure($variable);
9393
$delete->toQuery();
94-
$this->assertEquals([$variable], $delete->getStructures());
94+
$this->assertEquals([$variable], $delete->getStructural());
9595
$this->assertFalse($delete->detachesDeletion());
9696
}
9797

@@ -115,10 +115,10 @@ public function testSetVariables(): void
115115

116116
$variables = [$variableA, $variableB];
117117

118-
$delete->setStructures($variables);
118+
$delete->setPatterns($variables);
119119

120120
$this->assertSame("DELETE a, b", $delete->toQuery());
121-
$this->assertSame($variables, $delete->getStructures());
121+
$this->assertSame($variables, $delete->getStructural());
122122
}
123123

124124
public function testSetVariablesDoesNotAcceptAnyType(): void
@@ -132,20 +132,20 @@ public function testSetVariablesDoesNotAcceptAnyType(): void
132132

133133
$this->expectException(TypeError::class);
134134

135-
$delete->setStructures($variables);
135+
$delete->setPatterns($variables);
136136
$delete->toQuery();
137137
}
138138

139139
public function testGetVariables(): void
140140
{
141141
$delete = new DeleteClause();
142142

143-
$this->assertSame([], $delete->getStructures());
143+
$this->assertSame([], $delete->getStructural());
144144

145145
$variables = [new Variable('a')];
146-
$delete->setStructures($variables);
146+
$delete->setPatterns($variables);
147147

148-
$this->assertSame($variables, $delete->getStructures());
148+
$this->assertSame($variables, $delete->getStructural());
149149
}
150150

151151
public function testDetachesDeletion(): void

0 commit comments

Comments
 (0)