Passed
Push — main ( 98c11d...80b66f )
by Vasil
03:34
created

FindCountryResponse::findCountryArrayByName()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 1
dl 0
loc 12
ccs 0
cts 8
cp 0
crap 6
rs 10
c 0
b 0
f 0
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
use function array_filter;
12
use function strcasecmp;
13
use function mb_strtoupper;
14
15
/**
16
 * Class FindCountryResponse
17
 *
18
 * @author Vasil Dakov <[email protected]>
19
 * @copyright 2009-2022 Neutrino.bg
20
 * @version 1.0
21
 * @Serializer\AccessType("public_method")
22
 */
23
class FindCountryResponse
24
{
25
    /**
26
     * @Serializer\Type("ArrayCollection<VasilDakov\Speedy\Model\Country>")
27
     */
28
    private ArrayCollection $countries;
29
30 1
    public function __construct()
31
    {
32 1
        $this->countries = new ArrayCollection();
33
    }
34
35
    /**
36
     * @param ArrayCollection $countries
37
     */
38 5
    public function setCountries(ArrayCollection $countries): void
39
    {
40 5
        $this->countries = $countries;
41
    }
42
43
    /**
44
     * @return ArrayCollection
45
     */
46 3
    public function getCountries(): ArrayCollection
47
    {
48 3
        return $this->countries;
49
    }
50
51
52 2
    public function findCountryById(int $id): ?Country
53
    {
54 2
        $collection = $this->getCountries()->filter(function (Country $country) use ($id) {
55 2
            return $country->getId() === $id;
56 2
        });
57
58 2
        if ($collection->isEmpty()) {
59 1
            return null;
60
        }
61
62
        /** @var Country */
63 1
        return $collection->first();
64
    }
65
66
67
    /**
68
     * @param string $name
69
     * @return Country|null
70
     */
71 2
    public function findCountryByName(string $name): ?Country
72
    {
73 2
        $name = mb_strtoupper($name, 'UTF-8');
74
75 2
        $collection = $this->countries->filter(function (Country $country) use ($name) {
76 2
            return $country->getName() === $name;
77 2
        });
78
79 2
        if ($collection->isEmpty()) {
80 1
            return null;
81
        }
82
83
        /** @var Country */
84 1
        return $collection->first();
85
    }
86
87
    /**
88
     * @param string $isoAlpha2
89
     * @return Country|null
90
     */
91 2
    public function findCountryByIsoAlpha2(string $isoAlpha2): ?Country
92
    {
93 2
        $collection = $this->getCountries()->filter(function (Country $country) use ($isoAlpha2) {
94 2
            return $country->getIsoAlpha2() === $isoAlpha2;
95 2
        });
96
97 2
        if ($collection->isEmpty()) {
98 1
            return null;
99
        }
100
101
        /** @var Country */
102 1
        return $collection->first();
103
    }
104
}
105