1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\LazyJsonPages; |
4
|
|
|
|
5
|
|
|
use Cerbero\LazyJsonPages\Exceptions\LazyJsonPagesException; |
6
|
|
|
use GuzzleHttp\Client; |
7
|
|
|
use Illuminate\Http\Client\Response; |
8
|
|
|
use Illuminate\Support\Arr; |
9
|
|
|
use Psr\Http\Message\RequestInterface; |
10
|
|
|
use Psr\Http\Message\ResponseInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* The source wrapper. |
14
|
|
|
* |
15
|
|
|
*/ |
16
|
|
|
class SourceWrapper |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* The original source. |
20
|
|
|
* |
21
|
|
|
* @var RequestInterface|Response |
22
|
|
|
*/ |
23
|
|
|
public $original; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The HTTP request. |
27
|
|
|
* |
28
|
|
|
* @var RequestInterface |
29
|
|
|
*/ |
30
|
|
|
public $request; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* The HTTP response. |
34
|
|
|
* |
35
|
|
|
* @var ResponseInterface |
36
|
|
|
*/ |
37
|
|
|
public $response; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The decoded JSON. |
41
|
|
|
* |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
protected $decodedJson; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Instantiate the class. |
48
|
|
|
* |
49
|
|
|
* @param RequestInterface|Response $source |
50
|
|
|
* |
51
|
|
|
* @throws LazyJsonPagesException |
52
|
|
|
*/ |
53
|
18 |
|
public function __construct($source) |
54
|
|
|
{ |
55
|
18 |
|
if (!$source instanceof RequestInterface && !$source instanceof Response) { |
|
|
|
|
56
|
1 |
|
throw new LazyJsonPagesException('The provided JSON source is not valid.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
17 |
|
$this->original = $source; |
60
|
17 |
|
$this->request = $this->getSourceRequest(); |
61
|
16 |
|
$this->response = $this->getSourceResponse(); |
62
|
16 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Retrieve the HTTP request of the source |
66
|
|
|
* |
67
|
|
|
* @return RequestInterface |
68
|
|
|
* |
69
|
|
|
* @throws LazyJsonPagesException |
70
|
|
|
*/ |
71
|
17 |
|
protected function getSourceRequest(): RequestInterface |
72
|
|
|
{ |
73
|
17 |
|
if ($this->original instanceof RequestInterface) { |
74
|
15 |
|
return $this->original; |
75
|
2 |
|
} elseif (isset($this->original->transferStats)) { |
76
|
1 |
|
return $this->original->transferStats->getRequest(); |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
throw new LazyJsonPagesException('The HTTP client response is not aware of the original request.'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieve the HTTP response of the source |
84
|
|
|
* |
85
|
|
|
* @return ResponseInterface |
86
|
|
|
*/ |
87
|
16 |
|
protected function getSourceResponse(): ResponseInterface |
88
|
|
|
{ |
89
|
16 |
|
if ($this->original instanceof RequestInterface) { |
90
|
15 |
|
return (new Client())->send($this->original); |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $this->original->toPsrResponse(); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retrieve a fragment of the decoded JSON |
98
|
|
|
* |
99
|
|
|
* @param string $path |
100
|
|
|
* @param mixed $default |
101
|
|
|
* @return mixed |
102
|
|
|
*/ |
103
|
13 |
|
public function json(string $path, $default = null) |
104
|
|
|
{ |
105
|
13 |
|
if (!isset($this->decodedJson)) { |
106
|
13 |
|
$this->decodedJson = json_decode((string) $this->response->getBody(), true); |
107
|
|
|
} |
108
|
|
|
|
109
|
13 |
|
return Arr::get($this->decodedJson, $path, $default); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|