1
|
|
|
<?php |
2
|
|
|
namespace PlaygroundCore\Service; |
3
|
|
|
|
4
|
|
|
use Zend\ServiceManager\ServiceManagerAwareInterface; |
5
|
|
|
use Zend\ServiceManager\ServiceManager; |
6
|
|
|
use ZfcBase\EventManager\EventProvider; |
7
|
|
|
|
8
|
|
|
final class Country extends EventProvider implements ServiceManagerAwareInterface |
9
|
|
|
{ |
10
|
|
|
private $translatedTo; |
11
|
|
|
|
12
|
|
|
private $path; |
13
|
|
|
|
14
|
|
|
private $corePath; |
15
|
|
|
|
16
|
|
|
public function getTranslatedTo() |
17
|
|
|
{ |
18
|
|
|
return $this->translatedTo; |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function setTranslatedTo($translatedTo) |
22
|
|
|
{ |
23
|
|
|
$this->translatedTo = (string) $translatedTo; |
24
|
|
|
|
25
|
|
|
return $this; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function getPath() |
29
|
|
|
{ |
30
|
|
|
if(empty($this->path)){ |
31
|
|
|
$this->path = str_replace('\\', '/', getcwd()) . '/language/countries'; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->path; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function getCorePath() |
38
|
|
|
{ |
39
|
|
|
if(empty($this->corePath)){ |
40
|
|
|
$this->corePath = __DIR__ . '/../../../language/countries'; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
return $this->corePath; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function getAllCountries($translatedTo = null) |
47
|
|
|
{ |
48
|
|
|
if (null === $translatedTo) { |
49
|
|
|
$translatedTo = $this->getServiceManager()->get('translator')->getLocale(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$fileName = $this->getPath().'/'.$translatedTo.'.php'; |
53
|
|
View Code Duplication |
if (! file_exists($fileName)) { |
|
|
|
|
54
|
|
|
$fileName = $this->getCorePath().'/'.$translatedTo.'.php'; |
55
|
|
|
|
56
|
|
|
if (! file_exists($fileName)) { |
57
|
|
|
throw new \InvalidArgumentException("Language $translatedTo not found."); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
return include $fileName; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function getCountry($country, $translatedTo = null) |
65
|
|
|
{ |
66
|
|
|
if (null === $translatedTo) { |
67
|
|
|
$translatedTo = $this->getServiceManager()->get('translator')->getLocale(); |
68
|
|
|
} |
69
|
|
|
$fileName = $this->getPath().'/'.$translatedTo.'.php'; |
70
|
|
View Code Duplication |
if (! file_exists($fileName)) { |
|
|
|
|
71
|
|
|
$fileName = $this->getCorePath().'/'.$translatedTo.'.php'; |
72
|
|
|
if (! file_exists($fileName)) { |
73
|
|
|
throw new \InvalidArgumentException("Language $translatedTo not found."); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$list = include $fileName; |
78
|
|
|
if (!is_array($list)) { |
79
|
|
|
throw new \InvalidArgumentException("Language $translatedTo not found."); |
80
|
|
|
} |
81
|
|
|
$country = strtoupper($country); |
82
|
|
|
if (!array_key_exists($country, $list)) { |
83
|
|
|
throw new \InvalidArgumentException("Country $country not found for $translatedTo."); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
return $list[$country]; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* Retrieve service manager instance |
91
|
|
|
* |
92
|
|
|
* @return ServiceManager |
93
|
|
|
*/ |
94
|
|
|
public function getServiceManager() |
95
|
|
|
{ |
96
|
|
|
return $this->serviceManager; |
|
|
|
|
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Set service manager instance |
101
|
|
|
* |
102
|
|
|
* @param ServiceManager $serviceManager |
103
|
|
|
* @return User |
104
|
|
|
*/ |
105
|
|
|
public function setServiceManager(ServiceManager $serviceManager) |
106
|
|
|
{ |
107
|
|
|
$this->serviceManager = $serviceManager; |
108
|
|
|
|
109
|
|
|
return $this; |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.