-
Notifications
You must be signed in to change notification settings - Fork 301
/
Copy pathResult.php
139 lines (123 loc) · 3.42 KB
/
Result.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
<?php
/*
* This file is part of the Solarium package.
*
* For the full copyright and license information, please view the COPYING
* file that was distributed with this source code.
*/
namespace Solarium\Core\Query\Result;
use Solarium\Core\Client\Response;
use Solarium\Core\Query\AbstractQuery;
use Solarium\Core\Query\Status4xxNoExceptionInterface;
use Solarium\Exception\HttpException;
use Solarium\Exception\RuntimeException;
use Solarium\Exception\UnexpectedValueException;
/**
* Query result.
*
* This base class provides access to the response and decoded data. If you need more functionality
* like resultset parsing use one of the subclasses
*/
class Result implements ResultInterface, \JsonSerializable
{
/**
* Response object.
*
* @var Response
*/
protected $response;
/**
* Decoded response data.
*
* This is lazy loaded, {@link getData()}
*
* @var array
*/
protected $data;
/**
* Query used for this request.
*
* @var AbstractQuery
*/
protected $query;
/**
* Constructor.
*
* @param AbstractQuery $query
* @param Response $response
*
* @throws HttpException
*/
public function __construct(AbstractQuery $query, Response $response)
{
$this->query = $query;
$this->response = $response;
// by default, a status of 400 or above is considered an error
$errorStatus = 400;
// some query types expect 4xx statuses as a valid response
if ($query instanceof Status4xxNoExceptionInterface) {
$errorStatus = 500;
}
// check status for error
if ($response->getStatusCode() >= $errorStatus) {
throw new HttpException($response->getStatusMessage(), $response->getStatusCode(), $response->getBody());
}
}
/**
* Get response object.
*
* This is the raw HTTP response object, not the parsed data!
*
* @return Response
*/
public function getResponse(): Response
{
return $this->response;
}
/**
* Get query instance.
*
* @return AbstractQuery
*/
public function getQuery(): AbstractQuery
{
return $this->query;
}
/**
* Get Solr response data.
*
* Includes a lazy loading mechanism: JSON body data is decoded on first use and then saved for reuse.
*
* @throws UnexpectedValueException
* @throws RuntimeException
*
* @return array
*/
public function getData(): array
{
if (null === $this->data) {
switch ($this->query->getResponseWriter()) {
case AbstractQuery::WT_PHPS:
$this->data = unserialize($this->response->getBody(), [false]);
break;
case AbstractQuery::WT_JSON:
$this->data = json_decode($this->response->getBody(), true);
break;
default:
throw new RuntimeException(sprintf('Responseparser cannot handle %s', $this->query->getResponseWriter()));
}
if (null === $this->data) {
throw new UnexpectedValueException('Solr JSON response could not be decoded');
}
}
return $this->data;
}
#[\ReturnTypeWillChange]
/**
* {@inheritdoc}
*/
public function jsonSerialize()
{
return $this->getData();
}
}