Skip to content

Commit 77d6c63

Browse files
committed
moved all functionality to Query class
1 parent d33329a commit 77d6c63

File tree

9 files changed

+105
-187
lines changed

9 files changed

+105
-187
lines changed

src/ExpressionList.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,6 @@ public function __construct(array $expressions)
7474
$this->expressions = $expressions;
7575
}
7676

77-
/**
78-
* Constructs an expression list from literals (eg. strings, numbers and booleans)
79-
* @param iterable $literals
80-
* @return static
81-
*/
82-
public static function fromLiterals(iterable $literals): self
83-
{
84-
$tbr = [];
85-
foreach ($literals as $literal) {
86-
$tbr[] = Literal::fromLiteral($literal);
87-
}
88-
89-
return new self($tbr);
90-
}
91-
9277
/**
9378
* @inheritDoc
9479
*/

src/Literals/Boolean.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Represents a boolean (true or false) literal.
2929
*/
30-
class Boolean extends Literal implements BooleanType
30+
class Boolean implements BooleanType
3131
{
3232
use BooleanTypeTrait;
3333

src/Literals/Decimal.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
/**
2828
* Represents a decimal (integer or float) literal.
2929
*/
30-
class Decimal extends Literal implements NumeralType
30+
class Decimal implements NumeralType
3131
{
3232
use NumeralTypeTrait;
3333

src/Literals/Literal.php

Lines changed: 0 additions & 59 deletions
This file was deleted.

src/Literals/StringLiteral.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @see https://neo4j.com/docs/cypher-manual/current/syntax/expressions/#cypher-expressions-string-literals
3232
*/
33-
class StringLiteral extends Literal implements StringType
33+
class StringLiteral implements StringType
3434
{
3535
use StringTypeTrait;
3636

src/Query.php

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,14 @@
5454
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\NodeType;
5555
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\PathType;
5656
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
57+
use function get_class;
58+
use function gettype;
59+
use function is_bool;
60+
use function is_float;
61+
use function is_int;
62+
use function is_object;
63+
use function is_string;
64+
use function method_exists;
5765

5866
/**
5967
* Builder class for building complex Cypher queries.
@@ -125,25 +133,25 @@ public static function variable(string $variable): Variable
125133
* Creates a new literal from the given value. This function automatically constructs the appropriate
126134
* class based on the type of the value given.
127135
*
128-
* @param integer|float|bool|string $literal The literal to construct
136+
* @param mixed $literal The literal to construct
129137
* @return StringLiteral|Boolean|Decimal
130138
*/
131139
public static function literal($literal): PropertyType
132140
{
133-
$literalType = gettype($literal);
134-
135-
switch ($literalType) {
136-
case "string":
137-
return new StringLiteral($literal);
138-
case "boolean":
139-
return new Boolean($literal);
140-
case "double":
141-
case "float":
142-
case "integer":
143-
return new Decimal($literal);
144-
default:
145-
throw new InvalidArgumentException("The literal type " . $literalType . " is not supported by Cypher");
141+
if (is_string($literal) || (is_object($literal) && method_exists($literal, '__toString'))) {
142+
return new StringLiteral((string) $literal);
146143
}
144+
145+
if (is_bool($literal)) {
146+
return new Boolean($literal);
147+
}
148+
149+
if (is_int($literal) || is_float($literal)) {
150+
return new Decimal($literal);
151+
}
152+
153+
$actualType = is_object($literal) ? get_class($literal) : gettype($literal);
154+
throw new InvalidArgumentException("The literal type " . $actualType . " is not supported by Cypher");
147155
}
148156

149157
/**
@@ -157,6 +165,23 @@ public static function list(array $expressions): ExpressionList
157165
return new ExpressionList($expressions);
158166
}
159167

168+
/**
169+
* Creates a list of literal expressions.
170+
*
171+
* @param mixed[] $expressions
172+
*
173+
* @return ExpressionList
174+
*/
175+
public static function literalList(iterable $literals): ExpressionList
176+
{
177+
$tbr = [];
178+
foreach ($literals as $literal) {
179+
$tbr[] = self::literal($literal);
180+
}
181+
182+
return new ExpressionList($tbr);
183+
}
184+
160185
/**
161186
* Creates a property map.
162187
*

tests/Unit/ExpressionListTest.php

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -40,20 +40,6 @@ public function testEmpty(): void
4040
$this->assertSame("[]", $expressionList->toQuery());
4141
}
4242

43-
public function testFromLiterals(): void
44-
{
45-
$expressionList = ExpressionList::fromLiterals(['a', 'b', 234.3, 1, true, false]);
46-
47-
$this->assertSame("['a', 'b', 234.3, 1, true, false]", $expressionList->toQuery());
48-
}
49-
50-
public function testFromLiteralsError(): void
51-
{
52-
$expressionList = ExpressionList::fromLiterals(['a', 'b', 234.3, 1, true, false]);
53-
$this->expectException(TypeError::class);
54-
$expressionList = ExpressionList::fromLiterals([$expressionList]);
55-
}
56-
5743
/**
5844
* @dataProvider provideOneDimensionalData
5945
* @param array $expressions

tests/Unit/Literals/LiteralTest.php

Lines changed: 0 additions & 82 deletions
This file was deleted.

tests/Unit/QueryTest.php

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Tests\Unit;
2323

24+
use InvalidArgumentException;
2425
use PHPUnit\Framework\TestCase;
26+
use TypeError;
2527
use WikibaseSolutions\CypherDSL\Assignment;
2628
use WikibaseSolutions\CypherDSL\Clauses\Clause;
2729
use WikibaseSolutions\CypherDSL\Clauses\MatchClause;
@@ -373,6 +375,67 @@ public function testBuildEmpty()
373375
$this->assertSame("", $query->build());
374376
}
375377

378+
public function testInt(): void
379+
{
380+
$literal = Query::literal(1);
381+
self::assertInstanceOf(Decimal::class, $literal);
382+
self::assertEquals('1', $literal->toQuery());
383+
}
384+
385+
public function testFloat(): void
386+
{
387+
$literal = Query::literal(1.2);
388+
self::assertInstanceOf(Decimal::class, $literal);
389+
self::assertEquals('1.2', $literal->toQuery());
390+
}
391+
392+
public function testString(): void
393+
{
394+
$literal = Query::literal('abc');
395+
self::assertInstanceOf(StringLiteral::class, $literal);
396+
self::assertEquals("'abc'", $literal->toQuery());
397+
}
398+
399+
public function testStringAble(): void
400+
{
401+
$literal = Query::literal(new class () {
402+
public function __toString(): string
403+
{
404+
return 'stringable abc';
405+
}
406+
});
407+
self::assertInstanceOf(StringLiteral::class, $literal);
408+
self::assertEquals("'stringable abc'", $literal->toQuery());
409+
}
410+
411+
public function testBool(): void
412+
{
413+
$literal = Query::literal(true);
414+
self::assertInstanceOf(Boolean::class, $literal);
415+
self::assertEquals("true", $literal->toQuery());
416+
}
417+
418+
public function testInvalidLiteral(): void
419+
{
420+
$literal = Query::literal(true);
421+
$this->expectException(InvalidArgumentException::class);
422+
Query::literal($literal);
423+
}
424+
425+
public function testFromLiterals(): void
426+
{
427+
$expressionList = Query::literalList(['a', 'b', 234.3, 1, true, false]);
428+
429+
$this->assertSame("['a', 'b', 234.3, 1, true, false]", $expressionList->toQuery());
430+
}
431+
432+
public function testFromLiteralsError(): void
433+
{
434+
$expressionList = Query::literalList(['a', 'b', 234.3, 1, true, false]);
435+
$this->expectException(InvalidArgumentException::class);
436+
$expressionList = Query::literalList([$expressionList]);
437+
}
438+
376439
public function testWikiExamples()
377440
{
378441
/*

0 commit comments

Comments
 (0)