Completed
Push — develop ( 515643...e2e58e )
by
unknown
08:08
created

AbstractLocation::toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 19
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 12
nc 2
nop 0
1
<?php
2
/**
3
 * YAWIK
4
 *
5
 * @filesource
6
 * @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de)
7
 * @license   MIT
8
 */
9
10
namespace Core\Entity;
11
12
use GeoJson\GeoJson;
13
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
14
use Zend\Json\Json;
15
16
abstract class AbstractLocation extends AbstractEntity implements LocationInterface
17
{
18
    /**
19
     * city name of a job location
20
     *
21
     * @ODM\Field(type="string")
22
     */
23
    protected $city;
24
25
    /**
26
     * region of a job location. E.g "Hessen" is a region in germany
27
     *
28
     * @ODM\Field(type="string")
29
     */
30
    protected $region;
31
32
    /**
33
     * postalcode of a job location.
34
     *
35
     * @var String
36
     * @ODM\Field(type="string")
37
     */
38
    protected $postalcode;
39
40
    /**
41
     * coordinates of a job location.
42
     *
43
     * @var GeoJson
44
     * @ODM\EmbedOne(discriminatorField="_entity")
45
     */
46
    protected $coordinates;
47
48
    /**
49
     * Country of a job location
50
     * @var String
51
     *
52
     * @ODM\Field(type="string")
53
     */
54
    protected $country;
55
56
    public function __construct()
57
    {
58
    }
59
60
    public function toString()
61
    {
62
        $coords = $this->getCoordinates();
63
        $attributes = [
64
            'city' => $this->getCity(),
65
            'region' => $this->getRegion(),
66
            'postalCode' => $this->getPostalCode(),
67
            'country' => $this->getCountry(),
68
            'coordinates' => $coords
69
                    ? [
70
                        'type' => $coords->getType(),
71
                        'coordinates' => $coords->getCoordinates(),
72
                      ]
73
                    : null
74
75
        ];
76
77
        return Json::encode($attributes);
78
    }
79
80
    public function fromString($serialized)
81
    {
82
        $attributes = Json::decode($serialized, Json::TYPE_ARRAY);
83
84
        foreach ($attributes as $key => $value) {
85
            if (!$value) { continue; }
86
87
            if ('coordinates' == $key) {
88
                $class = '\\Geo\\Entity\\Geometry\\' . $value['type'];
89
                $value = new $class($value['coordinates']);
90
            }
91
92
            $setter = "set$key";
93
            if (is_callable([$this, $setter])) {
94
                $this->$setter($value);
95
            }
96
        }
97
98
        return $this;
99
    }
100
101
    public function preUpdate()
102
    {
103
    }
104
105
    public function getCoordinates()
106
    {
107
        return $this->coordinates;
108
    }
109
110
    /**
111
     * @param GeoJson $coordinates
112
     *
113
     * @return $this
114
     */
115
    public function setCoordinates(GeoJson $coordinates)
116
    {
117
        $this->coordinates = $coordinates;
118
        return $this;
119
    }
120
121
    /**
122
     * @return String
123
     */
124
    public function getPostalCode()
125
    {
126
        return $this->postalcode;
127
    }
128
129
    /**
130
     * @param $postalcode
131
     *
132
     * @return $this
133
     */
134
    public function setPostalCode($postalcode)
135
    {
136
        $this->postalcode = $postalcode;
137
        return $this;
138
    }
139
140
    /**
141
     * @return mixed
142
     */
143
    public function getCity()
144
    {
145
        return $this->city;
146
    }
147
148
    /**
149
     * @param $country
150
     *
151
     * @return $this
152
     */
153
    public function setCity($country)
154
    {
155
        $this->city = $country;
156
        return $this;
157
    }
158
159
    /**
160
     * @return String
161
     */
162
    public function getCountry()
163
    {
164
        return $this->country;
165
    }
166
167
    /**
168
     * @param $country
169
     *
170
     * @return $this
171
     */
172
    public function setCountry($country)
173
    {
174
        $this->country = $country;
175
        return $this;
176
    }
177
178
    /**
179
     * @return mixed
180
     */
181
    public function getRegion()
182
    {
183
        return $this->region;
184
    }
185
186
    /**
187
     * @param $region
188
     *
189
     * @return $this
190
     */
191
    public function setRegion($region)
192
    {
193
        $this->region = $region;
194
        return $this;
195
    }
196
}