1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Del\Repository; |
4
|
|
|
|
5
|
|
|
use Del\Exception\CountryException; |
6
|
|
|
use Del\Entity\Country; |
7
|
|
|
use Del\Factory\CountryList; |
8
|
|
|
|
9
|
|
|
class CountryRepository |
10
|
|
|
{ |
11
|
|
|
/** @var Country[] $countries */ |
12
|
|
|
private $countries; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* CountryRepository constructor. |
16
|
|
|
*/ |
17
|
1 |
|
public function __construct() |
18
|
|
|
{ |
19
|
1 |
|
$list = new CountryList(); |
20
|
1 |
|
$this->countries = $list->getCountries(); |
21
|
1 |
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @return Country[] |
25
|
|
|
*/ |
26
|
|
|
public function findAllCountries(): array |
27
|
|
|
{ |
28
|
|
|
$countries = []; |
29
|
|
|
|
30
|
|
|
foreach ($this->countries as $country) { |
31
|
|
|
$countries[] = $this->createFromArray($country); |
|
|
|
|
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $countries; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param string $id |
39
|
|
|
* @return Country |
40
|
|
|
* @throws CountryException |
41
|
|
|
*/ |
42
|
3 |
|
public function findCountryByIsoCode(string $id): Country |
43
|
|
|
{ |
44
|
3 |
|
if (isset($this->countries[$id])) { |
45
|
2 |
|
return $this->createFromArray($this->countries[$id]); |
|
|
|
|
46
|
|
|
} |
47
|
|
|
|
48
|
1 |
|
throw new CountryException(CountryException::ERROR_NOT_FOUND); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $id |
|
|
|
|
53
|
|
|
* @return Country |
54
|
|
|
* @throws CountryException |
55
|
|
|
*/ |
56
|
|
|
public function findCountryBy(string $key, string $value): Country |
57
|
|
|
{ |
58
|
|
|
foreach ($this->countries as $country) { |
59
|
|
|
if ($country[$key] === $value) { |
60
|
|
|
return $this->createFromArray($this->countries[$id]); |
|
|
|
|
61
|
|
|
} |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
throw new CountryException(CountryException::ERROR_NOT_FOUND); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @param array $data |
69
|
|
|
* @return Country |
70
|
|
|
*/ |
71
|
2 |
|
public function createFromArray(array $data): Country |
72
|
|
|
{ |
73
|
2 |
|
$country = new Country(); |
74
|
2 |
|
$country->setId($data['id']); |
75
|
2 |
|
$country->setIso($data['iso']); |
76
|
2 |
|
$country->setName($data['name']); |
77
|
2 |
|
$country->setNumCode($data['numcode']); |
78
|
2 |
|
$country->setFlag($data['flag']); |
79
|
|
|
|
80
|
2 |
|
return $country; |
81
|
|
|
} |
82
|
|
|
} |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: