• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mlocati / comuni-italiani / 9745192926

01 Jul 2024 01:30PM UTC coverage: 98.876% (-1.1%) from 100.0%
9745192926

push

github

mlocati
Add Finder

43 of 45 new or added lines in 1 file covered. (95.56%)

176 of 178 relevant lines covered (98.88%)

1127.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.56
/src/Finder.php
1
<?php
2

3
declare(strict_types=1);
4

5
namespace MLocati\ComuniItaliani;
6

7
class Finder
8
{
9
    protected Factory $factory;
10

11
    public function __construct(?Factory $factory = null)
12
    {
NEW
13
        $this->factory = $factory ?? new Factory();
×
14
    }
15

16
    /**
17
     * Find a territory (Geographical Subdivision, Region, Province, or Municipality) given its ID.
18
     *
19
     * @param int|string|mixed $id int for Geographical Subdivision, string for other territory types.
20
     */
21
    public function getTerritoryByID($id): ?Territory
22
    {
23
        switch (gettype($id)) {
26✔
24
            case 'integer':
26✔
25
                return $this->getGeographicalSubdivisionByID($id);
3✔
26
            case 'string':
23✔
27
                switch (strlen($id)) {
21✔
28
                    case 2:
21✔
29
                        return $this->getRegionByID($id);
3✔
30
                    case 3:
18✔
31
                        return $this->getProvinceByID($id);
4✔
32
                    case 6:
14✔
33
                        return $this->getMunicipalityByID($id);
6✔
34
                }
35
                break;
8✔
36
        }
37

38
        return null;
10✔
39
    }
40

41
    /**
42
     * Find a Geographical Subdivision given its ID
43
     */
44
    public function getGeographicalSubdivisionByID(int $id): ?GeographicalSubdivision
45
    {
46
        foreach ($this->factory->getGeographicalSubdivisions() as $geographicalSubdivision) {
3✔
47
            if ($geographicalSubdivision->getID() === $id) {
3✔
48
                return $geographicalSubdivision;
1✔
49
            }
50
        }
51

52
        return null;
2✔
53
    }
54

55
    /**
56
     * Find a Region given its ID
57
     */
58
    public function getRegionByID(string $id): ?Region
59
    {
60
        if (!preg_match('/^[0-9]{2}$/', $id)) {
3✔
61
            return null;
1✔
62
        }
63
        foreach ($this->factory->getGeographicalSubdivisions() as $geographicalSubdivisions) {
2✔
64
            foreach ($geographicalSubdivisions->getRegions() as $region) {
2✔
65
                if ($region->getID() === $id) {
2✔
66
                    return $region;
1✔
67
                }
68
            }
69
        }
70

71
        return null;
1✔
72
    }
73

74
    /**
75
     * Find a Province/UTS given its ID
76
     *
77
     * @param bool $oldIDToo look for $in in historical Province codes too?
78
     */
79
    public function getProvinceByID(string $id, bool $oldIDToo = false): ?Province
80
    {
81
        if (!preg_match('/^[0-9]{3}$/', $id)) {
4✔
82
            return null;
1✔
83
        }
84
        foreach ($this->factory->getGeographicalSubdivisions() as $geographicalSubdivisions) {
3✔
85
            foreach ($geographicalSubdivisions->getRegions() as $region) {
3✔
86
                foreach ($region->getProvinces() as $province) {
3✔
87
                    foreach ($province->getMunicipalities() as $municipality) {
3✔
88
                        if ($province->getID() === $id) {
3✔
89
                            return $province;
2✔
90
                        }
91
                        if ($oldIDToo && $province->getOldID() === $id) {
3✔
NEW
92
                            return $province;
×
93
                        }
94
                    }
95
                }
96
            }
97
        }
98

99
        return null;
1✔
100
    }
101

102

103
    /**
104
     * Find a Municipality given its ID
105
     *
106
     * @param bool $oldIDToo look for $in in historical Province codes too?
107
     */
108
    public function getMunicipalityByID(string $id): ?Municipality
109
    {
110
        if (!preg_match('/^[0-9]{6}$/', $id)) {
6✔
111
            return null;
1✔
112
        }
113
        foreach ($this->factory->getGeographicalSubdivisions() as $geographicalSubdivisions) {
5✔
114
            foreach ($geographicalSubdivisions->getRegions() as $region) {
5✔
115
                foreach ($region->getProvinces() as $province) {
5✔
116
                    foreach ($province->getMunicipalities() as $municipality) {
5✔
117
                        if ($municipality->getID() === $id) {
5✔
118
                            return $municipality;
2✔
119
                        }
120
                    }
121
                }
122
            }
123
        }
124

125
        return null;
3✔
126
    }
127
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc