Completed
Push — develop ( a16549...b4f423 )
by
unknown
15:42
created

AbstractLocation::getStreetname()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
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
     * Street name
20
     *
21
     * @var string
22
     * @ODM\Field(type="string")
23
     */
24
    protected $streetname;
25
26
    /**
27
     * Street number
28
     *
29
     * @var string
30
     * @ODM\Field(type="string")
31
     */
32
    protected $streetnumber;
33
34
    /**
35
     * city name of a job location
36
     *
37
     * @ODM\Field(type="string")
38
     */
39
    protected $city;
40
41
    /**
42
     * region of a job location. E.g "Hessen" is a region in germany
43
     *
44
     * @ODM\Field(type="string")
45
     */
46
    protected $region;
47
48
    /**
49
     * postalcode of a job location.
50
     *
51
     * @var String
52
     * @ODM\Field(type="string")
53
     */
54
    protected $postalcode;
55
56
    /**
57
     * coordinates of a job location.
58
     *
59
     * @var GeoJson
60
     * @ODM\EmbedOne(discriminatorField="_entity")
61
     */
62
    protected $coordinates;
63
64
    /**
65
     * Country of a job location
66
     * @var String
67
     *
68
     * @ODM\Field(type="string")
69
     */
70
    protected $country;
71
72
    public function __construct($attributes = null)
73
    {
74
        if (is_string($attributes)) {
75
            $this->fromString($attributes);
76
        } else if (is_array($attributes)) {
77
            $this->fromArray($attributes);
78
        }
79
    }
80
    
81
    public function __toString()
82
    {
83
        $coords = $this->getCoordinates();
84
        $street = $this->getStreetname();
85
        $number = $this->getStreetnumber();
86
        $postalCode = $this->getPostalCode();
87
        $city = $this->getCity();
88
        $country = $this->getCountry();
89
        $region = $this->getRegion();
90
        
91
        $str = '';
92
        if ($street) { $str .= $street; if (!$number) { $str .= ', '; } }
93
        if ($number) { $str .= ' ' . $number . ', '; }
94
        if ($postalCode) { $str .= $postalCode . ' '; }
95
        if ($city) { $str .= $city; }
96
        if ($region) { $str .= ', ' . $region; }
97
        if ($country) { $str .= ', ' . $country; }
98
        if ($coords) {
99
            $coords = $coords->getCoordinates();
100
            $str .= ' ( ' . join(', ', $coords) . ' )';
101
        }
102
        
103
        return $str;
104
105
    }
106
107
    public function toArray()
108
    {
109
        $coords = $this->getCoordinates();
110
        $attributes = [
111
            'streetname' => $this->getStreetname(),
112
            'streetnumber' => $this->getStreetnumber(),
113
            'city' => $this->getCity(),
114
            'region' => $this->getRegion(),
115
            'postalcode' => $this->getPostalCode(),
116
            'country' => $this->getCountry(),
117
            'coordinates' => $coords
118
                    ? [
119
                        'type' => $coords->getType(),
120
                        'coordinates' => $coords->getCoordinates(),
121
                      ]
122
                    : null
123
124
        ];
125
        return $attributes;
126
    }
127
128
    public function toString()
129
    {
130
        $attributes = $this->toArray();
131
132
        return Json::encode($attributes);
133
    }
134
135
    public function fromArray(array $data)
136
    {
137
        foreach ($data as $key => $value) {
138
            if (!$value) { continue; }
139
140
            if ('coordinates' == $key) {
141
                $class = '\\Geo\\Entity\\Geometry\\' . $value['type'];
142
                $value = new $class($value['coordinates']);
143
            }
144
145
            $setter = "set$key";
146
            if (is_callable([$this, $setter])) {
147
                $this->$setter($value);
148
            }
149
        }
150
151
        return $this;
152
    }
153
154
    public function fromString($serialized)
155
    {
156
        $attributes = Json::decode($serialized, Json::TYPE_ARRAY);
157
158
        return $this->fromArray($attributes);
159
    }
160
161
    /**
162
     * @codeCoverageIgnore
163
     */
164
    public function preUpdate()
165
    {
166
    }
167
168
    public function getCoordinates()
169
    {
170
        return $this->coordinates;
171
    }
172
173
    /**
174
     * @param GeoJson $coordinates
175
     *
176
     * @return $this
177
     */
178
    public function setCoordinates(GeoJson $coordinates)
179
    {
180
        $this->coordinates = $coordinates;
181
        return $this;
182
    }
183
184
    /**
185
     * @return String
186
     */
187
    public function getPostalCode()
188
    {
189
        return $this->postalcode;
190
    }
191
192
    /**
193
     * @param $postalcode
194
     *
195
     * @return $this
196
     */
197
    public function setPostalCode($postalcode)
198
    {
199
        $this->postalcode = $postalcode;
200
        return $this;
201
    }
202
203
    /**
204
     * @return string
205
     */
206
    public function getStreetname()
207
    {
208
        return $this->streetname;
209
    }
210
211
    /**
212
     * @param string $streetname
213
     *
214
     * @return self
215
     */
216
    public function setStreetname($streetname)
217
    {
218
        $this->streetname = $streetname;
219
220
        return $this;
221
    }
222
223
    /**
224
     * @return string
225
     */
226
    public function getStreetnumber()
227
    {
228
        return $this->streetnumber;
229
    }
230
231
    /**
232
     * @param string $streetnumber
233
     *
234
     * @return self
235
     */
236
    public function setStreetnumber($streetnumber)
237
    {
238
        $this->streetnumber = $streetnumber;
239
240
        return $this;
241
    }
242
243
244
245
    /**
246
     * @return mixed
247
     */
248
    public function getCity()
249
    {
250
        return $this->city;
251
    }
252
253
    /**
254
     * @param $country
255
     *
256
     * @return $this
257
     */
258
    public function setCity($country)
259
    {
260
        $this->city = $country;
261
        return $this;
262
    }
263
264
    /**
265
     * @return String
266
     */
267
    public function getCountry()
268
    {
269
        return $this->country;
270
    }
271
272
    /**
273
     * @param $country
274
     *
275
     * @return $this
276
     */
277
    public function setCountry($country)
278
    {
279
        $this->country = $country;
280
        return $this;
281
    }
282
283
    /**
284
     * @return mixed
285
     */
286
    public function getRegion()
287
    {
288
        return $this->region;
289
    }
290
291
    /**
292
     * @param $region
293
     *
294
     * @return $this
295
     */
296
    public function setRegion($region)
297
    {
298
        $this->region = $region;
299
        return $this;
300
    }
301
}
302