Skip to content

Commit 446b5bb

Browse files
committed
PointCollection/MultiPoint: Implemented Arrayable and Jsonable and primarily using ArrayAccess for operations (#15).
(cherry picked from commit afebcaf)
1 parent d8ae366 commit 446b5bb

File tree

2 files changed

+112
-69
lines changed

2 files changed

+112
-69
lines changed

src/Types/PointCollection.php

Lines changed: 66 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
use ArrayIterator;
77
use Countable;
88
use Illuminate\Contracts\Support\Arrayable;
9+
use Illuminate\Contracts\Support\Jsonable;
910
use InvalidArgumentException;
1011
use IteratorAggregate;
1112
use JsonSerializable;
1213

13-
abstract class PointCollection implements IteratorAggregate, Arrayable, ArrayAccess, Countable, JsonSerializable
14+
abstract class PointCollection implements IteratorAggregate, ArrayAccess, Arrayable, Countable, Jsonable, JsonSerializable
1415
{
1516
/**
1617
* @var Point[]
@@ -36,11 +37,6 @@ public function __construct(array $points)
3637
$this->points = $points;
3738
}
3839

39-
public function getPoints()
40-
{
41-
return $this->points;
42-
}
43-
4440
public function toArray()
4541
{
4642
return $this->points;
@@ -51,48 +47,24 @@ public function getIterator()
5147
return new ArrayIterator($this->points);
5248
}
5349

54-
public function prependPoint(Point $point)
55-
{
56-
array_unshift($this->points, $point);
57-
}
58-
59-
public function appendPoint(Point $point)
60-
{
61-
$this->points[] = $point;
62-
}
63-
64-
public function insertPoint($index, Point $point)
65-
{
66-
if (count($this->points) - 1 < $index) {
67-
throw new InvalidArgumentException('$index is greater than the size of the array');
68-
}
69-
70-
array_splice($this->points, $index, 0, [$point]);
71-
}
72-
7350
public function offsetExists($offset)
7451
{
7552
return isset($this->points[$offset]);
7653
}
7754

78-
/**
79-
* @param mixed $offset
80-
*
81-
* @return null|Point
82-
*/
8355
public function offsetGet($offset)
8456
{
8557
return $this->offsetExists($offset) ? $this->points[$offset] : null;
8658
}
8759

8860
public function offsetSet($offset, $value)
8961
{
90-
if (!($value instanceof Point)) {
62+
if (! ($value instanceof Point)) {
9163
throw new InvalidArgumentException('$value must be an instance of Point');
9264
}
9365

9466
if (is_null($offset)) {
95-
$this->appendPoint($value);
67+
$this->points[] = $value;
9668
} else {
9769
$this->points[$offset] = $value;
9870
}
@@ -108,10 +80,72 @@ public function count()
10880
return count($this->points);
10981
}
11082

83+
public function toJson($options = 0)
84+
{
85+
return json_encode($this, $options);
86+
}
87+
11188
public function toPairList()
11289
{
11390
return implode(',', array_map(function (Point $point) {
11491
return $point->toPair();
11592
}, $this->points));
11693
}
94+
95+
/**
96+
* @return array|\Grimzy\LaravelMysqlSpatial\Types\Point[]
97+
*
98+
* @deprecated 2.1.0 Use $multipoint->toArray() instead
99+
*
100+
* @see IteratorAggregate
101+
* @see ArrayAccess
102+
* @see Arrayable
103+
*/
104+
public function getPoints()
105+
{
106+
return $this->points;
107+
}
108+
109+
/**
110+
* @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
111+
*
112+
* @deprecated 2.1.0 Use array_unshift($multipoint, $point); instead
113+
*
114+
* @see array_unshift
115+
* @see ArrayAccess
116+
*/
117+
public function prependPoint(Point $point)
118+
{
119+
array_unshift($this->points, $point);
120+
}
121+
122+
/**
123+
* @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
124+
*
125+
* @deprecated 2.1.0 Use $multipoint[] = $point; instead
126+
*
127+
* @see ArrayAccess
128+
*/
129+
public function appendPoint(Point $point)
130+
{
131+
$this->points[] = $point;
132+
}
133+
134+
/**
135+
* @param $index
136+
* @param \Grimzy\LaravelMysqlSpatial\Types\Point $point
137+
*
138+
* @deprecated 2.1.0 Use array_splice($multipoint, $index, 0, [$point]); instead
139+
*
140+
* @see array_splice
141+
* @see ArrayAccess
142+
*/
143+
public function insertPoint($index, Point $point)
144+
{
145+
if (count($this->points) - 1 < $index) {
146+
throw new InvalidArgumentException('$index is greater than the size of the array');
147+
}
148+
149+
array_splice($this->points, $index, 0, [$point]);
150+
}
117151
}

tests/Unit/Types/MultiPointTest.php

Lines changed: 46 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,41 @@ public function testArrayAccess()
7171
$multipoint[] = 1;
7272
}
7373

74-
public function testPrependPoint()
74+
public function testToJson()
75+
{
76+
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
77+
78+
$multipoint = new MultiPoint($collection);
79+
80+
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', $multipoint->toJson());
81+
}
82+
83+
public function testJsonSerialize()
84+
{
85+
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
86+
87+
$multipoint = new MultiPoint($collection);
88+
89+
$this->assertInstanceOf(\GeoJson\Geometry\MultiPoint::class, $multipoint->jsonSerialize());
90+
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', json_encode($multipoint));
91+
}
92+
93+
public function testInvalidArgumentExceptionAtLeastOneEntry()
94+
{
95+
$this->assertException(InvalidArgumentException::class);
96+
$multipoint = new MultiPoint([]);
97+
}
98+
99+
public function testInvalidArgumentExceptionNotArrayOfLineString()
100+
{
101+
$this->assertException(InvalidArgumentException::class);
102+
$multipoint = new MultiPoint([
103+
new Point(0, 0),
104+
1,
105+
]);
106+
}
107+
108+
public function testDeprecatedPrependPoint()
75109
{
76110
$point1 = new Point(1, 1);
77111
$point2 = new Point(2, 2);
@@ -80,12 +114,12 @@ public function testPrependPoint()
80114
$point0 = new Point(0, 0);
81115
$multipoint->prependPoint($point0);
82116

83-
$this->assertEquals($point0, $multipoint->getPoints()[0]);
84-
$this->assertEquals($point1, $multipoint->getPoints()[1]);
85-
$this->assertEquals($point2, $multipoint->getPoints()[2]);
117+
$this->assertEquals($point0, $multipoint[0]);
118+
$this->assertEquals($point1, $multipoint[1]);
119+
$this->assertEquals($point2, $multipoint[2]);
86120
}
87121

88-
public function testAppendPoint()
122+
public function testDeprecatedAppendPoint()
89123
{
90124
$point0 = new Point(0, 0);
91125
$point1 = new Point(1, 1);
@@ -94,12 +128,12 @@ public function testAppendPoint()
94128
$point2 = new Point(2, 2);
95129
$multipoint->appendPoint($point2);
96130

97-
$this->assertEquals($point0, $multipoint->getPoints()[0]);
98-
$this->assertEquals($point1, $multipoint->getPoints()[1]);
99-
$this->assertEquals($point2, $multipoint->getPoints()[2]);
131+
$this->assertEquals($point0, $multipoint[0]);
132+
$this->assertEquals($point1, $multipoint[1]);
133+
$this->assertEquals($point2, $multipoint[2]);
100134
}
101135

102-
public function testInsertPoint()
136+
public function testDeprecatedInsertPoint()
103137
{
104138
$point1 = new Point(1, 1);
105139
$point3 = new Point(3, 3);
@@ -108,36 +142,11 @@ public function testInsertPoint()
108142
$point2 = new Point(2, 2);
109143
$multipoint->insertPoint(1, $point2);
110144

111-
$this->assertEquals($point1, $multipoint->getPoints()[0]);
112-
$this->assertEquals($point2, $multipoint->getPoints()[1]);
113-
$this->assertEquals($point3, $multipoint->getPoints()[2]);
145+
$this->assertEquals($point1, $multipoint[0]);
146+
$this->assertEquals($point2, $multipoint[1]);
147+
$this->assertEquals($point3, $multipoint[2]);
114148

115149
$this->assertException(InvalidArgumentException::class);
116150
$multipoint->insertPoint(100, new Point(100, 100));
117151
}
118-
119-
public function testJsonSerialize()
120-
{
121-
$collection = [new Point(0, 0), new Point(0, 1), new Point(1, 1)];
122-
123-
$multipoint = new MultiPoint($collection);
124-
125-
$this->assertInstanceOf(\GeoJson\Geometry\MultiPoint::class, $multipoint->jsonSerialize());
126-
$this->assertSame('{"type":"MultiPoint","coordinates":[[0,0],[1,0],[1,1]]}', json_encode($multipoint));
127-
}
128-
129-
public function testInvalidArgumentExceptionAtLeastOneEntry()
130-
{
131-
$this->assertException(InvalidArgumentException::class);
132-
$multipoint = new MultiPoint([]);
133-
}
134-
135-
public function testInvalidArgumentExceptionNotArrayOfLineString()
136-
{
137-
$this->assertException(InvalidArgumentException::class);
138-
$multipoint = new MultiPoint([
139-
new Point(0, 0),
140-
1,
141-
]);
142-
}
143152
}

0 commit comments

Comments
 (0)