Passed
Push — master ( e44489...51d43e )
by Bobby
09:09
created

MultiPolygon::addPolygon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ballen\Cartographer;
3
4
use Ballen\Cartographer\Core\GeoJSON;
5
use Ballen\Cartographer\Polygon;
6
use Ballen\Collection\Collection;
7
8
/**
9
 * Cartographer
10
 *
11
 * Cartographer is a PHP library providing the ability to programmatically
12
 * generate GeoJSON objects.
13
 *
14
 * @author Bobby Allen <[email protected]>
15
 * @license http://www.gnu.org/licenses/gpl-3.0.html
16
 * @link https://github.com/allebb/cartographer
17
 * @link http://bobbyallen.me
18
 *
19
 */
20
class MultiPolygon extends GeoJSON
21
{
22
23
    /**
24
     * The GeoJSON schema type
25
     * @var string 
26
     */
27
    protected $type = GeoJSON::TYPE_MULTIPOLYGON;
28
29
    /**
30
     * The collection of Polygon objects.
31
     * @var Collection
32
     */
33
    private $polygons;
34
35
    /**
36
     * Create a new instance of the MultiPolygon GeoJSON schema
37
     */
38 4
    public function __construct($init = null)
39
    {
40 4
        $this->polygons = new Collection;
41
42 4
        if (is_array($init)) {
43 4
            array_walk($init, function($item) {
44 4
                if (is_a($item, Polygon::class)) {
45 4
                    $this->addPolygon($item);
46
                }
47 4
            });
48
        }
49 4
    }
50
51
    /**
52
     * Add a new Polygon object collection.
53
     * @param \Ballen\Cartographer\Polygon $polygon
54
     * @return \Ballen\Cartographer\MultiPolygon
55
     */
56 4
    public function addPolygon(Polygon $polygon)
57
    {
58 4
        $this->polygons->push($polygon);
59 4
        return $this;
60
    }
61
62
    /**
63
     * Exports the type specific schema element(s).
64
     * @return array
65
     */
66
    public function export()
67
    {
68
        $polygons = [];
69
70
        foreach ($this->polygons->all()->toArray() as $polygon) {
71
            $polygons[] = $polygon->exportArray();
72
        }
73
        return [
74
            'coordinates' => $polygons,
75
        ];
76
    }
77
78
    /**
79
     * Validate the type specific schema element(s).
80
     * @return boolean
81
     */
82
    public function validate()
83
    {
84
        return true;
85
    }
86
}
87