-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNode.php
More file actions
172 lines (149 loc) · 4.51 KB
/
Copy pathNode.php
File metadata and controls
172 lines (149 loc) · 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php declare(strict_types=1);
/*
* This file is part of php-cypher-dsl.
*
* Copyright (C) Wikibase Solutions
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace WikibaseSolutions\CypherDSL\Patterns;
use WikibaseSolutions\CypherDSL\Expressions\Label;
use WikibaseSolutions\CypherDSL\Expressions\Literals\Map;
use function WikibaseSolutions\CypherDSL\node;
use WikibaseSolutions\CypherDSL\Traits\PatternTraits\PropertyPatternTrait;
use WikibaseSolutions\CypherDSL\Utils\NameUtils;
/**
* This class represents a node.
*
* @see https://neo4j.com/docs/cypher-manual/current/syntax/patterns/#cypher-pattern-node Corresponding documentation on Neo4j.com
*/
final class Node implements CompletePattern, PropertyPattern, RelatablePattern
{
use PropertyPatternTrait;
/**
* @var string[] The labels of this node
*/
private array $labels = [];
/**
* @param null|string $label The initial label to include on this node
*
* @internal This method is not covered by the backwards compatibility guarantee of php-cypher-dsl
*/
public function __construct(?string $label = null)
{
if ($label !== null) {
$this->labels[] = $label;
}
}
/**
* Sets the labels of this node. This overwrites any previously set labels.
*
* @param string[] $labels
*
* @return $this
*/
public function withLabels(array $labels): self
{
$this->labels = $labels;
return $this;
}
/**
* Adds one or more labels to this node.
*
* @return $this
*/
public function addLabel(string ...$label): self
{
$this->labels = array_merge($this->labels, $label);
return $this;
}
/**
* Returns the labels of the node.
*
* @return string[]
*/
public function getLabels(): array
{
return $this->labels;
}
/**
* Create a new node with the same variable as this node. Useful for using nodes
* inside a CREATE clause that were previously matched on in a MATCH clause.
*
* @see https://github.com/neo4j-php/php-cypher-dsl/issues/96
*/
public function variable(): self
{
return node()->withVariable($this->getVariable());
}
/**
* Returns a label with the variable in this node.
*
* @param string ...$labels The labels to attach to the variable in this node
*/
public function labeled(string ...$labels): Label
{
return new Label($this->getVariable(), ...$labels);
}
/**
* @inheritDoc
*/
public function relationship(Relationship $relationship, Pattern $pattern): Path
{
return (new Path($this))->relationship($relationship, $pattern);
}
/**
* @inheritDoc
*/
public function relationshipTo(Pattern $pattern, ?string $type = null, $properties = null, $name = null): Path
{
return (new Path($this))->relationshipTo($pattern, $type, $properties, $name);
}
/**
* @inheritDoc
*/
public function relationshipFrom(Pattern $pattern, ?string $type = null, $properties = null, $name = null): Path
{
return (new Path($this))->relationshipFrom($pattern, $type, $properties, $name);
}
/**
* @inheritDoc
*/
public function relationshipUni(Pattern $pattern, ?string $type = null, $properties = null, $name = null): Path
{
return (new Path($this))->relationshipUni($pattern, $type, $properties, $name);
}
/**
* Returns the string representation of this relationship that can be used directly
* in a query.
*/
public function toQuery(): string
{
return sprintf("(%s)", $this->nodeInnerToString());
}
/**
* Returns the string representation of the inner part of a node.
*/
private function nodeInnerToString(): string
{
$nodeInner = "";
if (isset($this->variable)) {
$nodeInner .= $this->variable->toQuery();
}
if ($this->labels !== []) {
foreach ($this->labels as $label) {
$nodeInner .= ":" . NameUtils::escape($label);
}
}
if (isset($this->properties)) {
if ($nodeInner !== "") {
$nodeInner .= " ";
}
if (!$this->properties instanceof Map || !$this->properties->isEmpty()) {
$nodeInner .= $this->properties->toQuery();
}
}
return $nodeInner;
}
}