1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace VasilDakov\Speedy\Service\Location\Country; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
8
|
|
|
use JMS\Serializer\Annotation as Serializer; |
9
|
|
|
use VasilDakov\Speedy\Model\Country; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class FindCountryResponse. |
13
|
|
|
* |
14
|
|
|
* @author Vasil Dakov <[email protected]> |
15
|
|
|
* @copyright 2009-2022 Neutrino.bg |
16
|
|
|
* |
17
|
|
|
* @version 1.0 |
18
|
|
|
* |
19
|
|
|
* @Serializer\AccessType("public_method") |
20
|
|
|
*/ |
21
|
|
|
class FindCountryResponse |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @Serializer\Type("ArrayCollection<VasilDakov\Speedy\Model\Country>") |
25
|
|
|
*/ |
26
|
|
|
private ArrayCollection $countries; |
27
|
|
|
|
28
|
1 |
|
public function __construct() |
29
|
|
|
{ |
30
|
1 |
|
$this->countries = new ArrayCollection(); |
31
|
|
|
} |
32
|
|
|
|
33
|
4 |
|
public function setCountries(ArrayCollection $countries): void |
34
|
|
|
{ |
35
|
4 |
|
$this->countries = $countries; |
36
|
|
|
} |
37
|
|
|
|
38
|
3 |
|
public function getCountries(): ArrayCollection |
39
|
|
|
{ |
40
|
3 |
|
return $this->countries; |
41
|
|
|
} |
42
|
|
|
|
43
|
2 |
|
public function findCountryById(int $id): ?Country |
44
|
|
|
{ |
45
|
2 |
|
$collection = $this->getCountries()->filter(function (Country $country) use ($id) { |
46
|
2 |
|
return $country->getId() === $id; |
47
|
2 |
|
}); |
48
|
|
|
|
49
|
2 |
|
if ($collection->isEmpty()) { |
50
|
1 |
|
return null; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/* @var Country */ |
54
|
1 |
|
return $collection->first(); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
public function findCountryByName(string $name): false|null|Country |
58
|
|
|
{ |
59
|
2 |
|
$name = \mb_strtoupper($name, 'UTF-8'); |
60
|
|
|
|
61
|
2 |
|
$collection = $this->countries->filter(function (Country $country) use ($name) { |
62
|
2 |
|
return $country->getName() === $name; |
63
|
2 |
|
}); |
64
|
|
|
|
65
|
2 |
|
return (! $collection->isEmpty()) ? $collection->first() : null; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
|
69
|
2 |
|
public function findCountryByIsoAlpha2(string $isoAlpha2): false|null|Country |
70
|
|
|
{ |
71
|
2 |
|
$collection = $this->getCountries()->filter(function (Country $country) use ($isoAlpha2) { |
72
|
2 |
|
return $country->getIsoAlpha2() === $isoAlpha2; |
73
|
2 |
|
}); |
74
|
|
|
|
75
|
2 |
|
return (! $collection->isEmpty()) ? $collection->first() : null; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|