Skip to content

Commit 6006a32

Browse files
committed
made all values array-like
1 parent 32ca84f commit 6006a32

18 files changed

+189
-136
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Laudis Neo4j package.
7+
*
8+
* (c) Laudis technologies <http://laudis.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Contracts;
15+
16+
use ArrayAccess;
17+
use IteratorAggregate;
18+
use JsonSerializable;
19+
20+
/**
21+
* @template TKey
22+
* @template TValue
23+
*
24+
* @template-extends ArrayAccess<TKey, TValue>
25+
* @template-extends IteratorAggregate<TKey, TValue>
26+
*/
27+
interface CypherContainerInterface extends JsonSerializable, ArrayAccess, IteratorAggregate
28+
{
29+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/*
6+
* This file is part of the Laudis Neo4j package.
7+
*
8+
* (c) Laudis technologies <http://laudis.tech>
9+
*
10+
* For the full copyright and license information, please view the LICENSE
11+
* file that was distributed with this source code.
12+
*/
13+
14+
namespace Laudis\Neo4j\Types;
15+
16+
use BadMethodCallException;
17+
use function get_class;
18+
use InvalidArgumentException;
19+
use Laudis\Neo4j\Contracts\CypherContainerInterface;
20+
use function sprintf;
21+
22+
/**
23+
* @implements CypherContainerInterface<string, CypherContainerInterface|scalar|null>
24+
*/
25+
abstract class AbstractCypherContainer implements CypherContainerInterface
26+
{
27+
public function jsonSerialize()
28+
{
29+
$tbr = [];
30+
31+
foreach ($this as $key => $value) {
32+
$tbr[$key] = $value;
33+
}
34+
35+
return $tbr;
36+
}
37+
38+
/**
39+
* @psalm-suppress UnusedVariable
40+
*/
41+
public function offsetExists($offset): bool
42+
{
43+
foreach ($this as $key => $value) {
44+
if ($key === $offset) {
45+
return true;
46+
}
47+
}
48+
49+
return false;
50+
}
51+
52+
public function offsetGet($offset)
53+
{
54+
foreach ($this as $key => $value) {
55+
if ($key === $offset) {
56+
return $value;
57+
}
58+
}
59+
60+
throw new InvalidArgumentException("Offset: $offset does not exists for class: ".static::class);
61+
}
62+
63+
public function offsetSet($offset, $value): void
64+
{
65+
throw new BadMethodCallException(sprintf('%s is immutable', get_class($this)));
66+
}
67+
68+
public function offsetUnset($offset): void
69+
{
70+
throw new BadMethodCallException(sprintf('%s is immutable', get_class($this)));
71+
}
72+
}

src/Types/Cartesian3DPoint.php

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16-
use JsonSerializable;
1716
use Laudis\Neo4j\Contracts\PointInterface;
1817

19-
final class Cartesian3DPoint implements PointInterface, JsonSerializable
18+
final class Cartesian3DPoint extends AbstractCypherContainer implements PointInterface
2019
{
2120
private float $z;
2221
private float $x;
@@ -62,14 +61,12 @@ public function getSrid(): int
6261
return $this->srid;
6362
}
6463

65-
public function jsonSerialize()
64+
public function getIterator()
6665
{
67-
return [
68-
'x' => $this->getX(),
69-
'y' => $this->getY(),
70-
'z' => $this->getZ(),
71-
'crs' => $this->getCrs(),
72-
'srid' => $this->getSrid(),
73-
];
66+
yield 'x' => $this->getX();
67+
yield 'y' => $this->getY();
68+
yield 'z' => $this->getZ();
69+
yield 'crs' => $this->getCrs();
70+
yield 'srid' => $this->getSrid();
7471
}
7572
}

src/Types/CartesianPoint.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,9 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16-
use JsonSerializable;
1716
use Laudis\Neo4j\Contracts\PointInterface;
1817

19-
final class CartesianPoint implements PointInterface, JsonSerializable
18+
final class CartesianPoint extends AbstractCypherContainer implements PointInterface
2019
{
2120
private float $x;
2221
private float $y;
@@ -55,13 +54,11 @@ public function getSrid(): int
5554
return $this->srid;
5655
}
5756

58-
public function jsonSerialize()
57+
public function getIterator()
5958
{
60-
return [
61-
'x' => $this->getX(),
62-
'y' => $this->getY(),
63-
'crs' => $this->getCrs(),
64-
'srid' => $this->getSrid(),
65-
];
59+
yield 'x' => $this->getX();
60+
yield 'y' => $this->getY();
61+
yield 'crs' => $this->getCrs();
62+
yield 'srid' => $this->getSrid();
6663
}
6764
}

src/Types/CypherList.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,17 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16-
use ArrayAccess;
1716
use BadMethodCallException;
1817
use Ds\Vector;
19-
use Generator;
20-
use IteratorAggregate;
21-
use JsonSerializable;
18+
use Laudis\Neo4j\Contracts\CypherContainerInterface;
2219
use Traversable;
2320

2421
/**
2522
* @template T
2623
*
27-
* @implements IteratorAggregate<T>
28-
* @implements ArrayAccess<int, T>
24+
* @implements CypherContainerInterface<int, T>
2925
*/
30-
final class CypherList implements IteratorAggregate, ArrayAccess, JsonSerializable
26+
final class CypherList implements CypherContainerInterface
3127
{
3228
/** @var Vector<T> */
3329
private Vector $vector;
@@ -66,11 +62,9 @@ public function toArray(): array
6662
return $this->vector->toArray();
6763
}
6864

69-
/**
70-
* @return Generator<T>|Traversable<T>
71-
*/
7265
public function getIterator()
7366
{
67+
/** @var Traversable<int, T> */
7468
return $this->vector->getIterator();
7569
}
7670

src/Types/CypherMap.php

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,21 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16-
use ArrayAccess;
1716
use BadMethodCallException;
1817
use Ds\Map;
1918
use Ds\Pair;
2019
use Ds\Sequence;
2120
use Ds\Set;
22-
use IteratorAggregate;
23-
use JsonSerializable;
21+
use Laudis\Neo4j\Contracts\CypherContainerInterface;
2422
use OutOfBoundsException;
2523
use Traversable;
2624

2725
/**
2826
* @template T
2927
*
30-
* @implements ArrayAccess<string, T>
31-
* @implements IteratorAggregate<T>
28+
* @implements CypherContainerInterface<string, T>
3229
*/
33-
final class CypherMap implements ArrayAccess, IteratorAggregate, JsonSerializable
30+
final class CypherMap implements CypherContainerInterface
3431
{
3532
/** @var Map<string, T> */
3633
private Map $map;
@@ -69,8 +66,9 @@ public function toArray(): array
6966
return $this->map->toArray();
7067
}
7168

72-
public function getIterator(): Traversable
69+
public function getIterator()
7370
{
71+
/** @var Traversable<string, T> */
7472
return $this->map->getIterator();
7573
}
7674

src/Types/Date.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,8 @@
1515

1616
use DateTimeImmutable;
1717
use Exception;
18-
use JsonSerializable;
1918

20-
final class Date implements JsonSerializable
19+
final class Date extends AbstractCypherContainer
2120
{
2221
private int $days;
2322

@@ -39,8 +38,8 @@ public function toDateTime(): DateTimeImmutable
3938
return (new DateTimeImmutable('@0'))->modify(sprintf('+%s days', $this->days));
4039
}
4140

42-
public function jsonSerialize()
41+
public function getIterator()
4342
{
44-
return ['days' => $this->days];
43+
yield 'days' => $this->days;
4544
}
4645
}

src/Types/DateTime.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,10 @@
1616
use DateTimeImmutable;
1717
use DateTimeZone;
1818
use Exception;
19-
use JsonSerializable;
2019
use Laudis\Neo4j\Exception\TimezoneOffsetException;
2120
use function sprintf;
2221

23-
final class DateTime implements JsonSerializable
22+
final class DateTime extends AbstractCypherContainer
2423
{
2524
private int $seconds;
2625
private int $nanoseconds;
@@ -67,12 +66,10 @@ public function toDateTime(): DateTimeImmutable
6766
throw new TimezoneOffsetException($message);
6867
}
6968

70-
public function jsonSerialize()
69+
public function getIterator()
7170
{
72-
return [
73-
'seconds' => $this->seconds,
74-
'nanoseconds' => $this->nanoseconds,
75-
'tzOffsetSeconds' => $this->tzOffsetSeconds,
76-
];
71+
yield 'seconds' => $this->seconds;
72+
yield 'nanoseconds' => $this->nanoseconds;
73+
yield 'tzOffsetSeconds' => $this->tzOffsetSeconds;
7774
}
7875
}

src/Types/Duration.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,12 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16+
use ArrayIterator;
1617
use DateInterval;
1718
use Exception;
18-
use JsonSerializable;
19+
use Traversable;
1920

20-
final class Duration implements JsonSerializable
21+
final class Duration extends AbstractCypherContainer
2122
{
2223
private int $months;
2324
private int $days;
@@ -60,13 +61,11 @@ public function toDateInterval(): DateInterval
6061
return new DateInterval(sprintf('P%dM%dDT%dS', $this->months, $this->days, $this->seconds));
6162
}
6263

63-
public function jsonSerialize()
64+
public function getIterator()
6465
{
65-
return [
66-
'months' => $this->months,
67-
'days' => $this->days,
68-
'seconds' => $this->seconds,
69-
'nanoseconds' => $this->nanoseconds,
70-
];
66+
yield 'months' => $this->months;
67+
yield 'days' => $this->days;
68+
yield 'seconds' => $this->seconds;
69+
yield 'nanoseconds' => $this->nanoseconds;
7170
}
7271
}

src/Types/LocalDateTime.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,9 @@
1515

1616
use DateTimeImmutable;
1717
use Exception;
18-
use JsonSerializable;
1918
use function sprintf;
2019

21-
final class LocalDateTime implements JsonSerializable
20+
final class LocalDateTime extends AbstractCypherContainer
2221
{
2322
private int $seconds;
2423
private int $nanoseconds;
@@ -48,8 +47,9 @@ public function toDateTime(): DateTimeImmutable
4847
->modify(sprintf('+%s microseconds', $this->nanoseconds / 1000));
4948
}
5049

51-
public function jsonSerialize()
50+
public function getIterator()
5251
{
53-
return ['seconds' => $this->seconds, 'nanoseconds' => $this->nanoseconds];
52+
yield 'seconds' => $this->seconds;
53+
yield 'nanoseconds' => $this->nanoseconds;
5454
}
5555
}

0 commit comments

Comments
 (0)