Skip to content

Commit c4810fc

Browse files
Merge branch '5.0.0-typing-and-consistency' into 5.0.0-fix-tests
2 parents dce0f96 + 16129ac commit c4810fc

File tree

6 files changed

+60
-9
lines changed

6 files changed

+60
-9
lines changed

src/Expressions/Literals/Float_.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@ public function getValue(): float
4848
*/
4949
public function toQuery(): string
5050
{
51-
return (string)$this->value;
51+
$value = (string) $this->value;
52+
if (
53+
ctype_digit($value)
54+
|| ($value[0] === '-' && ctype_digit(substr($value,1)) )
55+
) {
56+
$value .= '.0';
57+
}
58+
return $value;
5259
}
5360
}

src/Expressions/Literals/List_.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,16 @@ public function getExpressions(): array
7171
return $this->expressions;
7272
}
7373

74+
/**
75+
* Returns whether this list is empty.
76+
*
77+
* @return bool
78+
*/
79+
public function isEmpty(): bool
80+
{
81+
return empty($this->expressions);
82+
}
83+
7484
/**
7585
* @inheritDoc
7686
*/

src/Expressions/Literals/Map.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ public function getElements(): array
8888
return $this->elements;
8989
}
9090

91+
/**
92+
* Checks if this map is empty
93+
*
94+
* @return bool
95+
*/
96+
public function isEmpty(): bool
97+
{
98+
return empty($this->elements);
99+
}
100+
91101
/**
92102
* @inheritDoc
93103
*/

src/Patterns/Node.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ private function nodeInnerToString(): string
150150
}
151151
}
152152

153-
if (isset($this->properties)) {
153+
if (isset($this->properties) && !$this->properties->isEmpty()) {
154154
if ($nodeInner !== "") {
155155
$nodeInner .= " ";
156156
}

src/Query.php

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
use WikibaseSolutions\CypherDSL\Patterns\Relationship;
4949
use WikibaseSolutions\CypherDSL\Syntax\Alias;
5050
use WikibaseSolutions\CypherDSL\Syntax\PropertyReplacement;
51+
use WikibaseSolutions\CypherDSL\Traits\CastTrait;
5152
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
5253
use WikibaseSolutions\CypherDSL\Traits\EscapeTrait;
5354
use WikibaseSolutions\CypherDSL\Types\AnyType;
@@ -63,6 +64,7 @@ final class Query implements QueryConvertible
6364
{
6465
use EscapeTrait;
6566
use ErrorTrait;
67+
use CastTrait;
6668

6769
// A reference to the Literal class
6870
public const literal = Literal::class;
@@ -358,7 +360,7 @@ public function call($query = null, $variables = []): self
358360
* Creates the CALL procedure clause.
359361
*
360362
* @param Procedure $procedure The procedure to call
361-
* @param string|Variable|Alias|string[]|Variable[]|Alias[] $yields The result fields that should be returned
363+
* @param string|Variable|Alias|(string|Variable|Alias)[] $yields The result fields that should be returned
362364
*
363365
* @return $this
364366
*
@@ -370,6 +372,7 @@ public function callProcedure(Procedure $procedure, $yields = []): self
370372
if (!is_array($yields)) {
371373
$yields = [$yields];
372374
}
375+
$yields = $this->makeAliasArray($yields, 'toName');
373376

374377
$callProcedureClause = new CallProcedureClause();
375378
$callProcedureClause->setProcedure($procedure);
@@ -407,7 +410,7 @@ public function match($patterns): self
407410
/**
408411
* Creates the RETURN clause.
409412
*
410-
* @param AnyType|Alias|Pattern|int|float|string|bool|array|AnyType[]|Alias[]|Pattern[]|int[]|float[]|string[]|bool[]|array[] $expressions The expressions to return
413+
* @param AnyType|Alias|Pattern|int|float|string|bool|array|(AnyType|Alias|Pattern|int|float|string|bool|array)[] $expressions The expressions to return
411414
* @param bool $distinct Whether to be a RETURN DISTINCT query
412415
*
413416
* @return $this
@@ -420,6 +423,7 @@ public function returning($expressions, bool $distinct = false): self
420423
if (!is_array($expressions)) {
421424
$expressions = [$expressions];
422425
}
426+
$expressions = $this->makeAliasArray($expressions);
423427

424428
$returnClause = new ReturnClause();
425429
$returnClause->addColumn(...$expressions);
@@ -689,7 +693,7 @@ public function where($expressions, string $operator = WhereClause::AND): self
689693
/**
690694
* Creates the WITH clause.
691695
*
692-
* @param AnyType|Alias|Pattern|int|float|string|bool|array|AnyType[]|Alias[]|Pattern[]|int[]|float[]|string[]|bool[]|array[] $expressions The entries to add; if the array-key is non-numerical, it is used as the alias
696+
* @param AnyType|Alias|Pattern|int|float|string|bool|array|(AnyType|Alias|Pattern|int|float|string|bool|array)[] $expressions The entries to add; if the array-key is non-numerical, it is used as the alias
693697
*
694698
* @return $this
695699
*
@@ -701,6 +705,7 @@ public function with($expressions): self
701705
if (!is_array($expressions)) {
702706
$expressions = [$expressions];
703707
}
708+
$expressions = $this->makeAliasArray($expressions);
704709

705710
$withClause = new WithClause();
706711
$withClause->addEntry(...$expressions);
@@ -814,4 +819,23 @@ public function __toString(): string
814819
{
815820
return $this->build();
816821
}
822+
823+
/**
824+
* Changes an associative array into an array of aliasses.
825+
*
826+
* @param array $array The array to change into a sequential array
827+
* @param string $castFunc Name of the (static) method to use to cast the elements of $array.
828+
* @return array A sequential array, possibly consisting of aliasses.
829+
*/
830+
private function makeAliasArray(array $array, string $castFunc = 'toAnyType'): array
831+
{
832+
if (array_is_list($array)) {
833+
return array_map([self::class, $castFunc], $array);
834+
}
835+
$newArray = [];
836+
foreach ($array as $key => $value) {
837+
$newArray []= new Alias(call_user_func([self::class, $castFunc], $value), new Variable($key));
838+
}
839+
return $newArray;
840+
}
817841
}

src/Traits/CastTrait.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,14 @@ private static function toAnyType($value): AnyType
165165
{
166166
self::assertClass('value', [AnyType::class, Pattern::class, 'int', 'float', 'string', 'bool', 'array'], $value);
167167

168-
if ($value instanceof AnyType) {
169-
return $value;
170-
}
171-
172168
if ($value instanceof Pattern) {
173169
return $value->getVariable();
174170
}
175171

172+
if ($value instanceof AnyType) {
173+
return $value;
174+
}
175+
176176
return Literal::literal($value);
177177
}
178178
}

0 commit comments

Comments
 (0)