Skip to content

Commit 26c69e7

Browse files
author
Wout Gevaert
committed
Integer::$value is now a string, Literal::literal accepts objects, rename ExclusiveOr->ExclusiveDisjunction
1 parent f7ca956 commit 26c69e7

File tree

10 files changed

+48
-30
lines changed

10 files changed

+48
-30
lines changed

src/Expressions/Literals/Integer.php

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
use WikibaseSolutions\CypherDSL\Traits\TypeTraits\PropertyTypeTraits\IntegerTypeTrait;
1313
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\IntegerType;
14+
use TypeError;
1415

1516
/**
1617
* Represents an integer literal.
@@ -20,25 +21,34 @@ final class Integer implements IntegerType
2021
use IntegerTypeTrait;
2122

2223
/**
23-
* @var int The value
24+
* @var string The value
2425
*/
25-
private int $value;
26+
private string $value;
2627

2728
/**
28-
* @param int $value The value
29+
* @param int|string $value The value
2930
* @internal This function is not covered by the backwards compatibility guarantee of php-cypher-dsl
3031
*/
31-
public function __construct(int $value)
32+
public function __construct($value)
3233
{
33-
$this->value = $value;
34+
if (!is_int($value) && !is_string($value)) {
35+
throw new TypeError('An integer should be given as a string or integer, '.gettype($value).' received.');
36+
}
37+
$this->value = filter_var($value, FILTER_SANITIZE_NUMBER_INT);
38+
39+
if (is_string($value) && $this->value !== $value) {
40+
throw new TypeError(
41+
'A non-integer string has been provided: "'.$value.'", should be something like "'.$this->value.'".'
42+
);
43+
}
3444
}
3545

3646
/**
37-
* Returns the integer value.
47+
* Returns a string representation of the integer value.
3848
*
39-
* @return int
49+
* @return string
4050
*/
41-
public function getValue(): int
51+
public function getValue(): string
4252
{
4353
return $this->value;
4454
}
@@ -48,6 +58,6 @@ public function getValue(): int
4858
*/
4959
public function toQuery(): string
5060
{
51-
return (string)$this->value;
61+
return $this->value;
5262
}
5363
}

src/Expressions/Literals/Literal.php

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,12 @@ public static function literal($literal)
6969
return array_is_list($literal) ? self::list($literal) : self::map($literal);
7070
}
7171

72+
if (is_object($literal) && method_exists($literal, '__toString')) {
73+
return self::string($literal->__toString());
74+
}
75+
7276
throw new InvalidArgumentException(
73-
"The literal type " . is_object($literal) ? get_class($literal) : gettype($literal) . " is not supported by Cypher"
77+
"The literal type " . get_debug_type($literal) . " is not supported by Cypher"
7478
);
7579
}
7680

@@ -153,8 +157,11 @@ public static function float(float $value): Float_
153157
* @param array $value
154158
* @return List_
155159
*/
156-
public static function list(array $value): List_
160+
public static function list(iterable $value): List_
157161
{
162+
if (is_object($value)) {
163+
$value = iterator_to_array($value);
164+
}
158165
return new List_(array_map([self::class, 'toAnyType'], $value));
159166
}
160167

src/Expressions/Literals/Map.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ final class Map implements MapType
4343
*/
4444
public function __construct(array $elements = [])
4545
{
46-
self::assertClassArray('properties', AnyType::class, $elements);
46+
self::assertClassArray('elements', AnyType::class, $elements);
4747
$this->elements = $elements;
4848
}
4949

src/Expressions/Operators/ExclusiveDisjunction.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
namespace WikibaseSolutions\CypherDSL\Expressions\Operators;
1111

1212
/**
13-
* Represents the application of the exclusive or (XOR) operator.
13+
* Represents the application of the exclusive disjunction (XOR) operator.
1414
*
1515
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 50)
1616
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-boolean
1717
*/
18-
final class ExclusiveOr extends BooleanBinaryOperator
18+
final class ExclusiveDisjunction extends BooleanBinaryOperator
1919
{
2020
/**
2121
* @inheritDoc

src/Expressions/Operators/ModuloDivision.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
/**
1313
* Represents the application of the modulo division (%) operator.
14+
* This operator is sometimes simply called "modulo operator".
1415
*
1516
* @see https://s3.amazonaws.com/artifacts.opencypher.org/openCypher9.pdf (page 48)
1617
* @see https://neo4j.com/docs/cypher-manual/current/syntax/operators/#query-operators-mathematical

src/Patterns/Relationship.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ private function relationshipDetailToString(): string
301301

302302
if (isset($this->properties)) {
303303
// Only add the properties if they're not empty
304-
if (!$this->properties instanceof Map || $this->properties->getProperties() !== []) {
304+
if (!$this->properties instanceof Map || $this->properties->getElements() !== []) {
305305
$map = $this->properties->toQuery();
306306

307307
if ($conditionInner !== "") {

src/Query.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ public static function float(float $value): Float_
257257
* @param array $value
258258
* @return List_
259259
*/
260-
public static function list(array $value): List_
260+
public static function list(iterable $value): List_
261261
{
262262
return self::literal()::list($value);
263263
}
@@ -437,9 +437,9 @@ public function returning($expressions, bool $distinct = false): self
437437

438438
$expressions = array_map(function ($expression): AnyType {
439439
// If it has a variable, we want to put the variable in the RETURN clause instead of the
440-
// object itself. Theoretically, a node could be returned directly, but this is extremely
441-
// rare. If a user wants to do this, they can use the ReturnClause class directly.
442-
return $expression instanceof HasVariable ? $expression->getVariable() : $expression;
440+
// object itself. Theoretically, a node could be returned directly (i.e. "RETURN (n)"),
441+
// but this is extremely rare. If a user wants to do this, they can use the ReturnClause class directly.
442+
return $expression instanceof Pattern ? $expression->getVariable() : $expression;
443443
}, $expressions);
444444

445445
$returnClause->setColumns($expressions);
@@ -501,7 +501,7 @@ public function delete($structures, bool $detach = false): self
501501
/**
502502
* Creates the DETACH DELETE clause.
503503
*
504-
* @param string|Variable|HasVariable|(string|Variable|HasVariable)[] $variables The variables to delete, including relationships
504+
* @param string|Variable|Pattern|(string|Variable|Pattern)[] $variables The variables to delete, including nodes, relationships and paths
505505
*
506506
* @return $this
507507
* @see https://neo4j.com/docs/cypher-manual/current/clauses/delete/
@@ -724,9 +724,9 @@ public function with($expressions): self
724724
}
725725

726726
foreach ($expressions as $maybeAlias => $expression) {
727-
$this->assertClass('expression', AnyType::class, $expression);
727+
$this->assertClass('expression', [AnyType::class, Pattern::class], $expression);
728728

729-
if ($expression instanceof Node) {
729+
if ($expression instanceof Pattern) {
730730
$expression = $expression->getVariable();
731731
}
732732

src/Traits/EscapeTrait.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ trait EscapeTrait
4646
private static function escape(string $name): string
4747
{
4848
if ($name === "") {
49-
// Although some versions of Neo4j do not crash when the empty string is used as a name, there is no real
50-
// reason to ever use the empty string as a name
49+
// Although some versions of Neo4j do not crash when the empty string is used as a name,
50+
// there is no real reason to ever use the empty string as a name
5151
throw new InvalidArgumentException("A name cannot be the empty string");
5252
}
5353

src/Traits/TypeTraits/PropertyTypeTraits/BooleanTypeTrait.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Operators\Conjunction;
2525
use WikibaseSolutions\CypherDSL\Expressions\Operators\Disjunction;
26-
use WikibaseSolutions\CypherDSL\Expressions\Operators\ExclusiveOr;
26+
use WikibaseSolutions\CypherDSL\Expressions\Operators\ExclusiveDisjunction;
2727
use WikibaseSolutions\CypherDSL\Expressions\Operators\Negation;
2828
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
2929
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\BooleanType;
@@ -57,9 +57,9 @@ public function or($right, bool $insertParentheses = true): Disjunction
5757
/**
5858
* @inheritDoc
5959
*/
60-
public function xor($right, bool $insertParentheses = true): ExclusiveOr
60+
public function xor($right, bool $insertParentheses = true): ExclusiveDisjunction
6161
{
62-
return new ExclusiveOr($this, self::toBooleanType($right), $insertParentheses);
62+
return new ExclusiveDisjunction($this, self::toBooleanType($right), $insertParentheses);
6363
}
6464

6565
/**

src/Types/PropertyTypes/BooleanType.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
use WikibaseSolutions\CypherDSL\Expressions\Operators\Conjunction;
2525
use WikibaseSolutions\CypherDSL\Expressions\Operators\Disjunction;
26-
use WikibaseSolutions\CypherDSL\Expressions\Operators\ExclusiveOr;
26+
use WikibaseSolutions\CypherDSL\Expressions\Operators\ExclusiveDisjunction;
2727
use WikibaseSolutions\CypherDSL\Expressions\Operators\Negation;
2828
use WikibaseSolutions\CypherDSL\Traits\TypeTraits\PropertyTypeTraits\BooleanTypeTrait;
2929

@@ -57,9 +57,9 @@ public function or($right, bool $insertParentheses = true): Disjunction;
5757
*
5858
* @param BooleanType|bool $right
5959
* @param bool $insertParentheses
60-
* @return ExclusiveOr
60+
* @return ExclusiveDisjunction
6161
*/
62-
public function xor($right, bool $insertParentheses = true): ExclusiveOr;
62+
public function xor($right, bool $insertParentheses = true): ExclusiveDisjunction;
6363

6464
/**
6565
* Negate this expression (using the NOT operator).

0 commit comments

Comments
 (0)