Skip to content

Commit 0826dcf

Browse files
committed
finished documentation types
1 parent 574e6f2 commit 0826dcf

23 files changed

+562
-278
lines changed

src/Contracts/HasPropertiesInterface.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,30 @@
2020
* Defines how an object with properties should behave.
2121
*
2222
* @psalm-immutable
23+
*
24+
* @template T
2325
*/
2426
interface HasPropertiesInterface
2527
{
2628
/**
2729
* Returns the properties a map.
2830
*
29-
* @return CypherMap<mixed>
31+
* @return CypherMap<T>
3032
*/
3133
public function getProperties(): CypherMap;
3234

3335
/**
3436
* @param string $name
3537
*
36-
* @return mixed
38+
* @return T
3739
*/
3840
public function __get($name);
3941

4042
/**
4143
* Always throws an exception as cypher objects are immutable.
4244
*
4345
* @param string $name
44-
* @param mixed $value
46+
* @param T $value
4547
*
4648
* @throws BadMethodCallException
4749
*/

src/Contracts/PointInterface.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
* Defines a basic Point type in neo4j.
1818
*
1919
* @psalm-immutable
20+
*
21+
* @psalm-type Crs = 'wgs-84'|'wgs-84-3d'|'cartesian'|'cartesian-3d';
2022
*/
2123
interface PointInterface
2224
{
@@ -35,7 +37,7 @@ public function getY(): float;
3537
*
3638
* @see https://en.wikipedia.org/wiki/Spatial_reference_system
3739
*
38-
* @return 'wgs-84'|'wgs-84-3d'|'cartesian'|'cartesian-3d'
40+
* @return Crs
3941
*/
4042
public function getCrs(): string;
4143

src/Formatter/OGMFormatter.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
*
3535
* @see https://neo4j.com/docs/driver-manual/current/cypher-workflow/#driver-type-mapping
3636
*
37-
* @psalm-type OGMTypes = string|\Laudis\Neo4j\Types\Date|\Laudis\Neo4j\Types\DateTime|\Laudis\Neo4j\Types\Duration|\Laudis\Neo4j\Types\LocalDateTime|\Laudis\Neo4j\Types\LocalTime|\Laudis\Neo4j\Types\Time|int|float|bool|null|\Laudis\Neo4j\Types\CypherList|\Laudis\Neo4j\Types\CypherMap|\Laudis\Neo4j\Types\Node|\Laudis\Neo4j\Types\Relationship|\Laudis\Neo4j\Types\Path|\Laudis\Neo4j\Types\Cartesian3DPoint|\Laudis\Neo4j\Types\CartesianPoint|\Laudis\Neo4j\Types\WGS84Point|\Laudis\Neo4j\Types\WGS843DPoint
37+
* @psalm-type OGMTypes = string|int|float|bool|null|\Laudis\Neo4j\Types\Date|\Laudis\Neo4j\Types\DateTime|\Laudis\Neo4j\Types\Duration|\Laudis\Neo4j\Types\LocalDateTime|\Laudis\Neo4j\Types\LocalTime|\Laudis\Neo4j\Types\Time|\Laudis\Neo4j\Types\CypherList|\Laudis\Neo4j\Types\CypherMap|\Laudis\Neo4j\Types\Node|\Laudis\Neo4j\Types\Relationship|\Laudis\Neo4j\Types\Path|\Laudis\Neo4j\Types\Cartesian3DPoint|\Laudis\Neo4j\Types\CartesianPoint|\Laudis\Neo4j\Types\WGS84Point|\Laudis\Neo4j\Types\WGS843DPoint
3838
* @implements FormatterInterface<CypherList<CypherMap<OGMTypes>>>
3939
*
4040
* @psalm-type OGMResults = CypherList<CypherMap<OGMTypes>>

src/Types/AbstractCypherContainer.php renamed to src/Types/AbstractCypherObject.php

Lines changed: 42 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use function array_key_exists;
1717
use ArrayAccess;
18+
use ArrayIterator;
1819
use BadMethodCallException;
1920
use function get_class;
2021
use InvalidArgumentException;
@@ -23,53 +24,73 @@
2324
use function sprintf;
2425

2526
/**
26-
* @implements ArrayAccess<string, static|scalar|null>
27-
* @implements IteratorAggregate<string, static|scalar|null>
27+
* Abstract immutable container with basic functionality to integrate easily into the driver ecosystem.
28+
*
29+
* @psalm-immutable
30+
*
31+
* @template TKey of array-key
32+
* @template TValue
33+
*
34+
* @implements ArrayAccess<TKey, TValue>
35+
* @implements IteratorAggregate<TKey, TValue>
2836
*/
29-
abstract class AbstractCypherContainer implements JsonSerializable, ArrayAccess, IteratorAggregate
37+
abstract class AbstractCypherObject implements JsonSerializable, ArrayAccess, IteratorAggregate
3038
{
31-
/** @var array<string, self|scalar|null>|null */
32-
private ?array $cachedSerialized = null;
33-
3439
/**
35-
* @return array<string, static|scalar|null>
40+
* Represents the container as an array.
41+
*
42+
* @return array<TKey, TValue>
3643
*/
37-
final public function jsonSerialize(): array
38-
{
39-
if ($this->cachedSerialized === null) {
40-
$tbr = [];
44+
abstract public function toArray(): array;
4145

42-
foreach ($this as $key => $value) {
43-
$tbr[$key] = $value;
44-
}
45-
46-
$this->cachedSerialized = $tbr;
47-
}
46+
public function jsonSerialize(): array
47+
{
48+
return $this->toArray();
49+
}
4850

49-
return $this->cachedSerialized;
51+
/**
52+
* @return ArrayIterator<TKey, TValue>
53+
*/
54+
public function getIterator(): ArrayIterator
55+
{
56+
return new ArrayIterator($this->toArray());
5057
}
5158

59+
/**
60+
* @param TKey $offset
61+
*/
5262
final public function offsetExists($offset): bool
5363
{
54-
return array_key_exists($offset, $this->jsonSerialize());
64+
return array_key_exists($offset, $this->toArray());
5565
}
5666

67+
/**
68+
* @param TKey $offset
69+
*
70+
* @return TValue
71+
*/
5772
final public function offsetGet($offset)
5873
{
59-
$serialized = $this->jsonSerialize();
74+
$serialized = $this->toArray();
6075
if (!array_key_exists($offset, $serialized)) {
6176
throw new InvalidArgumentException("Offset: $offset does not exists for class: ".static::class);
6277
}
6378

64-
/** @psalm-suppress InvalidReturnStatement */
6579
return $serialized[$offset];
6680
}
6781

82+
/**
83+
* @param TKey $offset
84+
* @param TValue $value
85+
*/
6886
final public function offsetSet($offset, $value): void
6987
{
7088
throw new BadMethodCallException(sprintf('%s is immutable', get_class($this)));
7189
}
7290

91+
/**
92+
* @param TKey $offset
93+
*/
7394
final public function offsetUnset($offset): void
7495
{
7596
throw new BadMethodCallException(sprintf('%s is immutable', get_class($this)));

src/Types/AbstractCypherSequence.php

Lines changed: 41 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,29 @@
1616
use function array_key_exists;
1717
use function array_search;
1818
use ArrayAccess;
19-
use ArrayIterator;
20-
use BadMethodCallException;
2119
use function count;
2220
use Countable;
2321
use function implode;
2422
use function in_array;
25-
use IteratorAggregate;
26-
use JsonSerializable;
2723

2824
/**
25+
* Abstract immutable sequence with basic functional methods.
26+
*
2927
* @template TKey of array-key
3028
* @template TValue
3129
*
32-
* @implements ArrayAccess<TKey, TValue>
33-
* @implements ArrayAccess<TKey, TValue>
30+
* @extends AbstractCypherObject<TKey, TValue>
3431
*
3532
* @psalm-immutable
3633
*/
37-
abstract class AbstractCypherSequence implements Countable, JsonSerializable, ArrayAccess, IteratorAggregate
34+
abstract class AbstractCypherSequence extends AbstractCypherObject implements Countable
3835
{
3936
/** @var array<TKey, TValue> */
4037
protected array $sequence;
4138

4239
/**
40+
* Creates a new instance from the given iterable.
41+
*
4342
* @template Value
4443
*
4544
* @param iterable<Value> $iterable
@@ -56,21 +55,29 @@ final public function count(): int
5655
}
5756

5857
/**
58+
* Copies the sequence.
59+
*
5960
* @return static
6061
*/
6162
final public function copy(): self
6263
{
64+
// Make sure the sequence is actually copied by reassigning it.
6365
$map = $this->sequence;
6466

6567
return $this::fromIterable($map);
6668
}
6769

70+
/**
71+
* Returns whether or not the sequence is empty.
72+
*/
6873
final public function isEmpty(): bool
6974
{
7075
return count($this->sequence) === 0;
7176
}
7277

7378
/**
79+
* Returns the sequence as an array.
80+
*
7481
* @return array<TKey, TValue>
7582
*/
7683
final public function toArray(): array
@@ -79,61 +86,17 @@ final public function toArray(): array
7986
}
8087

8188
/**
82-
* @return ArrayIterator<TKey, TValue>
83-
*/
84-
final public function getIterator(): ArrayIterator
85-
{
86-
return new ArrayIterator($this->sequence);
87-
}
88-
89-
/**
90-
* @param TKey $offset
91-
*/
92-
final public function offsetExists($offset): bool
93-
{
94-
return array_key_exists($offset, $this->sequence);
95-
}
96-
97-
/**
98-
* @param TKey $offset
89+
* Creates a new sequence by merging this one with the provided iterable. The provided values will override the existing items in case of a key collision.
9990
*
100-
* @return TValue
101-
*/
102-
final public function offsetGet($offset)
103-
{
104-
return $this->sequence[$offset];
105-
}
106-
107-
/**
108-
* @param TKey $offset
109-
* @param TValue $value
110-
*/
111-
final public function offsetSet($offset, $value)
112-
{
113-
throw new BadMethodCallException(static::class.' is immutable');
114-
}
115-
116-
/**
117-
* @param string $offset
118-
*/
119-
final public function offsetUnset($offset)
120-
{
121-
throw new BadMethodCallException(static::class.' is immutable');
122-
}
123-
124-
final public function jsonSerialize(): array
125-
{
126-
return $this->sequence;
127-
}
128-
129-
/**
13091
* @param iterable<array-key, TValue> $values
13192
*
13293
* @return static
13394
*/
13495
abstract public function merge(iterable $values): self;
13596

13697
/**
98+
* Checks if the sequence contains the given key.
99+
*
137100
* @param TKey $key
138101
*/
139102
final public function hasKey($key): bool
@@ -142,6 +105,8 @@ final public function hasKey($key): bool
142105
}
143106

144107
/**
108+
* Checks if the sequence contains the given value. The equality check is strict.
109+
*
145110
* @param TValue $value
146111
*/
147112
final public function hasValue($value): bool
@@ -150,6 +115,8 @@ final public function hasValue($value): bool
150115
}
151116

152117
/**
118+
* Creates a filtered the sequence with the provided callback.
119+
*
153120
* @param pure-callable(TKey, TValue):bool $callback
154121
*
155122
* @return static
@@ -167,6 +134,8 @@ final public function filter(callable $callback): self
167134
}
168135

169136
/**
137+
* Maps the values of this sequence to a new one with the provided callback.
138+
*
170139
* @template U
171140
*
172141
* @param pure-callable(TKey, TValue):U $callback
@@ -184,6 +153,8 @@ final public function map(callable $callback): self
184153
}
185154

186155
/**
156+
* Reduces this sequence with the given callback.
157+
*
187158
* @template TInitial
188159
*
189160
* @param pure-callable(TInitial|null, TKey, TValue):TInitial $callback
@@ -201,37 +172,47 @@ final public function reduce(callable $callback, $initial = null)
201172
}
202173

203174
/**
175+
* Finds the position of the value within the sequence.
176+
*
204177
* @param TValue $value
205178
*
206-
* @return false|TKey
179+
* @return false|TKey returns the key of the value if it is found, false otherwise
207180
*/
208181
final public function find($value)
209182
{
210183
return array_search($value, $this->sequence, true);
211184
}
212185

213186
/**
187+
* Creates a reversed sequence.
188+
*
214189
* @return static
215190
*/
216191
abstract public function reversed(): self;
217192

218193
/**
194+
* Slices a new sequence starting from the given offset with a certain length.
195+
* If the length is null it will slice the entire remainder starting from the offset.
196+
*
219197
* @return static
220198
*/
221199
abstract public function slice(int $offset, int $length = null): self;
222200

223201
/**
202+
* Creates a sorted sequence. If the compoarator is null it will use natural ordering.
203+
*
224204
* @param (pure-callable(TValue, TValue):int)|null $comparator
225205
*
226206
* @return static
227207
*/
228208
abstract public function sorted(?callable $comparator = null): self;
229209

230-
final public function __debugInfo()
231-
{
232-
return $this->sequence;
233-
}
234-
210+
/**
211+
* Joins the values within the sequence together with the provided glue. If the glue is null, it will be an empty string.
212+
*
213+
* @param string|null $glue
214+
* @return string
215+
*/
235216
public function join(?string $glue = null): string
236217
{
237218
/** @psalm-suppress MixedArgumentTypeCoercion */

0 commit comments

Comments
 (0)