Skip to content

Commit b025025

Browse files
author
Wout Gevaert
committed
Refactor point types
This has some backwards compatibility breaks in how WGS84 points' toArray behaves. If you do not like that, it could be made backwards compatible, but I like it this way
1 parent 527e7aa commit b025025

File tree

8 files changed

+208
-177
lines changed

8 files changed

+208
-177
lines changed

src/Formatter/Specialised/BoltOGMTranslator.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
use Laudis\Neo4j\Types\Relationship;
4343
use Laudis\Neo4j\Types\Time;
4444
use Laudis\Neo4j\Types\UnboundRelationship;
45+
use Laudis\Neo4j\Types\WGS843DPoint;
46+
use Laudis\Neo4j\Types\WGS84Point;
4547
use UnexpectedValueException;
4648

4749
/**
@@ -187,12 +189,26 @@ private function makeFromBoltUnboundRelationship(BoltUnboundRelationship $rel):
187189

188190
private function makeFromBoltPoint2D(BoltPoint2d $x): CartesianPoint
189191
{
190-
return new CartesianPoint($x->x(), $x->y(), 'cartesian', $x->srid());
192+
if ($x->srid() === CartesianPoint::SRID) {
193+
return new CartesianPoint($x->x(), $x->y());
194+
} elseif ($x->srid() === WGS84Point::SRID) {
195+
return new WGS84Point($x->x(), $x->y());
196+
}
197+
throw new UnexpectedValueException(
198+
'An srid of ' . $x->srid() . ' has been returned, which has not been implemented.'
199+
);
191200
}
192201

193202
private function makeFromBoltPoint3D(BoltPoint3D $x): Cartesian3DPoint
194203
{
195-
return new Cartesian3DPoint($x->x(), $x->y(), $x->z(), 'cartesian', $x->srid());
204+
if ($x->srid() === Cartesian3DPoint::SRID) {
205+
return new Cartesian3DPoint($x->x(), $x->y(), $x->z());
206+
} elseif ($x->srid() === WGS843DPoint::SRID) {
207+
return new WGS843DPoint($x->x(), $x->y(), $x->z());
208+
}
209+
throw new UnexpectedValueException(
210+
'An srid of ' . $x->srid() . ' has been returned, which has not been implemented.'
211+
);
196212
}
197213

198214
private function makeFromBoltPath(BoltPath $path): Path

src/Formatter/Specialised/HttpOGMTranslator.php

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -278,49 +278,38 @@ private function translatePoint(stdClass $value): PointInterface
278278
{
279279
/** @var stdClass $crs */
280280
$crs = $value->crs;
281-
/** @var "cartesian"|"cartesian-3d"|"wgs-84"|"wgs-84-3d" */
282-
$name = $crs->name;
283281
/** @var array{0: float, 1: float, 2:float} $coordinates */
284282
$coordinates = $value->coordinates;
285283
/** @var int $srid */
286284
$srid = $crs->srid;
287-
if ($name === 'cartesian') {
285+
if ($srid === CartesianPoint::SRID) {
288286
return new CartesianPoint(
289287
$coordinates[0],
290288
$coordinates[1],
291-
$name,
292-
$srid
293289
);
294290
}
295-
if ($name === 'cartesian-3d') {
291+
if ($srid === Cartesian3DPoint::SRID) {
296292
return new Cartesian3DPoint(
297293
$coordinates[0],
298294
$coordinates[1],
299295
$coordinates[2],
300-
$name,
301-
$srid
302296
);
303297
}
304-
if ($name === 'wgs-84') {
298+
if ($srid === WGS84Point::SRID) {
305299
return new WGS84Point(
306300
$coordinates[0],
307301
$coordinates[1],
302+
);
303+
}
304+
if ($srid === WGS843DPoint::SRID) {
305+
return new WGS843DPoint(
308306
$coordinates[0],
309307
$coordinates[1],
310-
$name,
311-
$srid
308+
$coordinates[2],
312309
);
313310
}
314-
315-
return new WGS843DPoint(
316-
$coordinates[0],
317-
$coordinates[1],
318-
$coordinates[2],
319-
$coordinates[0],
320-
$coordinates[1],
321-
$coordinates[2],
322-
$name,
323-
$srid
311+
throw new UnexpectedValueException(
312+
'A point with srid ' . $srid . ' and name ' . $crs->name . ' has been returned, which has not been implemented.'
324313
);
325314
}
326315

src/Types/Abstract3DPoint.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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 Bolt\structures\IStructure;
17+
use Bolt\structures\Point3D;
18+
use Laudis\Neo4j\Contracts\BoltConvertibleInterface;
19+
use Laudis\Neo4j\Contracts\PointInterface;
20+
21+
/**
22+
* A cartesian point in three dimensional space.
23+
*
24+
* @see https://neo4j.com/docs/cypher-manual/current/functions/spatial/#functions-point-cartesian-3d
25+
*
26+
* @psalm-immutable
27+
*
28+
* @psalm-import-type Crs from \Laudis\Neo4j\Contracts\PointInterface
29+
*/
30+
abstract class Abstract3DPoint extends AbstractPoint implements PointInterface, BoltConvertibleInterface
31+
{
32+
private float $z;
33+
34+
public function convertToBolt(): IStructure
35+
{
36+
return new Point3D($this->getSrid(), $this->getX(), $this->getY(), $this->getZ());
37+
}
38+
39+
/**
40+
* @param Crs $crs
41+
*/
42+
public function __construct(float $x, float $y, float $z)
43+
{
44+
parent::__construct($x, $y, $crs, $srid);
45+
$this->z = $z;
46+
}
47+
48+
public function getZ(): float
49+
{
50+
return $this->z;
51+
}
52+
53+
/**
54+
* @return array{x: float, y: float, z: float, srid: int, crs: Crs}
55+
*/
56+
public function toArray(): array
57+
{
58+
$tbr = parent::toArray();
59+
60+
$tbr['z'] = $this->z;
61+
62+
return $tbr;
63+
}
64+
}

src/Types/AbstractPoint.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
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 Bolt\structures\IStructure;
17+
use Bolt\structures\Point2D;
18+
use Laudis\Neo4j\Contracts\BoltConvertibleInterface;
19+
use Laudis\Neo4j\Contracts\PointInterface;
20+
21+
/**
22+
* A cartesian point in two dimensional space.
23+
*
24+
* @see https://neo4j.com/docs/cypher-manual/current/functions/spatial/#functions-point-cartesian-2d
25+
*
26+
* @psalm-immutable
27+
*
28+
* @psalm-import-type Crs from \Laudis\Neo4j\Contracts\PointInterface
29+
*/
30+
abstract class AbstractPoint extends AbstractPropertyObject implements PointInterface, BoltConvertibleInterface
31+
{
32+
private float $x;
33+
private float $y;
34+
35+
/**
36+
* @param Crs $crs
37+
*/
38+
public function __construct(float $x, float $y)
39+
{
40+
$this->x = $x;
41+
$this->y = $y;
42+
}
43+
44+
abstract public function getCrs(): string;
45+
46+
abstract public function getSrid(): int;
47+
48+
public function convertToBolt(): IStructure
49+
{
50+
return new Point2D($this->getSrid(), $this->getX(), $this->getY());
51+
}
52+
53+
public function getX(): float
54+
{
55+
return $this->x;
56+
}
57+
58+
public function getY(): float
59+
{
60+
return $this->y;
61+
}
62+
63+
public function getProperties(): CypherMap
64+
{
65+
/** @psalm-suppress InvalidReturnStatement False positive */
66+
return new CypherMap($this);
67+
}
68+
69+
/**
70+
* @psalm-suppress ImplementedReturnTypeMismatch False positive
71+
*
72+
* @return array{x: float, y: float, crs: Crs, srid: int}
73+
*/
74+
public function toArray(): array
75+
{
76+
return [
77+
'x' => $this->x,
78+
'y' => $this->y,
79+
'crs' => $this->getCrs(),
80+
'srid' => $this->getSrid(),
81+
];
82+
}
83+
}

src/Types/Cartesian3DPoint.php

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -27,38 +27,18 @@
2727
*
2828
* @psalm-import-type Crs from \Laudis\Neo4j\Contracts\PointInterface
2929
*/
30-
class Cartesian3DPoint extends CartesianPoint implements PointInterface, BoltConvertibleInterface
30+
final class Cartesian3DPoint extends Abstract3DPoint implements PointInterface, BoltConvertibleInterface
3131
{
32-
private float $z;
32+
public const SRID = 9157;
33+
public const CRS = 'cartesian-3d';
3334

34-
public function convertToBolt(): IStructure
35+
public function getSrid(): int
3536
{
36-
return new Point3D($this->getSrid(), $this->getX(), $this->getY(), $this->getZ());
37+
return self::SRID;
3738
}
3839

39-
/**
40-
* @param Crs $crs
41-
*/
42-
public function __construct(float $x, float $y, float $z, string $crs, int $srid)
40+
public function getCrs(): string
4341
{
44-
parent::__construct($x, $y, $crs, $srid);
45-
$this->z = $z;
46-
}
47-
48-
public function getZ(): float
49-
{
50-
return $this->z;
51-
}
52-
53-
/**
54-
* @return array{x: float, y: float, z: float, srid: int, crs: Crs}
55-
*/
56-
public function toArray(): array
57-
{
58-
$tbr = parent::toArray();
59-
60-
$tbr['z'] = $this->z;
61-
62-
return $tbr;
42+
return self::CRS;
6343
}
6444
}

src/Types/CartesianPoint.php

Lines changed: 5 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -27,68 +27,19 @@
2727
*
2828
* @psalm-import-type Crs from \Laudis\Neo4j\Contracts\PointInterface
2929
*/
30-
class CartesianPoint extends AbstractPropertyObject implements PointInterface, BoltConvertibleInterface
30+
final class CartesianPoint extends AbstractPoint implements PointInterface, BoltConvertibleInterface
3131
{
32-
private float $x;
33-
private float $y;
3432
/** @var Crs */
35-
private string $crs;
36-
private int $srid;
37-
38-
/**
39-
* @param Crs $crs
40-
*/
41-
public function __construct(float $x, float $y, string $crs, int $srid)
42-
{
43-
$this->x = $x;
44-
$this->y = $y;
45-
$this->crs = $crs;
46-
$this->srid = $srid;
47-
}
48-
49-
public function convertToBolt(): IStructure
50-
{
51-
return new Point2D($this->getSrid(), $this->getX(), $this->getY());
52-
}
53-
54-
public function getX(): float
55-
{
56-
return $this->x;
57-
}
58-
59-
public function getY(): float
60-
{
61-
return $this->y;
62-
}
33+
public const CRS = 'cartesian';
34+
public const SRID = 7203;
6335

6436
public function getCrs(): string
6537
{
66-
return $this->crs;
38+
return self::CRS;
6739
}
6840

6941
public function getSrid(): int
7042
{
71-
return $this->srid;
72-
}
73-
74-
public function getProperties(): CypherMap
75-
{
76-
/** @psalm-suppress InvalidReturnStatement False positive */
77-
return new CypherMap($this);
78-
}
79-
80-
/**
81-
* @psalm-suppress ImplementedReturnTypeMismatch False positive
82-
*
83-
* @return array{x: float, y: float, crs: Crs, srid: int}
84-
*/
85-
public function toArray(): array
86-
{
87-
return [
88-
'x' => $this->x,
89-
'y' => $this->y,
90-
'crs' => $this->crs,
91-
'srid' => $this->srid,
92-
];
43+
return self::SRID;
9344
}
9445
}

0 commit comments

Comments
 (0)