Skip to content

Commit c08bfaa

Browse files
committed
wip
1 parent 859edd9 commit c08bfaa

12 files changed

+364
-58
lines changed

src/Helper/ToStringHelper.php

Lines changed: 212 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66

77
use Exception;
88
use Stringable;
9+
use Syndesi\CypherDataStructures\Contract\NodeConstraintInterface;
10+
use Syndesi\CypherDataStructures\Contract\NodeIndexInterface;
911
use Syndesi\CypherDataStructures\Contract\NodeInterface;
12+
use Syndesi\CypherDataStructures\Contract\RelationConstraintInterface;
13+
use Syndesi\CypherDataStructures\Contract\RelationIndexInterface;
1014
use Syndesi\CypherDataStructures\Contract\RelationInterface;
1115
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
1216

@@ -221,4 +225,212 @@ public static function relationToString(RelationInterface $relation, bool $ident
221225

222226
return implode('', $parts);
223227
}
228+
229+
/**
230+
* @param array<string, mixed> $options
231+
*/
232+
public static function optionsToString(array $options): string
233+
{
234+
return '';
235+
}
236+
237+
public static function nodeConstraintToString(NodeConstraintInterface $nodeConstraint): string
238+
{
239+
$parts = [];
240+
241+
$parts[] = 'CONSTRAINT';
242+
243+
if ($name = $nodeConstraint->getName()) {
244+
$parts[] = $name;
245+
}
246+
247+
$parts[] = 'FOR';
248+
249+
$for = $nodeConstraint->getFor();
250+
if (!$for) {
251+
throw new InvalidArgumentException('For can not be null');
252+
}
253+
$parts[] = sprintf("(node:%s)", $for);
254+
255+
$parts[] = 'REQUIRE';
256+
257+
$properties = array_keys($nodeConstraint->getProperties());
258+
sort($properties);
259+
$propertyCount = count($properties);
260+
if (0 === $propertyCount) {
261+
throw new InvalidArgumentException('At least one property is required');
262+
}
263+
if (1 === $propertyCount) {
264+
$parts[] = sprintf("node.%s", array_shift($properties));
265+
}
266+
if ($propertyCount > 1) {
267+
$propertyParts = [];
268+
foreach ($properties as $property) {
269+
$propertyParts[] = sprintf("node.%s", $property);
270+
}
271+
$parts[] = sprintf("(%s)", implode(', ', $propertyParts));
272+
}
273+
274+
$parts[] = 'IS';
275+
276+
$type = $nodeConstraint->getType();
277+
if (!$type) {
278+
throw new InvalidArgumentException('Type can not be null');
279+
}
280+
$parts[] = $type;
281+
282+
$options = $nodeConstraint->getOptions();
283+
if (count($options) > 0) {
284+
$parts[] = 'OPTIONS';
285+
$parts[] = self::optionsToString($options);
286+
}
287+
288+
return implode(' ', $parts);
289+
}
290+
291+
public static function relationConstraintToString(RelationConstraintInterface $relationConstraint): string
292+
{
293+
$parts = [];
294+
295+
$parts[] = 'CONSTRAINT';
296+
297+
if ($name = $relationConstraint->getName()) {
298+
$parts[] = $name;
299+
}
300+
301+
$parts[] = 'FOR';
302+
303+
$for = $relationConstraint->getFor();
304+
if (!$for) {
305+
throw new InvalidArgumentException('For can not be null');
306+
}
307+
$parts[] = sprintf("()-[relation:%s]-()", $for);
308+
309+
$parts[] = 'REQUIRE';
310+
311+
$properties = array_keys($relationConstraint->getProperties());
312+
sort($properties);
313+
$propertyCount = count($properties);
314+
if (0 === $propertyCount) {
315+
throw new InvalidArgumentException('At least one property is required');
316+
}
317+
if (1 === $propertyCount) {
318+
$parts[] = sprintf("relation.%s", array_shift($properties));
319+
}
320+
if ($propertyCount > 1) {
321+
$propertyParts = [];
322+
foreach ($properties as $property) {
323+
$propertyParts[] = sprintf("relation.%s", $property);
324+
}
325+
$parts[] = sprintf("(%s)", implode(', ', $propertyParts));
326+
}
327+
328+
$parts[] = 'IS';
329+
330+
$type = $relationConstraint->getType();
331+
if (!$type) {
332+
throw new InvalidArgumentException('Type can not be null');
333+
}
334+
$parts[] = $type;
335+
336+
$options = $relationConstraint->getOptions();
337+
if (count($options) > 0) {
338+
$parts[] = 'OPTIONS';
339+
$parts[] = self::optionsToString($options);
340+
}
341+
342+
return implode(' ', $parts);
343+
}
344+
345+
public static function nodeIndexToString(NodeIndexInterface $nodeIndex): string
346+
{
347+
$parts = [];
348+
349+
$type = $nodeIndex->getType();
350+
if ($type) {
351+
$parts[] = $type;
352+
}
353+
354+
$parts[] = 'INDEX';
355+
356+
if ($name = $nodeIndex->getName()) {
357+
$parts[] = $name;
358+
}
359+
360+
$parts[] = 'FOR';
361+
362+
$for = $nodeIndex->getFor();
363+
if (!$for) {
364+
throw new InvalidArgumentException('For can not be null');
365+
}
366+
$parts[] = sprintf("(node:%s)", $for);
367+
368+
$parts[] = 'ON';
369+
370+
$properties = array_keys($nodeIndex->getProperties());
371+
sort($properties);
372+
$propertyCount = count($properties);
373+
if (0 === $propertyCount) {
374+
throw new InvalidArgumentException('At least one property is required');
375+
}
376+
$propertyParts = [];
377+
foreach ($properties as $property) {
378+
$propertyParts[] = sprintf("node.%s", $property);
379+
}
380+
$parts[] = sprintf("(%s)", implode(', ', $propertyParts));
381+
382+
$options = $nodeIndex->getOptions();
383+
if (count($options) > 0) {
384+
$parts[] = 'OPTIONS';
385+
$parts[] = self::optionsToString($options);
386+
}
387+
388+
return implode(' ', $parts);
389+
}
390+
391+
public static function relationIndexToString(RelationIndexInterface $relationIndex): string
392+
{
393+
$parts = [];
394+
395+
$type = $relationIndex->getType();
396+
if ($type) {
397+
$parts[] = $type;
398+
}
399+
400+
$parts[] = 'INDEX';
401+
402+
if ($name = $relationIndex->getName()) {
403+
$parts[] = $name;
404+
}
405+
406+
$parts[] = 'FOR';
407+
408+
$for = $relationIndex->getFor();
409+
if (!$for) {
410+
throw new InvalidArgumentException('For can not be null');
411+
}
412+
$parts[] = sprintf("()-[relation:%s]-()", $for);
413+
414+
$parts[] = 'ON';
415+
416+
$properties = array_keys($relationIndex->getProperties());
417+
sort($properties);
418+
$propertyCount = count($properties);
419+
if (0 === $propertyCount) {
420+
throw new InvalidArgumentException('At least one property is required');
421+
}
422+
$propertyParts = [];
423+
foreach ($properties as $property) {
424+
$propertyParts[] = sprintf("relation.%s", $property);
425+
}
426+
$parts[] = sprintf("(%s)", implode(', ', $propertyParts));
427+
428+
$options = $relationIndex->getOptions();
429+
if (count($options) > 0) {
430+
$parts[] = 'OPTIONS';
431+
$parts[] = self::optionsToString($options);
432+
}
433+
434+
return implode(' ', $parts);
435+
}
224436
}

src/Type/Constraint.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\ConstraintInterface;
8-
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9-
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
108
use Syndesi\CypherDataStructures\Trait\OptionsTrait;
119
use Syndesi\CypherDataStructures\Trait\PropertiesTrait;
1210

@@ -56,21 +54,4 @@ public function setFor(?string $for): self
5654

5755
return $this;
5856
}
59-
60-
/**
61-
* @throws InvalidArgumentException
62-
*/
63-
public function __toString()
64-
{
65-
return ToCypherHelper::constraintToCypherString($this);
66-
}
67-
68-
public function isEqualTo(mixed $element): bool
69-
{
70-
if (!($element instanceof ConstraintInterface)) {
71-
return false;
72-
}
73-
74-
return ToCypherHelper::constraintToCypherString($this) === ToCypherHelper::constraintToCypherString($element);
75-
}
7657
}

src/Type/Index.php

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\IndexInterface;
8-
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9-
use Syndesi\CypherDataStructures\Helper\ToCypherHelper;
108
use Syndesi\CypherDataStructures\Trait\OptionsTrait;
119
use Syndesi\CypherDataStructures\Trait\PropertiesTrait;
1210

@@ -56,21 +54,4 @@ public function setFor(?string $for): self
5654

5755
return $this;
5856
}
59-
60-
/**
61-
* @throws InvalidArgumentException
62-
*/
63-
public function __toString()
64-
{
65-
return ToCypherHelper::indexToCypherString($this);
66-
}
67-
68-
public function isEqualTo(mixed $element): bool
69-
{
70-
if (!($element instanceof IndexInterface)) {
71-
return false;
72-
}
73-
74-
return ToCypherHelper::indexToCypherString($this) === ToCypherHelper::indexToCypherString($element);
75-
}
7657
}

src/Type/NodeConstraint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\NodeConstraintInterface;
8+
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
810

911
class NodeConstraint extends Constraint implements NodeConstraintInterface
1012
{
13+
/**
14+
* @throws InvalidArgumentException
15+
*/
16+
public function __toString()
17+
{
18+
return ToStringHelper::nodeConstraintToString($this);
19+
}
20+
21+
public function isEqualTo(mixed $element): bool
22+
{
23+
if (!($element instanceof NodeConstraintInterface)) {
24+
return false;
25+
}
26+
27+
return ToStringHelper::nodeConstraintToString($this) === ToStringHelper::nodeConstraintToString($element);
28+
}
1129
}

src/Type/NodeIndex.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\NodeIndexInterface;
8+
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
810

911
class NodeIndex extends Index implements NodeIndexInterface
1012
{
13+
/**
14+
* @throws InvalidArgumentException
15+
*/
16+
public function __toString()
17+
{
18+
return ToStringHelper::nodeIndexToString($this);
19+
}
20+
21+
public function isEqualTo(mixed $element): bool
22+
{
23+
if (!($element instanceof NodeIndexInterface)) {
24+
return false;
25+
}
26+
27+
return ToStringHelper::nodeIndexToString($this) === ToStringHelper::nodeIndexToString($element);
28+
}
1129
}

src/Type/RelationConstraint.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\RelationConstraintInterface;
8+
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
810

911
class RelationConstraint extends Constraint implements RelationConstraintInterface
1012
{
13+
/**
14+
* @throws InvalidArgumentException
15+
*/
16+
public function __toString()
17+
{
18+
return ToStringHelper::relationConstraintToString($this);
19+
}
20+
21+
public function isEqualTo(mixed $element): bool
22+
{
23+
if (!($element instanceof RelationConstraintInterface)) {
24+
return false;
25+
}
26+
27+
return ToStringHelper::relationConstraintToString($this) === ToStringHelper::relationConstraintToString($element);
28+
}
1129
}

src/Type/RelationIndex.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,25 @@
55
namespace Syndesi\CypherDataStructures\Type;
66

77
use Syndesi\CypherDataStructures\Contract\RelationIndexInterface;
8+
use Syndesi\CypherDataStructures\Exception\InvalidArgumentException;
9+
use Syndesi\CypherDataStructures\Helper\ToStringHelper;
810

911
class RelationIndex extends Index implements RelationIndexInterface
1012
{
13+
/**
14+
* @throws InvalidArgumentException
15+
*/
16+
public function __toString()
17+
{
18+
return ToStringHelper::relationIndexToString($this);
19+
}
20+
21+
public function isEqualTo(mixed $element): bool
22+
{
23+
if (!($element instanceof RelationIndexInterface)) {
24+
return false;
25+
}
26+
27+
return ToStringHelper::relationIndexToString($this) === ToStringHelper::relationIndexToString($element);
28+
}
1129
}

0 commit comments

Comments
 (0)