Skip to content

Commit 28a46fa

Browse files
Merge pull request #67 from wgevaert/5.0.0-map-patch
Make Map not reference PropertyMap-like constructs anymore
2 parents 56eea3e + d37c9fa commit 28a46fa

File tree

2 files changed

+33
-29
lines changed

2 files changed

+33
-29
lines changed

src/Expressions/Literals/Map.php

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,14 @@
1616
use WikibaseSolutions\CypherDSL\Types\CompositeTypes\MapType;
1717

1818
/**
19-
* This class represents a map of properties. For example, this class can represent the following
19+
* This class represents a CYPHER map. For example, this class can represent the following
2020
* construct:
2121
*
2222
* {name: 'Andy', sport: 'Brazilian Ju-Jitsu'}
2323
*
24+
* @see https://neo4j.com/docs/cypher-manual/current/syntax/maps/
25+
*
26+
* For its use for properties, see
2427
* @see https://neo4j.com/docs/cypher-manual/current/syntax/patterns/#cypher-pattern-properties
2528
*/
2629
final class Map implements MapType
@@ -30,12 +33,12 @@ final class Map implements MapType
3033
use MapTypeTrait;
3134

3235
/**
33-
* @var AnyType[] The map of properties
36+
* @var AnyType[] The map
3437
*/
35-
private array $properties;
38+
private array $elements;
3639

3740
/**
38-
* @param AnyType[] $properties The map of expression
41+
* @param AnyType[] $elements Associative array of the elements that this map should have
3942
* @internal This method is not covered by the backwards compatibility promise of php-cypher-dsl
4043
*/
4144
public function __construct(array $properties = [])
@@ -45,19 +48,19 @@ public function __construct(array $properties = [])
4548
}
4649

4750
/**
48-
* Adds a property for the given name with the given value. Overrides the property if it already exists.
51+
* Adds an element for the given name with the given value. Overrides the element if the $key already exists.
4952
*
50-
* @param string $key The name of the property
51-
* @param mixed $value The value of the property
53+
* @param string $key The name/label for the element
54+
* @param mixed $value The value of the element
5255
* @return $this
5356
*/
54-
public function addProperty(string $key, $value): self
57+
public function add(string $key, $value): self
5558
{
5659
if (!$value instanceof AnyType) {
5760
$value = Literal::literal($value);
5861
}
5962

60-
$this->properties[$key] = $value;
63+
$this->elements[$key] = $value;
6164

6265
return $this;
6366
}
@@ -70,19 +73,19 @@ public function addProperty(string $key, $value): self
7073
*/
7174
public function mergeWith(Map $map): self
7275
{
73-
$this->properties = array_merge($this->properties, $map->properties);
76+
$this->elements = array_merge($this->elements, $map->getElements());
7477

7578
return $this;
7679
}
7780

7881
/**
79-
* Returns the map of properties as a number of key-expression pairs.
82+
* Returns the elements of this map as an associative array with key-value pairs.
8083
*
8184
* @return AnyType[]
8285
*/
83-
public function getProperties(): array
86+
public function getElements(): array
8487
{
85-
return $this->properties;
88+
return $this->elements;
8689
}
8790

8891
/**
@@ -92,7 +95,7 @@ public function toQuery(): string
9295
{
9396
$pairs = [];
9497

95-
foreach ($this->properties as $key => $value) {
98+
foreach ($this->elements as $key => $value) {
9699
$pairs[] = sprintf("%s: %s", $this->escape(strval($key)), $value->toQuery());
97100
}
98101

src/Traits/PatternTraits/AssignablePatternTrait.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,9 @@ public function withProperties($properties): self
6767
*/
6868
public function addProperty(string $key, $property): self
6969
{
70-
if (!isset($this->properties)) {
71-
$this->properties = new Map();
72-
} elseif (!$this->properties instanceof Map) {
73-
// Adding to a map is not natively supported by the MapType, but it is supported by Map. Syntactically, it
74-
// is not possible to add new items to, for instance, a Variable, eventhough it implements MapType. It is
75-
// however still useful to be able to add items to objects where a Map is used (that is, an object of
76-
// MapType with the {} syntax).
77-
throw new TypeError('$this->properties must be of type Map to support "addProperty"');
78-
}
70+
$this->makeProperties();
7971

80-
$this->properties->addProperty($key, $property);
72+
$this->properties->add($key, $property);
8173

8274
return $this;
8375
}
@@ -89,11 +81,7 @@ public function addProperties($properties): self
8981
{
9082
self::assertClass('properties', [Map::class, 'array'], $properties);
9183

92-
if (!isset($this->properties)) {
93-
$this->properties = new Map();
94-
} elseif (!$this->properties instanceof Map) {
95-
throw new TypeError('$this->properties must be of type PropertyMap to support "addProperty"');
96-
}
84+
$this->makeProperties();
9785

9886
if (is_array($properties)) {
9987
// Cast the array to a Map
@@ -105,6 +93,19 @@ public function addProperties($properties): self
10593
return $this;
10694
}
10795

96+
private function makeProperties(): void
97+
{
98+
if (!isset($this->properties)) {
99+
$this->properties = new Map();
100+
} elseif (!($this->properties instanceof Map)) {
101+
// Adding to a map is not natively supported by the MapType, but it is supported by Map. Syntactically, it
102+
// is not possible to add new items to, for instance, a Variable, even though it implements MapType. It is
103+
// however still useful to be able to add items to objects where a Map is used (that is, an object of
104+
// MapType with the {} syntax).
105+
throw new TypeError('$this->properties must be of type Map to support "addProperty"');
106+
}
107+
}
108+
108109
/**
109110
* @inheritDoc
110111
*/

0 commit comments

Comments
 (0)