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
|
|
|
* @param string $country Country name |
33
|
|
|
* |
34
|
|
|
* $return integer |
35
|
|
|
*/ |
36
|
2 |
|
public function getCountryCode($country) |
37
|
|
|
{ |
38
|
2 |
|
$rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Landekoder.csv", $country); |
39
|
2 |
|
$stored_in_column = 3; |
40
|
2 |
|
return $rows[$stored_in_column]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Gets municipality code |
45
|
|
|
* |
46
|
|
|
* @param string $municipality Municipality name |
47
|
|
|
* |
48
|
|
|
* $return integer |
49
|
|
|
*/ |
50
|
2 |
|
public function getMunicipalityCode($municipality) |
51
|
|
|
{ |
52
|
2 |
|
$rows = $this->getCSV(DIRNAME(__FILE__) . "/data/Kommuner.csv", $municipality); |
53
|
2 |
|
$stored_in_column = 2; |
54
|
2 |
|
return $rows[$stored_in_column]; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Remove spaces from phone number |
59
|
|
|
* |
60
|
|
|
* $param string $phone Phone number |
61
|
|
|
* |
62
|
|
|
* $return integer |
63
|
|
|
*/ |
64
|
|
|
public function fixPhoneNumber($phonenumber) |
65
|
|
|
{ |
66
|
|
|
$string = preg_replace('/\s+/', '', $phonenumber); |
67
|
|
|
return $string; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Loops through csv file and stops with search |
73
|
|
|
* |
74
|
|
|
* @param $file_name |
75
|
|
|
* @param $search |
76
|
|
|
* @return array |
77
|
|
|
*/ |
78
|
4 |
|
protected function getCSV($file_name, $search) |
79
|
|
|
{ |
80
|
4 |
|
$ch = fopen($file_name, "r"); |
81
|
4 |
|
$header_row = fgetcsv($ch); |
|
|
|
|
82
|
|
|
|
83
|
|
|
// This will loop through all the rows until it reaches the end |
84
|
4 |
|
while ($row = fgetcsv($ch, 0, ";")) { |
85
|
4 |
|
if ($this->substrInArray($search, $row)) { |
86
|
2 |
|
return $row; |
87
|
|
|
} |
88
|
4 |
|
} |
89
|
2 |
|
return 0; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* A version of in_array() that does a sub string match on $needle |
94
|
|
|
* |
95
|
|
|
* @param mixed $needle The searched value |
96
|
|
|
* @param array $haystack The array to search in |
97
|
|
|
* @return boolean |
98
|
|
|
*/ |
99
|
|
|
private function substrInArray($needle, array $haystack) |
100
|
|
|
{ |
101
|
4 |
|
$filtered = array_filter($haystack, function ($item) use ($needle) { |
102
|
4 |
|
return false !== strpos($item, $needle); |
103
|
4 |
|
}); |
104
|
|
|
|
105
|
4 |
|
return !empty($filtered); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.