|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @license MIT |
|
7
|
|
|
* @copyright 2013 - 2017 Cross Solution <http://cross-solution.de> |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
/** */ |
|
11
|
|
|
namespace Geo\Service; |
|
12
|
|
|
|
|
13
|
|
|
use Zend\Http\Client; |
|
14
|
|
|
use Zend\Json\Json; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* ${CARET} |
|
18
|
|
|
* |
|
19
|
|
|
* @author Mathias Gelhausen <[email protected]> |
|
20
|
|
|
* @todo write test |
|
21
|
|
|
*/ |
|
22
|
|
|
class AbstractClient |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* |
|
26
|
|
|
* |
|
27
|
|
|
* @var \Zend\Http\Client |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $client; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* |
|
33
|
|
|
* |
|
34
|
|
|
* @var \Zend\Cache\Storage\Adapter\AbstractAdapter |
|
35
|
|
|
*/ |
|
36
|
|
|
protected $cache; |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
public function __construct($uri, $cache = false) |
|
40
|
|
|
{ |
|
41
|
|
|
$this->client = $this->setupClient($uri); |
|
42
|
|
|
$this->cache = $cache; |
|
|
|
|
|
|
43
|
|
|
|
|
44
|
|
|
// $namespace = explode('\\', get_class($this)); |
|
|
|
|
|
|
45
|
|
|
// $namespace = array_pop($namespace); |
|
46
|
|
|
// //$this->cache->setOptions(['namespace' => $namespace]); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected function setupClient($uri) |
|
50
|
|
|
{ |
|
51
|
|
|
return new Client($uri); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
protected function preQuery($term, array $params) |
|
55
|
|
|
{ |
|
56
|
|
|
$params['q'] = $term; |
|
57
|
|
|
$this->client->setParameterGet($params); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function query($term, array $params = []) |
|
61
|
|
|
{ |
|
62
|
|
|
$cacheId = md5($term); |
|
63
|
|
|
|
|
64
|
|
|
if ($this->cache && ($result = $this->cache->getItem($cacheId))) { |
|
65
|
|
|
return $result; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
$this->preQuery($term, $params); |
|
69
|
|
|
|
|
70
|
|
|
$response = $this->client->send(); |
|
71
|
|
|
if ($response->getStatusCode() !== 200) { |
|
72
|
|
|
throw new \RuntimeException('Query failed, because ' . $response->getReasonPhrase()); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
$result = $response->getBody(); |
|
76
|
|
|
$result = $this->processResult($result); |
|
77
|
|
|
|
|
78
|
|
|
if ($this->cache) { |
|
79
|
|
|
$this->cache->setItem($cacheId, $result); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $result; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
public function queryOne($term) |
|
86
|
|
|
{ |
|
87
|
|
|
$result = $this->query($term); |
|
88
|
|
|
$result = Json::decode($result, Json::TYPE_ARRAY); |
|
89
|
|
|
|
|
90
|
|
|
return isset($result[0]) ? $result[0] : false; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
protected function processResult($result) |
|
94
|
|
|
{ |
|
95
|
|
|
return $result; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..