1 | <?php |
||
2 | /** |
||
3 | * Class Feature |
||
4 | * |
||
5 | * @created 25.06.2018 |
||
6 | * @author smiley <[email protected]> |
||
7 | * @copyright 2018 smiley |
||
8 | * @license MIT |
||
9 | */ |
||
10 | |||
11 | namespace chillerlan\GeoJSON; |
||
12 | |||
13 | use function in_array; |
||
14 | use function is_int; |
||
15 | use function is_string; |
||
16 | |||
17 | /** |
||
18 | * @see https://www.rfc-editor.org/rfc/rfc7946#section-3.2 |
||
19 | */ |
||
20 | class Feature extends GeoJSONAbstract{ |
||
21 | |||
22 | public const types = [ |
||
23 | 'Point', |
||
24 | 'MultiPoint', |
||
25 | 'LineString', |
||
26 | 'MultiLineString', |
||
27 | 'Polygon', |
||
28 | 'MultiPolygon', |
||
29 | 'GeometryCollection', |
||
30 | ]; |
||
31 | |||
32 | protected array $coords; |
||
33 | protected string $type; |
||
34 | protected array $properties; |
||
35 | /** @var int|string */ |
||
36 | protected $id; |
||
37 | |||
38 | /** |
||
39 | * Feature constructor. |
||
40 | * |
||
41 | * @param array|null $coords |
||
42 | * @param string|null $type |
||
43 | * @param int|string|null $id |
||
44 | * |
||
45 | * @throws \chillerlan\GeoJSON\GeoJSONException |
||
46 | */ |
||
47 | public function __construct(array $coords = null, string $type = null, $id = null){ |
||
48 | |||
49 | if($coords !== null){ |
||
50 | |||
51 | if($type === null){ |
||
52 | throw new GeoJSONException('got coords but no feature type'); |
||
53 | } |
||
54 | |||
55 | $this->setGeometry($coords, $type); |
||
56 | } |
||
57 | |||
58 | if($id !== null){ |
||
59 | $this->setID($id); |
||
60 | } |
||
61 | |||
62 | } |
||
63 | |||
64 | /** |
||
65 | * @throws \chillerlan\GeoJSON\GeoJSONException |
||
66 | */ |
||
67 | public function setGeometry(array $coords, string $type):Feature{ |
||
68 | |||
69 | if(empty($coords)){ |
||
70 | throw new GeoJSONException('invalid coords array'); |
||
71 | } |
||
72 | |||
73 | if(!in_array($type, $this::types, true)){ |
||
74 | throw new GeoJSONException('invalid geometry type'); |
||
75 | } |
||
76 | |||
77 | $this->coords = $coords; |
||
78 | $this->type = $type; |
||
79 | |||
80 | return $this; |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * |
||
85 | */ |
||
86 | public function setProperties(array $properties):Feature{ |
||
87 | $this->properties = $properties; |
||
88 | |||
89 | return $this; |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * @param int|string $id |
||
94 | * |
||
95 | * @throws \chillerlan\GeoJSON\GeoJSONException |
||
96 | */ |
||
97 | public function setID($id):Feature{ |
||
98 | |||
99 | if(!is_string($id) && !is_int($id)){ |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
100 | throw new GeoJSONException('invalid id'); |
||
101 | } |
||
102 | |||
103 | $this->id = $id; |
||
104 | |||
105 | return $this; |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * @throws \chillerlan\GeoJSON\GeoJSONException |
||
110 | */ |
||
111 | public function toArray():array{ |
||
112 | |||
113 | if(empty($this->coords) || empty($this->type)){ |
||
114 | throw new GeoJSONException('invalid feature'); |
||
115 | } |
||
116 | |||
117 | $arr = [ |
||
118 | 'type' => 'Feature', |
||
119 | 'geometry' => [ |
||
120 | 'type' => $this->type, |
||
121 | 'coordinates' => $this->coords, |
||
122 | ] |
||
123 | ]; |
||
124 | |||
125 | if(!empty($this->bbox)){ |
||
126 | $arr['bbox'] = $this->bbox; |
||
127 | } |
||
128 | |||
129 | if(!empty($this->properties)){ |
||
130 | $arr['properties'] = $this->properties; |
||
131 | } |
||
132 | |||
133 | if(!empty($this->id)){ |
||
134 | $arr['properties']['id'] = $this->id; // leaflet |
||
135 | # $arr['id'] = $this->id; // GMaps |
||
136 | } |
||
137 | |||
138 | return $arr; |
||
139 | } |
||
140 | |||
141 | } |
||
142 |