-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathGeometryCollection.php
174 lines (150 loc) · 4.29 KB
/
GeometryCollection.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
declare(strict_types=1);
namespace MatanYadaev\EloquentSpatial\Objects;
use ArrayAccess;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use InvalidArgumentException;
use MatanYadaev\EloquentSpatial\Enums\Srid;
use MatanYadaev\EloquentSpatial\Helper;
class GeometryCollection extends Geometry implements ArrayAccess
{
/** @var Collection<int, Geometry> */
protected Collection $geometries;
protected string $collectionOf = Geometry::class;
protected int $minimumGeometries = 0;
/**
* @param Collection<int, Geometry>|array<int, Geometry> $geometries
*
* @throws InvalidArgumentException
*/
public function __construct(Collection|array $geometries, int|Srid|null $srid = null)
{
if (is_array($geometries)) {
$geometries = collect($geometries);
}
$this->geometries = $geometries;
$this->srid = Helper::getSrid($srid);
$this->validateGeometriesType();
$this->validateGeometriesCount();
}
public function toWkt(): string
{
$wktData = $this->getWktData();
if ($wktData === '') {
return 'GEOMETRYCOLLECTION EMPTY';
}
return "GEOMETRYCOLLECTION({$wktData})";
}
public function getWktData(): string
{
return $this->geometries
->map(static function (Geometry $geometry): string {
return $geometry->toWkt();
})
->join(', ');
}
/**
* @return array<mixed>
*/
public function getCoordinates(): array
{
return $this->geometries
->map(static function (Geometry $geometry): array {
return $geometry->getCoordinates();
})
->all();
}
/**
* @return array<mixed>
*/
public function toArray(): array
{
if ($this->isExtended()) {
return parent::toArray();
}
return [
'type' => class_basename(static::class),
'geometries' => $this->geometries->map(static function (Geometry $geometry): array {
return $geometry->toArray();
}),
];
}
/**
* @return Collection<int, Geometry>
*/
public function getGeometries(): Collection
{
return new Collection($this->geometries->all());
}
/**
* @param int $offset
*/
public function offsetExists($offset): bool
{
return isset($this->geometries[$offset]);
}
/**
* @param int $offset
*/
public function offsetGet($offset): Geometry
{
// @phpstan-ignore-next-line
return $this->geometries[$offset];
}
/**
* @param int $offset
* @param Geometry $value
*/
public function offsetSet($offset, $value): void
{
$this->geometries[$offset] = $value;
$this->validateGeometriesType();
}
/**
* @param int $offset
*/
public function offsetUnset($offset): void
{
$this->geometries->splice($offset, 1);
$this->validateGeometriesCount();
}
/**
* @throws InvalidArgumentException
*/
protected function validateGeometriesCount(): void
{
$geometriesCount = $this->geometries->count();
if ($geometriesCount < $this->minimumGeometries) {
throw new InvalidArgumentException(
sprintf(
'%s must contain at least %s %s',
static::class,
$this->minimumGeometries,
Str::plural('entries', $geometriesCount)
)
);
}
}
/**
* @throws InvalidArgumentException
*/
protected function validateGeometriesType(): void
{
$this->geometries->each(function (mixed $geometry): void {
/** @var mixed $geometry */
if (! is_object($geometry) || ! ($geometry instanceof $this->collectionOf)) {
throw new InvalidArgumentException(
sprintf('%s must be a collection of %s', static::class, $this->collectionOf)
);
}
});
}
/**
* Checks whether the class is used directly or via a sub-class.
*/
private function isExtended(): bool
{
return is_subclass_of(static::class, self::class);
}
}