Completed
Push — develop ( fa5032...159e70 )
by
unknown
14:52 queued 07:55
created

Converter::toEntity()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 21
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 2
Metric Value
c 2
b 0
f 2
dl 0
loc 21
rs 9.0534
cc 4
eloc 13
nc 6
nop 2
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @license MIT
7
 * @copyright  2013 - 2016 Cross Solution <http://cross-solution.de>
8
 */
9
  
10
/** */
11
namespace Geo\Form\GeoText;
12
13
use Geo\Entity\Geometry\Point;
14
use Jobs\Entity\Location;
15
use Zend\Http\Client;
16
use Zend\Json\Json;
17
18
/**
19
 * ${CARET}
20
 * 
21
 * @author Mathias Gelhausen <[email protected]>
22
 * @todo write test 
23
 */
24
class Converter 
25
{
26
27
    public function toEntity($data, $type)
28
    {
29
        if ('photon' == $type) {
30
            $data = $this->normalizePhotonData($data);
31
        }
32
33
34
        if (empty($data)) {
35
            return new Location();
36
        }
37
        $entity = new Location();
38
        $entity->setCity($data['city'])
39
               ->setRegion($data['region'])
40
               ->setPostalcode($data['postalcode'])
41
               ->setCountry($data['country']);
42
        if (!empty($data['coordinates'])) {
43
               $entity->setCoordinates(new Point($data['coordinates']));
44
        }
45
46
        return $entity;
47
    }
48
49
    protected function normalizePhotonData($data)
50
    {
51
        if (empty($data)) {
52
            return [];
53
        }
54
55
        $data = Json::decode($data, Json::TYPE_ARRAY);
56
57
        $data = [
58
            'city' => isset($data['properties']['city']) ? $data['properties']['city'] : null,
59
            'region' => isset($data['properties']['state']) ? $data['properties']['state'] : null,
60
            'postalcode' => isset($data['properties']['postcode']) ? $data['properties']['postcode'] : null,
61
            'country' => isset($data['properties']['country']) ? $data['properties']['country'] : null,
62
            'coordinates' => isset($data['geometry']['coordinates']) ? $data['geometry']['coordinates'] : null,
63
        ];
64
65
        return $data;
66
    }
67
68
    public function toValue(Location $location, $type)
69
    {
70
        if ('photon' == $type) {
71
            $coordinates = $location->getCoordinates();
72
            $data = [
73
                "geometry" => [
74
                    "coordinates" => $coordinates?$coordinates->getCoordinates():[0,0],
75
                    "type" => $coordinates?$coordinates->getType():'Point'
76
                ],
77
                "type" => "Feature",
78
                "properties" => [
79
                    "country" => $location->getCountry(),
80
                    "city" => $location->getCity(),
81
                    "state" => $location->getRegion(),
82
                    "postcode" => $location->getPostalcode()
83
                ]
84
            ];
85
            return  $location->getCity() . '|' . Json::encode($data);
86
        } else {
87
            return $location->getCity() . ', ' . $location->getRegion();
88
        }
89
    }
90
91
    /**
92
     * used by the beo plugin only. We can hardcode the geoCoderUrl for the moment
93
     *
94
     * @param $input
95
     *
96
     * @return array|mixed|string
97
     */
98
    public function toCoordinates($input) {
99
        $client = new Client('http://api.cross-solution.de/geo');
100
        $client->setMethod('GET');
101
        $client->setParameterGet(array('q' => $input, 'country' => 'DE', 'coor' => 1, 'zoom' => 1 , 'strict' => 0));
102
        $response = $client->send();
103
        $result = $response->getBody();
104
        $result = json_decode($result);
105
        $result = (array) $result->result;
106
        return $result;
107
    }
108
}