FeatureCollection::addFeature()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 1
1
<?php
2
/**
3
 * Class FeatureCollection
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 array_map;
14
15
/**
16
 * @see https://www.rfc-editor.org/rfc/rfc7946#section-3.3
17
 */
18
class FeatureCollection extends GeoJSONAbstract{
19
20
	/** @var \chillerlan\GeoJSON\Feature[] */
21
	protected array $features = [];
22
23
	/**
24
	 * FeatureCollection constructor.
25
	 *
26
	 * @param \chillerlan\GeoJSON\Feature[]|null $features
27
	 */
28
	public function __construct(iterable $features = null){
29
30
		if(!empty($features)){
31
			$this->addFeatures($features);
32
		}
33
34
	}
35
36
	/**
37
	 *
38
	 */
39
	public function addFeature(Feature $feature):FeatureCollection{
40
		$this->features[] = $feature;
41
42
		return $this;
43
	}
44
45
	/**
46
	 * @param \chillerlan\GeoJSON\Feature[] $features
47
	 */
48
	public function addFeatures(iterable $features):FeatureCollection{
49
50
		foreach($features as $feature){
51
			if($feature instanceof Feature){
52
				$this->addFeature($feature);
53
			}
54
		}
55
56
		return $this;
57
	}
58
59
	/**
60
	 *
61
	 */
62
	public function clearFeatures():FeatureCollection{
63
		$this->features = [];
64
65
		return $this;
66
	}
67
68
	/**
69
	 *
70
	 */
71
	public function toArray():array{
72
		$arr = ['type' => 'FeatureCollection'];
73
74
		if(!empty($this->bbox)){
75
			$arr['bbox'] = $this->bbox;
76
		}
77
78
		$arr['features'] = array_map(fn(Feature $feature):array => $feature->toArray(), $this->features);
79
80
		return $arr;
81
	}
82
83
}
84