Skip to content

Commit 41269db

Browse files
authored
Merge pull request #115 from wgevaert/main
Refactor point types Fixes #114
2 parents 527e7aa + ec747ff commit 41269db

File tree

8 files changed

+214
-181
lines changed

8 files changed

+214
-181
lines changed

src/Formatter/Specialised/BoltOGMTranslator.php

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
use Bolt\structures\Time as BoltTime;
2828
use Bolt\structures\UnboundRelationship as BoltUnboundRelationship;
2929
use function call_user_func;
30+
use Laudis\Neo4j\Types\Abstract3DPoint;
31+
use Laudis\Neo4j\Types\AbstractPoint;
3032
use Laudis\Neo4j\Types\Cartesian3DPoint;
3133
use Laudis\Neo4j\Types\CartesianPoint;
3234
use Laudis\Neo4j\Types\CypherList;
@@ -42,6 +44,8 @@
4244
use Laudis\Neo4j\Types\Relationship;
4345
use Laudis\Neo4j\Types\Time;
4446
use Laudis\Neo4j\Types\UnboundRelationship;
47+
use Laudis\Neo4j\Types\WGS843DPoint;
48+
use Laudis\Neo4j\Types\WGS84Point;
4549
use UnexpectedValueException;
4650

4751
/**
@@ -185,14 +189,24 @@ private function makeFromBoltUnboundRelationship(BoltUnboundRelationship $rel):
185189
);
186190
}
187191

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

193-
private function makeFromBoltPoint3D(BoltPoint3D $x): Cartesian3DPoint
202+
private function makeFromBoltPoint3D(BoltPoint3D $x): Abstract3DPoint
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('An srid of '.$x->srid().' has been returned, which has not been implemented.');
196210
}
197211

198212
private function makeFromBoltPath(BoltPath $path): Path

src/Formatter/Specialised/HttpOGMTranslator.php

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
use stdClass;
4343
use function str_pad;
4444
use function substr;
45+
use UnexpectedValueException;
4546

4647
/**
4748
* @psalm-immutable
@@ -278,50 +279,39 @@ private function translatePoint(stdClass $value): PointInterface
278279
{
279280
/** @var stdClass $crs */
280281
$crs = $value->crs;
281-
/** @var "cartesian"|"cartesian-3d"|"wgs-84"|"wgs-84-3d" */
282-
$name = $crs->name;
283282
/** @var array{0: float, 1: float, 2:float} $coordinates */
284283
$coordinates = $value->coordinates;
285284
/** @var int $srid */
286285
$srid = $crs->srid;
287-
if ($name === 'cartesian') {
286+
if ($srid === CartesianPoint::SRID) {
288287
return new CartesianPoint(
289288
$coordinates[0],
290289
$coordinates[1],
291-
$name,
292-
$srid
293290
);
294291
}
295-
if ($name === 'cartesian-3d') {
292+
if ($srid === Cartesian3DPoint::SRID) {
296293
return new Cartesian3DPoint(
297294
$coordinates[0],
298295
$coordinates[1],
299296
$coordinates[2],
300-
$name,
301-
$srid
302297
);
303298
}
304-
if ($name === 'wgs-84') {
299+
if ($srid === WGS84Point::SRID) {
305300
return new WGS84Point(
306301
$coordinates[0],
307302
$coordinates[1],
303+
);
304+
}
305+
if ($srid === WGS843DPoint::SRID) {
306+
return new WGS843DPoint(
308307
$coordinates[0],
309308
$coordinates[1],
310-
$name,
311-
$srid
309+
$coordinates[2],
312310
);
313311
}
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
324-
);
312+
/** @var string $name */
313+
$name = $crs->name;
314+
throw new UnexpectedValueException('A point with srid '.$srid.' and name '.$name.' has been returned, which has not been implemented.');
325315
}
326316

327317
/**

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);
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 & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@
1313

1414
namespace Laudis\Neo4j\Types;
1515

16-
use Bolt\structures\IStructure;
17-
use Bolt\structures\Point3D;
1816
use Laudis\Neo4j\Contracts\BoltConvertibleInterface;
1917
use Laudis\Neo4j\Contracts\PointInterface;
2018

@@ -27,38 +25,18 @@
2725
*
2826
* @psalm-import-type Crs from \Laudis\Neo4j\Contracts\PointInterface
2927
*/
30-
class Cartesian3DPoint extends CartesianPoint implements PointInterface, BoltConvertibleInterface
28+
final class Cartesian3DPoint extends Abstract3DPoint implements PointInterface, BoltConvertibleInterface
3129
{
32-
private float $z;
30+
public const SRID = 9157;
31+
public const CRS = 'cartesian-3d';
3332

34-
public function convertToBolt(): IStructure
33+
public function getSrid(): int
3534
{
36-
return new Point3D($this->getSrid(), $this->getX(), $this->getY(), $this->getZ());
35+
return self::SRID;
3736
}
3837

39-
/**
40-
* @param Crs $crs
41-
*/
42-
public function __construct(float $x, float $y, float $z, string $crs, int $srid)
38+
public function getCrs(): string
4339
{
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;
40+
return self::CRS;
6341
}
6442
}

0 commit comments

Comments
 (0)