Completed
Push — master ( 9894be...cc7c8f )
by Tobias
21:38
created

AbstractArrayDumper::getArray()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 38
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 38
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 23
nc 8
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/*
6
 * This file is part of the Geocoder package.
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 *
10
 * @license    MIT License
11
 */
12
13
namespace Geocoder\Dumper;
14
15
use Geocoder\Location;
16
17
/**
18
 * @author Tomas Norkūnas <[email protected]>
19
 */
20
abstract class AbstractArrayDumper
21
{
22
    /**
23
     * @param Location $location
24
     *
25
     * @return array
26
     */
27
    protected function getArray(Location $location): array
28
    {
29
        $properties = array_filter($location->toArray(), function ($value) {
30
            return !empty($value);
31
        });
32
33
        unset(
34
            $properties['latitude'],
35
            $properties['longitude'],
36
            $properties['bounds']
37
        );
38
39
        if (0 === count($properties)) {
40
            $properties = null;
41
        }
42
43
        $lat = 0;
44
        $lon = 0;
45
        if (null !== $coordinates = $location->getCoordinates()) {
46
            $lat = $coordinates->getLatitude();
47
            $lon = $coordinates->getLongitude();
48
        }
49
50
        $array = [
51
            'type' => 'Feature',
52
            'geometry' => [
53
                'type' => 'Point',
54
                'coordinates' => [$lon, $lat],
55
            ],
56
            'properties' => $properties,
57
        ];
58
59
        if (null !== $bounds = $location->getBounds()) {
60
            $array['bounds'] = $bounds->toArray();
61
        }
62
63
        return $array;
64
    }
65
}
66