|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Utility to get country and municipality codes with EDB-Brugs |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category EDBBrugs |
|
8
|
|
|
* @package EDBBrugs |
|
9
|
|
|
* @author Lars Olesen <[email protected]> |
|
10
|
|
|
* @license MIT Open Source License https://opensource.org/licenses/MIT |
|
11
|
|
|
* @version GIT: <git_id> |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace EDBBrugs; |
|
15
|
|
|
|
|
16
|
|
|
use EDBBrugs\UtilityInterface; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Utility to get country and municipality codes with EDB-Brugs |
|
20
|
|
|
* |
|
21
|
|
|
* @category EDBBrugs |
|
22
|
|
|
* @package EDBBrugs |
|
23
|
|
|
* @author Lars Olesen <[email protected]> |
|
24
|
|
|
* @license MIT Open Source License https://opensource.org/licenses/MIT |
|
25
|
|
|
* @version GIT: <git_id> |
|
26
|
|
|
*/ |
|
27
|
|
|
class Utility implements UtilityInterface |
|
28
|
|
|
{ |
|
29
|
|
|
/** |
|
30
|
|
|
* Gets country code |
|
31
|
|
|
* |
|
32
|
|
|
* $return integer |
|
33
|
|
|
*/ |
|
34
|
2 |
|
public function getCountryCode($country) |
|
35
|
|
|
{ |
|
36
|
2 |
|
$rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Landekoder.csv", $country); |
|
37
|
2 |
|
$stored_in_column = 2; |
|
38
|
2 |
|
return $rows[$stored_in_column]; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Gets municipality code |
|
43
|
|
|
* |
|
44
|
|
|
* $return integer |
|
45
|
|
|
*/ |
|
46
|
2 |
|
public function getMunicipalityCode($municipality) |
|
47
|
|
|
{ |
|
48
|
2 |
|
$rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Kommuner.csv", $municipality); |
|
49
|
2 |
|
$stored_in_column = 2; |
|
50
|
2 |
|
return $rows[$stored_in_column]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Loops through csv file and stops with search |
|
55
|
|
|
* |
|
56
|
|
|
* @param $file_name |
|
57
|
|
|
* @param $search |
|
58
|
|
|
* @return array |
|
59
|
|
|
*/ |
|
60
|
4 |
|
protected function getCSV($file_name, $search) |
|
61
|
|
|
{ |
|
62
|
4 |
|
$ch = fopen($file_name, "r"); |
|
63
|
4 |
|
$header_row = fgetcsv($ch); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
// This will loop through all the rows until it reaches the end |
|
66
|
4 |
|
while ($row = fgetcsv($ch, 0, ";")) { |
|
67
|
4 |
|
if ($this->substr_in_array($search, $row)) { |
|
68
|
2 |
|
return $row; |
|
69
|
|
|
} |
|
70
|
4 |
|
} |
|
71
|
2 |
|
return 0; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* A version of in_array() that does a sub string match on $needle |
|
76
|
|
|
* |
|
77
|
|
|
* @param mixed $needle The searched value |
|
78
|
|
|
* @param array $haystack The array to search in |
|
79
|
|
|
* @return boolean |
|
80
|
|
|
*/ |
|
81
|
|
|
function substr_in_array($needle, array $haystack) |
|
|
|
|
|
|
82
|
|
|
{ |
|
83
|
4 |
|
$filtered = array_filter($haystack, function ($item) use ($needle) { |
|
84
|
4 |
|
return false !== strpos($item, $needle); |
|
85
|
4 |
|
}); |
|
86
|
|
|
|
|
87
|
4 |
|
return !empty($filtered); |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.