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