Skip to content

Commit 17d0ad0

Browse files
author
Wout Gevaert
committed
Improve Query::returning, Query::with and Query::callProcedure and fix CastTrait::toAnyType
1 parent 44faad2 commit 17d0ad0

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

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)