Skip to content

Commit 27f9578

Browse files
committed
ArrayAccess
1 parent 46d891d commit 27f9578

12 files changed

+276
-171
lines changed

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ parameters:
66
- tests
77
level: max
88
ignoreErrors:
9-
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\.+::getGeometries\(\) should return array<.+> but returns array<MatanYadaev\\EloquentSpatial\\Objects\\Geometry>\.#'
109
- '#Method MatanYadaev\\EloquentSpatial\\Objects\\(Geometry|GeometryCollection)::(toJson|toFeatureCollectionJson)\(\) should return string but returns string\|false\.#'
1110
excludePaths:
1211
- ./src/Factory.php

src/Objects/GeometryCollection.php

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@
44

55
namespace MatanYadaev\EloquentSpatial\Objects;
66

7+
use ArrayAccess;
78
use Illuminate\Database\Query\Expression;
89
use Illuminate\Support\Collection;
910
use Illuminate\Support\Facades\DB;
1011
use Illuminate\Support\Str;
1112
use InvalidArgumentException;
1213

13-
class GeometryCollection extends Geometry
14+
class GeometryCollection extends Geometry implements ArrayAccess
1415
{
1516
/** @var Collection<Geometry> */
1617
protected Collection $geometries;
@@ -71,11 +72,11 @@ public function toArray(): array
7172
}
7273

7374
/**
74-
* @return array<Geometry>
75+
* @return Collection<Geometry>
7576
*/
76-
public function getGeometries(): array
77+
public function getGeometries(): Collection
7778
{
78-
return $this->geometries->all();
79+
return $this->geometries->collect();
7980
}
8081

8182
/**
@@ -120,4 +121,41 @@ protected function toCollectionWkt(): Expression
120121

121122
return DB::raw($wkb);
122123
}
124+
125+
/**
126+
* @param mixed $offset
127+
* @return bool
128+
*/
129+
public function offsetExists($offset): bool
130+
{
131+
return isset($this->geometries[$offset]);
132+
}
133+
134+
/**
135+
* @param mixed $offset
136+
* @return Geometry
137+
*/
138+
public function offsetGet($offset): Geometry
139+
{
140+
return $this->geometries[$offset];
141+
}
142+
143+
/**
144+
* @param mixed $offset
145+
* @param Geometry $geometry
146+
*/
147+
public function offsetSet($offset, $geometry): void
148+
{
149+
$this->geometries[$offset] = $geometry;
150+
$this->validateGeometriesType();
151+
}
152+
153+
/**
154+
* @param mixed $offset
155+
*/
156+
public function offsetUnset($offset): void
157+
{
158+
$this->geometries->splice($offset, 1);
159+
$this->validateGeometriesCount();
160+
}
123161
}

src/Objects/LineString.php

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,4 @@ public function toWkt(): Expression
1515
{
1616
return DB::raw("LINESTRING({$this->toCollectionWkt()})");
1717
}
18-
19-
/**
20-
* @return array<Point>
21-
*/
22-
public function getGeometries(): array
23-
{
24-
return parent::getGeometries();
25-
}
2618
}

src/Objects/MultiLineString.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88
use Illuminate\Support\Collection;
99
use Illuminate\Support\Facades\DB;
1010

11+
/**
12+
* @method array<LineString> getGeometries()
13+
* @method LineString offsetGet(mixed $offset)
14+
*/
1115
class MultiLineString extends GeometryCollection
1216
{
1317
/** @var Collection<LineString> */
@@ -29,12 +33,4 @@ public function toWkt(): Expression
2933
{
3034
return DB::raw("MULTILINESTRING({$this->toCollectionWkt()})");
3135
}
32-
33-
/**
34-
* @return array<LineString>
35-
*/
36-
public function getGeometries(): array
37-
{
38-
return parent::getGeometries();
39-
}
4036
}

src/Objects/MultiPolygon.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
use Illuminate\Support\Facades\DB;
1010
use InvalidArgumentException;
1111

12+
/**
13+
* @method array<Polygon> getGeometries()
14+
* @method Polygon offsetGet(mixed $offset)
15+
*/
1216
class MultiPolygon extends GeometryCollection
1317
{
1418
/** @var Collection<Polygon> */
@@ -32,12 +36,4 @@ public function toWkt(): Expression
3236
{
3337
return DB::raw("MULTIPOLYGON({$this->toCollectionWkt()})");
3438
}
35-
36-
/**
37-
* @return array<Polygon>
38-
*/
39-
public function getGeometries(): array
40-
{
41-
return parent::getGeometries();
42-
}
4339
}

src/Objects/PointCollection.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Illuminate\Support\Collection;
88
use InvalidArgumentException;
99

10+
/**
11+
* @method array<Point> getGeometries()
12+
* @method Point offsetGet(mixed $offset)
13+
*/
1014
abstract class PointCollection extends GeometryCollection
1115
{
1216
/** @var Collection<Point> */
@@ -23,12 +27,4 @@ public function __construct(Collection | array $geometries)
2327
{
2428
parent::__construct($geometries);
2529
}
26-
27-
/**
28-
* @return array<Point>
29-
*/
30-
public function getGeometries(): array
31-
{
32-
return parent::getGeometries();
33-
}
3430
}

0 commit comments

Comments
 (0)