1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\JsonParser\Sources; |
4
|
|
|
|
5
|
|
|
use Cerbero\JsonParser\Concerns\DetectsEndpoints; |
6
|
|
|
use Cerbero\JsonParser\Concerns\GuzzleAware; |
7
|
|
|
use Psr\Http\Message\ResponseInterface; |
8
|
|
|
use Psr\Http\Message\UriInterface; |
9
|
|
|
use Traversable; |
10
|
|
|
|
11
|
|
|
use function is_string; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* The endpoint source. |
15
|
|
|
* |
16
|
|
|
* @property-read UriInterface|string $source |
17
|
|
|
*/ |
18
|
|
|
class Endpoint extends Source |
19
|
|
|
{ |
20
|
|
|
use DetectsEndpoints; |
21
|
|
|
use GuzzleAware; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* The endpoint response. |
25
|
|
|
* |
26
|
|
|
* @var ResponseInterface|null |
27
|
|
|
*/ |
28
|
|
|
protected ?ResponseInterface $response; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Retrieve the JSON fragments |
32
|
|
|
* |
33
|
|
|
* @return Traversable<int, string> |
34
|
|
|
* @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException |
35
|
|
|
*/ |
36
|
1 |
|
public function getIterator(): Traversable |
37
|
|
|
{ |
38
|
1 |
|
return new Psr7Message($this->response(), $this->config); |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Retrieve the endpoint response |
43
|
|
|
* |
44
|
|
|
* @return ResponseInterface |
45
|
|
|
* @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException |
46
|
|
|
*/ |
47
|
1 |
|
protected function response(): ResponseInterface |
48
|
|
|
{ |
49
|
1 |
|
$this->requireGuzzle(); |
50
|
|
|
|
51
|
|
|
return $this->response ??= $this->getJson($this->source); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Determine whether the JSON source can be handled |
56
|
|
|
* |
57
|
|
|
* @return bool |
58
|
|
|
*/ |
59
|
128 |
|
public function matches(): bool |
60
|
|
|
{ |
61
|
|
|
// @phpstan-ignore-next-line |
62
|
128 |
|
return (is_string($this->source) || $this->source instanceof UriInterface) && $this->isEndpoint($this->source); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve the calculated size of the JSON source |
67
|
|
|
* |
68
|
|
|
* @return int|null |
69
|
|
|
* @throws \Cerbero\JsonParser\Exceptions\GuzzleRequiredException |
70
|
|
|
*/ |
71
|
|
|
protected function calculateSize(): ?int |
72
|
|
|
{ |
73
|
|
|
return $this->response()->getBody()->getSize(); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|