Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
CollectionAPIResult | |
0.00% |
0 / 16 |
|
0.00% |
0 / 4 |
132 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
20 | |||
get | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
12 | |||
isError | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
12 | |||
getError | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Collection\Rendering; |
4 | |
5 | use MediaWiki\Json\FormatJson; |
6 | |
7 | /** |
8 | * A wrapper for data returned by the API |
9 | */ |
10 | class CollectionAPIResult { |
11 | /** @var array|null Decoded JSON returned by server */ |
12 | public $response = []; |
13 | |
14 | /** |
15 | * @param string|null $data Data returned by HTTP request |
16 | */ |
17 | public function __construct( $data ) { |
18 | if ( $data ) { |
19 | $this->response = FormatJson::decode( $data, true ); |
20 | if ( $this->response === null ) { |
21 | wfDebugLog( 'collection', "Server returned bogus data: $data" ); |
22 | $this->response = null; |
23 | } |
24 | if ( $this->isError() ) { |
25 | wfDebugLog( 'collection', "Server returned error: {$this->getError()}" ); |
26 | } |
27 | } |
28 | } |
29 | |
30 | /** |
31 | * Returns data for specified key(s) |
32 | * @param string $key |
33 | * @param string ...$keys |
34 | * @return mixed |
35 | */ |
36 | public function get( $key, ...$keys ) { |
37 | $val = $this->response; |
38 | foreach ( func_get_args() as $key ) { |
39 | if ( !isset( $val[$key] ) ) { |
40 | return ''; |
41 | } |
42 | $val = $val[$key]; |
43 | } |
44 | return $val; |
45 | } |
46 | |
47 | /** |
48 | * @return bool |
49 | */ |
50 | public function isError() { |
51 | return !$this->response |
52 | || ( isset( $this->response['error'] ) && $this->response['error'] ); |
53 | } |
54 | |
55 | /** |
56 | * @return string Internal (not user-facing) error description |
57 | */ |
58 | protected function getError() { |
59 | return $this->response['error'] ?? '(error unknown)'; |
60 | } |
61 | } |