1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Part of the Saudi Address API PHP package. |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* Licensed under the MIT. |
9
|
|
|
* |
10
|
|
|
* This source file is subject to the MIT License that is |
11
|
|
|
* bundled with this package in the LICENSE file. |
12
|
|
|
* |
13
|
|
|
* @package Saudi Address |
14
|
|
|
* @version 2.0 |
15
|
|
|
* @author Ali Alharthi |
16
|
|
|
* @license MIT |
17
|
|
|
* @copyright (c) 2020, Ali Alharthi |
18
|
|
|
* @link https://aalharthi.sa |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AliAlharthi\SaudiAddress; |
22
|
|
|
|
23
|
|
|
class Config implements ConfigInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* The current package version. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
protected $version = 2.0; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The Saudi National Address API key. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $apiKey; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The Saudi National Address API subscription. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $apiSubscription; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Default locale. |
48
|
|
|
* API only supports English and Arabic languages (API Default is Arabic) |
49
|
|
|
* |
50
|
|
|
* @var string |
51
|
|
|
*/ |
52
|
|
|
protected $locale = 'E'; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Enabling Cache. |
56
|
|
|
* |
57
|
|
|
* @var bool |
58
|
|
|
*/ |
59
|
|
|
protected $cache = true; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Constructor. |
63
|
|
|
* |
64
|
|
|
* @param string $apiKey |
65
|
|
|
* @param string $apiSubscription |
66
|
|
|
* @param string $locale |
67
|
|
|
* @param bool $cache |
68
|
|
|
*/ |
69
|
|
|
public function __construct($apiKey, $apiSubscription = 'Development', $locale = 'E', $cache = false) |
70
|
|
|
{ |
71
|
|
|
$this->setApiKey($apiKey ?: self::getEnvVariable('SNA_API_KEY', '')); |
72
|
|
|
$this->setApiSubscription($apiSubscription); |
73
|
|
|
$this->setLocale($locale); |
74
|
|
|
$this->setCache($cache); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get env Variable. |
79
|
|
|
* |
80
|
|
|
* @param string $name |
81
|
|
|
* @param string|null $default |
82
|
|
|
* @return string|null |
83
|
|
|
*/ |
84
|
|
|
private static function getEnvVariable($name, $default = null) |
85
|
|
|
{ |
86
|
|
|
if (isset($_SERVER[$name])) { |
87
|
|
|
return (string) $_SERVER[$name]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
if (PHP_SAPI === 'cli' && ($value = getenv($name)) !== false) { |
91
|
|
|
return (string) $value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
return $default; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Returns the package version. |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
|
|
public function getVersion() |
103
|
|
|
{ |
104
|
|
|
return $this->version; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Returns the Saudi National Address API key. |
109
|
|
|
* |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getApiKey() |
113
|
|
|
{ |
114
|
|
|
return $this->apiKey; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* Sets the Saudi National Address API key. |
119
|
|
|
* |
120
|
|
|
* @param string $apiKey |
121
|
|
|
* @return Config |
122
|
|
|
*/ |
123
|
|
|
public function setApiKey($apiKey) |
124
|
|
|
{ |
125
|
|
|
$this->apiKey = $apiKey; |
126
|
|
|
|
127
|
|
|
return $this; |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* Returns the Saudi National Address API subscription type. |
132
|
|
|
* |
133
|
|
|
* @return string |
134
|
|
|
*/ |
135
|
|
|
public function getApiSubscription() |
136
|
|
|
{ |
137
|
|
|
return $this->apiSubscription; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Sets the Saudi National Address API subscription type. |
142
|
|
|
* |
143
|
|
|
* @param string $apiSubscription |
144
|
|
|
* @return Config |
145
|
|
|
*/ |
146
|
|
|
public function setApiSubscription($apiSubscription) |
147
|
|
|
{ |
148
|
|
|
$this->apiSubscription = $apiSubscription; |
149
|
|
|
|
150
|
|
|
return $this; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* Returns the locale. |
155
|
|
|
* |
156
|
|
|
* @return string |
157
|
|
|
*/ |
158
|
|
|
public function getLocale() |
159
|
|
|
{ |
160
|
|
|
return $this->locale; |
161
|
|
|
} |
162
|
|
|
|
163
|
|
|
/** |
164
|
|
|
* Sets the locale. |
165
|
|
|
* |
166
|
|
|
* @param string $locale |
167
|
|
|
* @return Config |
168
|
|
|
*/ |
169
|
|
|
public function setLocale($locale) |
170
|
|
|
{ |
171
|
|
|
$this->locale = $locale; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Returns the cache status. |
178
|
|
|
* |
179
|
|
|
* @return bool |
180
|
|
|
*/ |
181
|
|
|
public function getCache() |
182
|
|
|
{ |
183
|
|
|
return $this->cache; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** |
187
|
|
|
* Sets the cache status. |
188
|
|
|
* |
189
|
|
|
* @param bool $cache |
190
|
|
|
* @return Config |
191
|
|
|
*/ |
192
|
|
|
public function setCache($cache) |
193
|
|
|
{ |
194
|
|
|
$this->cache = $cache; |
195
|
|
|
|
196
|
|
|
return $this; |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
|