1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Del\Repository; |
6
|
|
|
|
7
|
|
|
use Del\Exception\CountryException; |
8
|
|
|
use Del\Entity\Country; |
9
|
|
|
use Del\Factory\CountryList; |
10
|
|
|
|
11
|
|
|
class CountryRepository |
12
|
|
|
{ |
13
|
|
|
/** @var Country[] $countries */ |
14
|
|
|
private array $countries; |
15
|
|
|
|
16
|
1 |
|
public function __construct() |
17
|
|
|
{ |
18
|
1 |
|
$list = new CountryList(); |
19
|
1 |
|
$this->countries = $list->getCountries(); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** @return Country[] */ |
23
|
|
|
public function findAllCountries(): array |
24
|
|
|
{ |
25
|
|
|
$countries = []; |
26
|
|
|
|
27
|
|
|
foreach ($this->countries as $country) { |
28
|
|
|
$countries[] = $this->createFromArray($country); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
return $countries; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** @throws CountryException */ |
35
|
3 |
|
public function findCountryByIsoCode(string $id): Country |
36
|
|
|
{ |
37
|
3 |
|
if (isset($this->countries[$id])) { |
38
|
2 |
|
return $this->createFromArray($this->countries[$id]); |
39
|
|
|
} |
40
|
|
|
|
41
|
1 |
|
throw new CountryException(CountryException::ERROR_NOT_FOUND); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @throws CountryException */ |
45
|
|
|
public function findCountryBy(string $key, string $value): Country |
46
|
|
|
{ |
47
|
|
|
foreach ($this->countries as $country) { |
48
|
|
|
if ($country[$key] === $value) { |
49
|
|
|
return $this->createFromArray($this->countries[$id]); |
|
|
|
|
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
throw new CountryException(CountryException::ERROR_NOT_FOUND); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
public function createFromArray(array $data): Country |
57
|
|
|
{ |
58
|
2 |
|
$country = new Country(); |
59
|
2 |
|
$country->setId($data['id']); |
60
|
2 |
|
$country->setIso($data['iso']); |
61
|
2 |
|
$country->setName($data['name']); |
62
|
2 |
|
$country->setNumCode($data['numcode']); |
63
|
2 |
|
$country->setFlag($data['flag']); |
64
|
|
|
|
65
|
2 |
|
return $country; |
66
|
|
|
} |
67
|
|
|
} |
68
|
|
|
|