1 | <?php |
||
35 | abstract class Api implements ApiInterface |
||
36 | { |
||
37 | const API_BASE_URL = 'https://apina.address.gov.sa/NationalAddress/'; |
||
38 | const USER_AGENT_SUFFIX = "alharthi-saudi-address-api-php-client/"; |
||
39 | const IN_CHARACTER_ENCODING = 'windows-1256'; |
||
40 | const OUT_CHARACTER_ENCODING = 'UTF-8'; |
||
41 | |||
42 | /** |
||
43 | * The Config repository instance. |
||
44 | * |
||
45 | * @var \AliAlharthi\SaudiAddress\ConfigInterface |
||
46 | */ |
||
47 | protected $config; |
||
48 | |||
49 | /** |
||
50 | * Constructor. |
||
51 | * |
||
52 | * @param \AliAlharthi\SaudiAddress\ConfigInterface $config |
||
53 | */ |
||
54 | public function __construct(ConfigInterface $config) |
||
55 | { |
||
56 | $this->config = $config; |
||
57 | } |
||
58 | |||
59 | /** |
||
60 | * Returns the Saudi National Address API endpoint. |
||
61 | * |
||
62 | * @return string |
||
63 | */ |
||
64 | public function baseUrl() |
||
65 | { |
||
66 | return self::API_BASE_URL; |
||
67 | } |
||
68 | |||
69 | /** |
||
70 | * Execute a get request. |
||
71 | * |
||
72 | * @param string $url |
||
73 | * @param string $lang |
||
74 | * @param array $parameters |
||
75 | * @param bool $encode |
||
76 | * @return null|array |
||
77 | */ |
||
78 | public function _get($url = null, $lang = 'A', $parameters = [], $encode = true) |
||
79 | { |
||
80 | return $this->execute('get', $url, $lang, $parameters, $encode); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * Execute a request. |
||
85 | * |
||
86 | * @param string $httpMethod |
||
87 | * @param string $url |
||
88 | * @param string $lang |
||
89 | * @param array $parameters |
||
90 | * @param bool $encode |
||
91 | * @return null|array |
||
92 | * @throws AliAlharthi\SaudiAddress\Exception\Handler |
||
93 | */ |
||
94 | public function execute($httpMethod, $url, $lang = 'A', array $parameters = [], $encode = true) |
||
110 | |||
111 | /** |
||
112 | * Returns an Http client instance. |
||
113 | * |
||
114 | * @return \GuzzleHttp\Client |
||
115 | */ |
||
116 | protected function getClient() |
||
122 | |||
123 | /** |
||
124 | * Create the client handler. |
||
125 | * |
||
126 | * @return \GuzzleHttp\HandlerStack |
||
127 | * @throws \RuntimeException |
||
128 | */ |
||
129 | protected function createHandler() |
||
157 | |||
158 | /** |
||
159 | * Generates the main user agent string. |
||
160 | * |
||
161 | * @return string |
||
162 | */ |
||
163 | protected function generateUserAgent() |
||
167 | |||
168 | /** |
||
169 | * Return a value from cache. |
||
170 | * |
||
171 | * @param string $file |
||
172 | * @return array|null |
||
173 | */ |
||
174 | protected function cacheValue($file) |
||
182 | } |
||
183 |