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

GeometryCollection::__construct()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 5
nc 2
nop 1
dl 0
loc 8
ccs 6
cts 6
cp 1
crap 3
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Ballen\Cartographer;
3
4
use Ballen\Cartographer\Core\GeoJSONTypeInterface;
5
use Ballen\Cartographer\Core\GeoJSON;
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 GeometryCollection extends GeoJSON implements GeoJSONTypeInterface
21
{
22
23
    /**
24
     * The GeoJSON schema type
25
     * @var string
26
     */
27
    protected $type = GeoJSON::TYPE_GEOMETRYCOLLECTION;
28
29
    /**
30
     * The geometries collection (of Geomerty Objects)
31
     * @var Collection
32
     */
33
    private $geometries;
34
35 4
    public function __construct($init = [])
36
    {
37 4
        $this->geometries = new Collection;
38
39 4
        if (is_array($init)) {
40 4
            array_walk($init, function($item) {
41 4
                if (is_a($item, GeoJSONTypeInterface::class)) {
42 4
                    $this->addGeometry($item);
43
                }
44 4
            });
45
        }
46 4
    }
47
48
    /**
49
     * Add a new geometry object to the GeometryCollection.
50
     * @param GeoJSONTypeInterface $geometry
51
     */
52 4
    public function addGeometry(GeoJSONTypeInterface $geometry)
53
    {
54 4
        $this->geometries->push($geometry);
55 4
    }
56
57
    /**
58
     * Exports the type specific schema element(s).
59
     * @return array
60
     */
61 2
    public function export()
62
    {
63 2
        $geometries = [];
64 2
        foreach ($this->geometries->all()->toArray() as $gemometry) {
65 2
            $geometries[] = $gemometry->generateMember();
66
        }
67
        return [
68 2
            'geometries' => $geometries,
69
        ];
70
    }
71
72
    /**
73
     * Validate the type specific schema element(s).
74
     * @return boolean
75
     */
76 4
    public function validate()
77
    {
78
        // GeometryCollection type must have one or more geometry type.
79 4
        if ($this->geometries->count() > 0) {
80 2
            return true;
81
        }
82 2
        return false;
83
    }
84
}
85