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

MultiLineString::addLineString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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