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
|
|
|
use ReflectionClass; |
24
|
|
|
|
25
|
|
|
class SaudiAddress |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* The package version. |
29
|
|
|
* |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
const VERSION = 1.3; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* The Config repository instance. |
36
|
|
|
* |
37
|
|
|
* @var \AliAlharthi\SaudiAddress\ConfigInterface |
38
|
|
|
*/ |
39
|
|
|
protected $config; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor. |
43
|
|
|
* |
44
|
|
|
* @param string $apiKey |
45
|
|
|
* @param string $apiSubscription |
46
|
|
|
*/ |
47
|
|
|
public function __construct($apiKey = null, $apiSubscription = 'Development', $cache = false) |
48
|
|
|
{ |
49
|
|
|
$this->config = new Config($apiKey, $apiSubscription, $cache); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create a new Saudi National Address API instance. |
54
|
|
|
* |
55
|
|
|
* @param string $apiKey |
56
|
|
|
* @param string $apiSubscription |
57
|
|
|
* @return SaudiAddress |
58
|
|
|
*/ |
59
|
|
|
public static function make($apiKey = null, $apiSubscription = 'Development', $cache = false) |
60
|
|
|
{ |
61
|
|
|
return new static($apiKey, $apiSubscription, $cache); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Returns the current package version. |
66
|
|
|
* |
67
|
|
|
* @return double |
68
|
|
|
*/ |
69
|
|
|
public static function getVersion() |
70
|
|
|
{ |
71
|
|
|
return self::VERSION; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Returns the Config repository instance. |
76
|
|
|
* |
77
|
|
|
* @return \AliAlharthi\SaudiAddress\ConfigInterface |
78
|
|
|
*/ |
79
|
|
|
public function getConfig() |
80
|
|
|
{ |
81
|
|
|
return $this->config; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Sets the Config repository instance. |
86
|
|
|
* |
87
|
|
|
* @param \AliAlharthi\SaudiAddress\ConfigInterface $config |
88
|
|
|
* @return SaudiAddress |
89
|
|
|
*/ |
90
|
|
|
public function setConfig(ConfigInterface $config) |
91
|
|
|
{ |
92
|
|
|
$this->config = $config; |
93
|
|
|
|
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
/** |
97
|
|
|
* Returns the Saudi National Address API key. |
98
|
|
|
* |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public function getApiKey() |
102
|
|
|
{ |
103
|
|
|
return $this->config->getApiKey(); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* Sets the Saudi National Address API key. |
108
|
|
|
* |
109
|
|
|
* @param string $apiKey |
110
|
|
|
* @return SaudiAddress |
111
|
|
|
*/ |
112
|
|
|
public function setApiKey($apiKey) |
113
|
|
|
{ |
114
|
|
|
$this->config->setApiKey($apiKey); |
115
|
|
|
|
116
|
|
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Dynamically handle missing methods. |
121
|
|
|
* |
122
|
|
|
* @param string $method |
123
|
|
|
* @param array $parameters |
124
|
|
|
* @return \AliAlharthi\SaudiAddress\Api\ApiInterface |
125
|
|
|
*/ |
126
|
|
|
public function __call($method, array $parameters) |
127
|
|
|
{ |
128
|
|
|
return $this->getApiInstance($method); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Returns the Api class instance for the given method. |
133
|
|
|
* |
134
|
|
|
* @param string $method |
135
|
|
|
* @return \AliAlharthi\SaudiAddress\Api\ApiInterface |
136
|
|
|
* @throws \BadMethodCallException |
137
|
|
|
*/ |
138
|
|
|
protected function getApiInstance($method) |
139
|
|
|
{ |
140
|
|
|
$class = "\\AliAlharthi\SaudiAddress\\Api\\" . ucwords($method); |
141
|
|
|
|
142
|
|
|
if (class_exists($class) && !(new ReflectionClass($class))->isAbstract()) { |
143
|
|
|
return new $class($this->config); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
throw new \BadMethodCallException("Undefined method [{$method}] called."); |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|