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 1.3 |
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 = 1.3; |
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
|
|
|
* Enabling Cache |
48
|
|
|
* |
49
|
|
|
* @var bool |
50
|
|
|
*/ |
51
|
|
|
protected $cache = true; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Constructor. |
55
|
|
|
* |
56
|
|
|
* @param string $apiKey |
57
|
|
|
* @param string $apiSubscription |
58
|
|
|
*/ |
59
|
|
|
public function __construct($apiKey, $apiSubscription = 'Development', $cache = false) |
60
|
|
|
{ |
61
|
|
|
$this->setApiKey($apiKey ?: self::getEnvVariable('SNA_API_KEY', '')); |
62
|
|
|
$this->setApiSubscription($apiSubscription); |
63
|
|
|
$this->setCache($cache); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Get env Variable. |
68
|
|
|
* |
69
|
|
|
* @param string $name |
70
|
|
|
* @param string|null $default |
71
|
|
|
* @return string|null |
72
|
|
|
*/ |
73
|
|
|
private static function getEnvVariable($name, $default = null) |
74
|
|
|
{ |
75
|
|
|
if (isset($_SERVER[$name])) { |
76
|
|
|
return (string) $_SERVER[$name]; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
if (PHP_SAPI === 'cli' && ($value = getenv($name)) !== false) { |
80
|
|
|
return (string) $value; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
return $default; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Returns the package version. |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function getVersion() |
92
|
|
|
{ |
93
|
|
|
return $this->version; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Returns the Saudi National Address API key. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getApiKey() |
102
|
|
|
{ |
103
|
|
|
return $this->apiKey; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets the Saudi National Address API key. |
108
|
|
|
* |
109
|
|
|
* @param string $apiKey |
110
|
|
|
* @return Config |
111
|
|
|
*/ |
112
|
|
|
public function setApiKey($apiKey) |
113
|
|
|
{ |
114
|
|
|
$this->apiKey = $apiKey; |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Returns the Saudi National Address API subscription type. |
121
|
|
|
* |
122
|
|
|
* @return string |
123
|
|
|
*/ |
124
|
|
|
public function getApiSubscription() |
125
|
|
|
{ |
126
|
|
|
return $this->apiSubscription; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets the Saudi National Address API subscription type. |
131
|
|
|
* |
132
|
|
|
* @param string $apiSubscription |
133
|
|
|
* @return Config |
134
|
|
|
*/ |
135
|
|
|
public function setApiSubscription($apiSubscription) |
136
|
|
|
{ |
137
|
|
|
$this->apiSubscription = $apiSubscription; |
138
|
|
|
|
139
|
|
|
return $this; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Returns the cache status. |
144
|
|
|
* |
145
|
|
|
* @return bool |
146
|
|
|
*/ |
147
|
|
|
public function getCache() |
148
|
|
|
{ |
149
|
|
|
return $this->cache; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Sets the cache status. |
154
|
|
|
* |
155
|
|
|
* @param bool $cache |
156
|
|
|
* @return Config |
157
|
|
|
*/ |
158
|
|
|
public function setCache($cache) |
159
|
|
|
{ |
160
|
|
|
$this->cache = $cache; |
161
|
|
|
|
162
|
|
|
return $this; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|