|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by Maciej Paprocki for Bureau-VA. |
|
4
|
|
|
* Date: 17/02/2016 |
|
5
|
|
|
* Project Name: MaciekPaprocki\WordpressGuzzle |
|
6
|
|
|
* Time: 10:46 |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace BureauVa\WordpressGuzzle\Repository; |
|
10
|
|
|
|
|
11
|
|
|
use GuzzleHttp\Promise\Promise; |
|
12
|
|
|
use GuzzleHttp\Psr7\Response; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* Class AbstractRepository |
|
16
|
|
|
* @package MaciekPaprocki\WordpressGuzzle |
|
17
|
|
|
*/ |
|
18
|
|
|
abstract class RepositoryAbstract implements RepositoryInterface |
|
19
|
|
|
{ |
|
20
|
|
|
protected $client; |
|
21
|
|
|
protected $reducers = ['BureauVa\\WordpressGuzzle\\Reducer\\CastType::castType']; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* let us keep reference to our client |
|
25
|
|
|
* @return mixed |
|
26
|
|
|
*/ |
|
27
|
|
|
public function getClient() |
|
28
|
|
|
{ |
|
29
|
|
|
return $this->client; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Let us set our client reference taken from transaction |
|
34
|
|
|
* @param $client |
|
35
|
|
|
* @return RepositoryAbstract |
|
36
|
|
|
*/ |
|
37
|
|
|
public function setClient($client) |
|
38
|
|
|
{ |
|
39
|
|
|
$this->client = $client; |
|
40
|
|
|
return $this; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @param $data |
|
45
|
|
|
* @return string |
|
46
|
|
|
*/ |
|
47
|
|
|
public function createRequestQuery($data) |
|
48
|
|
|
{ |
|
49
|
|
|
if (empty($data)) { |
|
50
|
|
|
return ''; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return http_build_query([ |
|
54
|
|
|
'filter' => $data |
|
55
|
|
|
]); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param $address |
|
60
|
|
|
* @param null|array|object $parameters |
|
61
|
|
|
* @return Promise |
|
62
|
|
|
*/ |
|
63
|
|
|
public function createPromise($address, $parameters = null) |
|
64
|
|
|
{ |
|
65
|
|
|
$encodedParam = $this->createRequestQuery($parameters); |
|
66
|
|
|
$query = $encodedParam ? '?' . $encodedParam : ''; |
|
67
|
|
|
$reducers = $this->getReducers(); |
|
68
|
|
|
return $this->client->requestAsync('GET', $address . $query) |
|
69
|
|
|
->then(function (Response $res) use ($reducers) { |
|
70
|
|
|
|
|
71
|
|
|
$data = \json_decode((string)$res->getBody()); |
|
72
|
|
|
|
|
73
|
|
|
if ($data instanceof \stdClass) { |
|
74
|
|
|
foreach ($reducers as $reducer) { |
|
75
|
|
|
$data = \call_user_func($reducer, $data); |
|
76
|
|
|
} |
|
77
|
|
|
} elseif (is_array($data)) { |
|
78
|
|
|
foreach ($data as $key => $val) { |
|
79
|
|
|
foreach ($reducers as $reducer) { |
|
80
|
|
|
$data[$key] = \call_user_func($reducer, $val); |
|
81
|
|
|
} |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
return $data; |
|
86
|
|
|
}, function () { |
|
87
|
|
|
echo 'error'; |
|
88
|
|
|
}); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return array |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getReducers() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->reducers; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param $reducers |
|
101
|
|
|
* @return $this |
|
102
|
|
|
*/ |
|
103
|
|
|
public function setReducers($reducers) |
|
104
|
|
|
{ |
|
105
|
|
|
$this->reducers = $reducers; |
|
106
|
|
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $reducer |
|
111
|
|
|
* @return $this |
|
112
|
|
|
*/ |
|
113
|
|
|
public function addReducer($reducer) |
|
114
|
|
|
{ |
|
115
|
|
|
$this->reducers[] = $reducer; |
|
116
|
|
|
return $this; |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|