1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GeoBase\Countries\Country; |
4
|
|
|
|
5
|
|
|
use GeoBase\Countries\Country\CountryName\CountryNameEntity; |
6
|
|
|
|
7
|
|
|
class CountryMapper |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @param array $data |
11
|
|
|
* |
12
|
|
|
* @return CountryCollection |
13
|
|
|
*/ |
14
|
2 |
|
public function mapArrayToCollection(array $data) |
15
|
|
|
{ |
16
|
2 |
|
$collection = new CountryCollection(); |
17
|
2 |
|
foreach ($data as $attributes) { |
18
|
2 |
|
$country = $this->mapArrayToEntity($attributes); |
19
|
2 |
|
if (null !== $country) { |
20
|
2 |
|
$collection->add($country); |
21
|
|
|
} |
22
|
|
|
} |
23
|
|
|
|
24
|
2 |
|
return $collection; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param array $attributes |
29
|
|
|
* |
30
|
|
|
* @return CountryEntity |
31
|
|
|
*/ |
32
|
4 |
|
public function mapArrayToEntity(array $attributes) |
33
|
|
|
{ |
34
|
4 |
|
$country = new CountryEntity(); |
35
|
4 |
|
$country->setCode($attributes['code']); |
36
|
4 |
|
$country->setShortCode($attributes['shortCode']); |
37
|
4 |
|
$country->setLatitude($attributes['latitude']); |
38
|
4 |
|
$country->setLongitude($attributes['longitude']); |
39
|
4 |
|
$country->setCurrency($attributes['currency']); |
40
|
4 |
|
$country->setContinent($attributes['continent']); |
41
|
4 |
|
$country->setPopulation($attributes['population']); |
42
|
4 |
|
$country->setArea($attributes['area']); |
43
|
4 |
|
$country->setCapital($attributes['capital']); |
44
|
4 |
|
$country->setTimezone($attributes['timezone']); |
45
|
4 |
|
$country->setNorth($attributes['north']); |
46
|
4 |
|
$country->setEast($attributes['east']); |
47
|
4 |
|
$country->setSouth($attributes['south']); |
48
|
4 |
|
$country->setWest($attributes['west']); |
49
|
|
|
|
50
|
4 |
|
foreach ($attributes['names'] as $language => $name) { |
51
|
4 |
|
$countryName = new CountryNameEntity(); |
52
|
4 |
|
$countryName->setLanguage($language); |
53
|
4 |
|
$countryName->setName($name); |
54
|
4 |
|
$country->getNames()->add($countryName); |
55
|
|
|
} |
56
|
|
|
|
57
|
4 |
|
return $country; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|