1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Departements\Datasource; |
4
|
|
|
|
5
|
|
|
use PhpCollection\Map; |
6
|
|
|
|
7
|
|
|
abstract class AbstractDatasource implements DatasourceInterface |
8
|
|
|
{ |
9
|
|
|
protected $regions; |
10
|
|
|
protected $departements; |
11
|
|
|
protected $index; |
12
|
|
|
|
13
|
|
|
public function __construct() { |
14
|
|
|
$this->regions = new Map(); |
15
|
|
|
$this->departements = new Map(); |
16
|
|
|
$this->communes = new Map(); |
|
|
|
|
17
|
|
|
$this->index = array( |
18
|
|
|
'departements' => array(), |
19
|
|
|
'regions' => array(), |
20
|
|
|
'communes' => array() |
21
|
|
|
); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
protected function slugify($value) { |
25
|
|
|
$orginalLocale = setlocale(LC_ALL, 0); |
26
|
|
|
setlocale(LC_ALL, "en_US.utf8"); |
27
|
|
|
$value = strtolower(iconv('UTF-8', 'ASCII//TRANSLIT', $value)); |
28
|
|
|
setlocale(LC_ALL, $orginalLocale); |
29
|
|
|
return preg_replace("/[^a-z0-9]/", "", $value); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function addToIndex($type, $value, $key) { |
33
|
|
|
$this->index[$type][$this->slugify($value)] = $key; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function findAllDepartements($sortByValue = false) { |
37
|
|
|
if (!$sortByValue) { |
38
|
|
|
return $this->departements; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
return $this->sortByValue($this->departements); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function findAllRegions($sortByValue = false) { |
45
|
|
|
if (!$sortByValue) { |
46
|
|
|
return $this->regions; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
return $this->sortByValue($this->regions); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function findAllCommunes() { |
53
|
|
|
return $this->communes; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function findCommunesByZipcode($zipcode) { |
57
|
|
|
return $this->communes->get($zipcode)->get(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function findDepartementByCode($departementCode) { |
61
|
|
|
return $this->departements->get($departementCode)->getOrElse(null); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
public function findDepartementByName($departementName) { |
|
|
|
|
65
|
|
|
$departementName = $this->slugify($departementName); |
66
|
|
|
|
67
|
|
|
if(!isset($this->index['departements'][$departementName])) { |
68
|
|
|
return null; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
$departementCode = $this->index['departements'][$departementName]; |
72
|
|
|
|
73
|
|
|
return $this->departements->get($departementCode)->get(); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
public function findRegionByName($regionName) { |
|
|
|
|
77
|
|
|
$regionName = $this->slugify($regionName); |
78
|
|
|
|
79
|
|
|
if(!isset($this->index['regions'][$regionName])) { |
80
|
|
|
return null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$regionCode = $this->index['regions'][$regionName]; |
84
|
|
|
|
85
|
|
|
return $this->regions->get($regionCode)->get(); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
public function findRegionByCode($regionCode) { |
89
|
|
|
return $this->regions->get($regionCode)->getOrElse(null); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
protected function sortByValue($collection) { |
93
|
|
|
$collator = new \Collator('fr_FR'); |
94
|
|
|
|
95
|
|
|
$items = iterator_to_array($collection); |
96
|
|
|
$collator->asort($items); |
97
|
|
|
|
98
|
|
|
return new Map($items); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: