Passed
Push — master ( a4f803...3d4b77 )
by smiley
12:11
created

GeoJSONAbstract   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
eloc 7
c 1
b 0
f 0
dl 0
loc 35
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
A toJSON() 0 2 1
A jsonSerialize() 0 2 1
A setBbox() 0 9 2
1
<?php
2
/**
3
 * Class GeoJSONAbstract
4
 *
5
 * @created      14.02.2019
6
 * @author       smiley <[email protected]>
7
 * @copyright    2019 smiley
8
 * @license      MIT
9
 */
10
11
namespace chillerlan\GeoJSON;
12
13
use JsonSerializable;
14
use function count;
15
use function in_array;
16
use function json_encode;
17
18
abstract class GeoJSONAbstract implements JsonSerializable{
19
20
	protected array $bbox;
21
22
	/**
23
	 * @throws \chillerlan\GeoJSON\GeoJSONException
24
	 */
25
	public function setBbox(array $bbox):GeoJSONAbstract{
26
27
		if(!in_array(count($bbox), [4, 6], true)){
28
			throw new GeoJSONException('invalid bounding box array');
29
		}
30
31
		$this->bbox = $bbox;
32
33
		return $this;
34
	}
35
36
	/**
37
	 *
38
	 */
39
	abstract public function toArray():array;
40
41
	/**
42
	 *
43
	 */
44
	public function toJSON(int $options = null):string{
45
		return json_encode($this->toArray(), $options ?? 0);
46
	}
47
48
	/**
49
	 * @inheritDoc
50
	 */
51
	public function jsonSerialize():array{
52
		return $this->toArray();
53
	}
54
55
}
56