Skip to content

Commit 1d833ad

Browse files
Merge pull request #37 from transistive/name-generation
WIP: Big PR adding a lot of stuff to improve quality of life
2 parents e11ef9c + 0547a2e commit 1d833ad

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1585
-1095
lines changed

src/Alias.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace WikibaseSolutions\CypherDSL;
4+
5+
use WikibaseSolutions\CypherDSL\Types\AnyType;
6+
use function sprintf;
7+
8+
/**
9+
* Represents aliasing an expression or variable.
10+
*
11+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/return/#return-column-alias
12+
* @see https://neo4j.com/docs/cypher-manual/current/clauses/with/#with-introduce-variables
13+
*/
14+
class Alias implements AnyType
15+
{
16+
/**
17+
* @var AnyType The original item to be aliased
18+
*/
19+
private AnyType $original;
20+
21+
/**
22+
* @var Variable The new variable aliasing the original
23+
*/
24+
private Variable $variable;
25+
26+
/**
27+
* Alias constructor.
28+
*
29+
* @param AnyType $original The original item to be aliased
30+
* @param Variable $variable The new variable aliasing the original
31+
*/
32+
public function __construct(AnyType $original, Variable $variable)
33+
{
34+
$this->original = $original;
35+
$this->variable = $variable;
36+
}
37+
38+
/**
39+
* @inheritDoc
40+
*/
41+
public function toQuery(): string
42+
{
43+
return sprintf("%s AS %s", $this->original->toQuery(), $this->variable->toQuery());
44+
}
45+
46+
/**
47+
* Gets the original item of the alias.
48+
*
49+
* @return AnyType
50+
*/
51+
public function getOriginal(): AnyType
52+
{
53+
return $this->original;
54+
}
55+
56+
/**
57+
* Gets the variable from the alias.
58+
*
59+
* @return Variable
60+
*/
61+
public function getVariable(): Variable
62+
{
63+
return $this->variable;
64+
}
65+
}

src/Assignment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class Assignment extends BinaryOperator
4646
*/
4747
public function __construct(AnyType $left, AnyType $right)
4848
{
49-
$this->assertClass('left', [Property::class, Variable::class], $left);
49+
self::assertClass('left', [Property::class, Variable::class], $left);
5050

5151
parent::__construct($left, $right, false);
5252
}

src/BinaryOperator.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,20 @@
2121

2222
namespace WikibaseSolutions\CypherDSL;
2323

24+
use WikibaseSolutions\CypherDSL\Traits\AliasableTrait;
2425
use WikibaseSolutions\CypherDSL\Types\AnyType;
2526

2627
/**
2728
* This class represents the application of a binary operator, such as "+", "/" and "*".
2829
*/
2930
abstract class BinaryOperator implements QueryConvertable
3031
{
32+
use AliasableTrait;
33+
3134
/**
3235
* @var bool Whether to insert parentheses around the expression
3336
*/
34-
private bool $insertParentheses = true;
37+
private bool $insertParentheses;
3538

3639
/**
3740
* @var AnyType The left-hand of the expression

src/Clauses/CreateClause.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use WikibaseSolutions\CypherDSL\Assignment;
2524
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2625
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2726

@@ -35,20 +34,18 @@ class CreateClause extends Clause
3534
use ErrorTrait;
3635

3736
/**
38-
* @var StructuralType[]|Assignment[] The patterns to create
37+
* @var StructuralType[] The patterns to create
3938
*/
4039
private array $patterns = [];
4140

4241
/**
4342
* Add a pattern to create.
4443
*
45-
* @param StructuralType|Assignment $pattern The pattern to create
44+
* @param StructuralType $pattern The pattern to create
4645
* @return CreateClause
4746
*/
48-
public function addPattern($pattern): self
47+
public function addPattern(StructuralType $pattern): self
4948
{
50-
$this->assertClass('pattern', [StructuralType::class, Assignment::class], $pattern);
51-
5249
$this->patterns[] = $pattern;
5350

5451
return $this;

src/Clauses/MatchClause.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use WikibaseSolutions\CypherDSL\Assignment;
2524
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2625
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2726

@@ -35,7 +34,7 @@ class MatchClause extends Clause
3534
use ErrorTrait;
3635

3736
/**
38-
* @var StructuralType[]|Assignment[] List of patterns
37+
* @var StructuralType[] List of patterns
3938
*/
4039
private array $patterns = [];
4140

@@ -52,13 +51,11 @@ public function getPatterns(): array
5251
/**
5352
* Add a pattern to the match clause.
5453
*
55-
* @param StructuralType|Assignment $pattern
54+
* @param StructuralType $pattern
5655
* @return MatchClause
5756
*/
58-
public function addPattern($pattern): self
57+
public function addPattern(StructuralType $pattern): self
5958
{
60-
$this->assertClass('pattern', [StructuralType::class, Assignment::class], $pattern);
61-
6259
$this->patterns[] = $pattern;
6360

6461
return $this;

src/Clauses/MergeClause.php

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

2424
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
25-
use WikibaseSolutions\CypherDSL\Assignment;
2625
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2726

2827
/**
@@ -35,9 +34,9 @@ class MergeClause extends Clause
3534
use ErrorTrait;
3635

3736
/**
38-
* @var StructuralType|Assignment|null $pattern The pattern to merge
37+
* @var StructuralType|null $pattern The pattern to merge
3938
*/
40-
private $pattern = null;
39+
private ?StructuralType $pattern = null;
4140

4241
/**
4342
* @var Clause|null $createClause The clause to execute when the pattern is created
@@ -82,13 +81,11 @@ public function getPattern(): ?StructuralType
8281
/**
8382
* Sets the pattern to merge.
8483
*
85-
* @param StructuralType|Assignment $pattern The pattern to merge
84+
* @param StructuralType $pattern The pattern to merge
8685
* @return MergeClause
8786
*/
88-
public function setPattern($pattern): self
87+
public function setPattern(StructuralType $pattern): self
8988
{
90-
$this->assertClass('pattern', [StructuralType::class, Assignment::class], $pattern);
91-
9289
$this->pattern = $pattern;
9390

9491
return $this;

src/Clauses/OptionalMatchClause.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
namespace WikibaseSolutions\CypherDSL\Clauses;
2323

24-
use WikibaseSolutions\CypherDSL\Assignment;
2524
use WikibaseSolutions\CypherDSL\Traits\ErrorTrait;
2625
use WikibaseSolutions\CypherDSL\Types\StructuralTypes\StructuralType;
2726

@@ -35,7 +34,7 @@ class OptionalMatchClause extends Clause
3534
use ErrorTrait;
3635

3736
/**
38-
* @var StructuralType[]|Assignment[] List of patterns
37+
* @var StructuralType[] List of patterns
3938
*/
4039
private array $patterns = [];
4140

@@ -52,13 +51,11 @@ public function getPatterns(): array
5251
/**
5352
* Add a pattern to the optional match clause.
5453
*
55-
* @param StructuralType|Assignment $pattern
54+
* @param StructuralType $pattern
5655
* @return OptionalMatchClause
5756
*/
58-
public function addPattern($pattern): self
57+
public function addPattern(StructuralType $pattern): self
5958
{
60-
$this->assertClass('pattern', [StructuralType::class, Assignment::class], $pattern);
61-
6259
$this->patterns[] = $pattern;
6360

6461
return $this;

src/Clauses/SkipClause.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@ protected function getSubject(): string
7979

8080
return "";
8181
}
82-
}
82+
}

src/Functions/FunctionCall.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
namespace WikibaseSolutions\CypherDSL\Functions;
2323

2424
use WikibaseSolutions\CypherDSL\QueryConvertable;
25+
use WikibaseSolutions\CypherDSL\Traits\AliasableTrait;
2526
use WikibaseSolutions\CypherDSL\Types\AnyType;
2627
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\ListType;
2728
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
@@ -35,6 +36,8 @@
3536
*/
3637
abstract class FunctionCall implements QueryConvertable
3738
{
39+
use AliasableTrait;
40+
3841
/**
3942
* Produces a raw function call. This enables the usage of unimplemented functions in your
4043
* Cypher queries. The parameters of this function are not type-checked.

src/Functions/LocalDateTime.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
use WikibaseSolutions\CypherDSL\Traits\LocalDateTimeTrait;
2525
use WikibaseSolutions\CypherDSL\Types\AnyType;
26-
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\DateType;
2726
use WikibaseSolutions\CypherDSL\Types\PropertyTypes\LocalDateTimeType;
2827

2928
/**

0 commit comments

Comments
 (0)