-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeoJson.php
68 lines (60 loc) · 1.62 KB
/
GeoJson.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
<?php
namespace futuretek\geojson;
use futuretek\geojson\types\TypeInterface;
use Yii;
use yii\base\InvalidParamException;
use yii\helpers\Json;
class GeoJson
{
/**
* @var array
*/
public static $allowedTypes = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeometryCollection', 'Feature', 'FeatureCollection'];
/**
* @var array
*/
public static $geometryObjects = ['Point', 'MultiPoint', 'LineString', 'MultiLineString', 'Polygon', 'MultiPolygon', 'GeometryCollection'];
/**
* @var TypeInterface[]
*/
private $_items = [];
/**
* Add object to GeoJSON
*
* @param TypeInterface $object
* @return self
* @throws \yii\base\InvalidParamException
*/
public function add(TypeInterface $object)
{
$this->_validate($object);
$this->_items[] = $object;
return $this;
}
/**
* Output encoded GeoJSON
*
* @return string
* @throws \yii\base\InvalidParamException
*/
public function output()
{
$result = [];
foreach ($this->_items as $object) {
$result[] = $object->export();
}
return Json::encode($result);
}
/**
* Check if object is valid
*
* @param TypeInterface $object
* @throws \yii\base\InvalidParamException
*/
private function _validate(TypeInterface $object)
{
if (!in_array($object->getType(), self::$allowedTypes, true)) {
throw new InvalidParamException('Only types ' . implode(', ', self::$allowedTypes) . ' are allowed.');
}
}
}