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

LineString   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 1 Features 1
Metric Value
eloc 21
dl 0
loc 80
ccs 25
cts 25
cp 1
rs 10
c 1
b 1
f 1
wmc 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A export() 0 9 2
A addCoordinate() 0 3 1
A __construct() 0 8 3
A validate() 0 7 2
A exportArray() 0 7 2
1
<?php
2
namespace Ballen\Cartographer;
3
4
use Ballen\Cartographer\Core\GeoJSONTypeInterface;
5
use Ballen\Cartographer\Core\Multipliable;
6
use Ballen\Cartographer\Core\GeoJSON;
7
use Ballen\Cartographer\Core\LatLong;
8
use Ballen\Collection\Collection;
9
10
/**
11
 * Cartographer
12
 *
13
 * Cartographer is a PHP library providing the ability to programmatically
14
 * generate GeoJSON objects.
15
 *
16
 * @author Bobby Allen <[email protected]>
17
 * @license http://www.gnu.org/licenses/gpl-3.0.html
18
 * @link https://github.com/allebb/cartographer
19
 * @link http://bobbyallen.me
20
 *
21
 */
22
class LineString extends GeoJSON implements GeoJSONTypeInterface, Multipliable
23
{
24
25
    /**
26
     * The GeoJSON schema type
27
     * @var string
28
     */
29
    protected $type = GeoJSON::TYPE_LINESTRING;
30
31
    /**
32
     * The coordinates collection (of LatLong objects)
33
     * @var Collection
34
     */
35
    private $coordinates;
36
37
    /**
38
     * 
39
     */
40 20
    public function __construct($init = [])
41
    {
42 20
        $this->coordinates = new Collection;
43
44 20
        if (is_array($init)) {
45 20
            array_walk($init, function($item) {
46 20
                if (is_a($item, LatLong::class)) {
47 20
                    $this->addCoordinate($item);
48
                }
49 20
            });
50
        }
51 20
    }
52
53
    /**
54
     * Add a new coordinate to the LineString sequence.
55
     * @param LatLong $coordinate
56
     */
57 20
    public function addCoordinate(LatLong $coordinate)
58
    {
59 20
        $this->coordinates->push($coordinate);
60 20
    }
61
62
    /**
63
     * Exports the type specific schema element(s).
64
     * @return array
65
     */
66 10
    public function export()
67
    {
68 10
        $coords = [];
69 10
        foreach ($this->coordinates->all()->toArray() as $c) {
70 10
            $coords[] = $c->lngLatArray();
71
        }
72
73
        return [
74 10
            'coordinates' => $coords,
75
        ];
76
    }
77
78
    /**
79
     * Exports the type specific schema array (for use in MultiX types).
80
     * @return array
81
     */
82 4
    public function exportArray()
83
    {
84 4
        $coords = [];
85 4
        foreach ($this->coordinates->all()->toArray() as $coord) {
86 4
            $coords[] = $coord->lngLatArray();
87
        }
88 4
        return $coords;
89
    }
90
91
    /**
92
     * Validate the type specific schema element(s).
93
     * @return boolean
94
     */
95 4
    public function validate()
96
    {
97
        // LineString Type must have two or more coordinates.
98 4
        if ($this->coordinates->count() < 2) {
99 2
            return false;
100
        }
101 2
        return true;
102
    }
103
}
104