1 <?php
2 namespace OpenDNS\MES;
3
4 /**
5 * Base class for API responses.
6 *
7 * Accessible as an array, but you can't change any values.
8 */
9 class Response implements \ArrayAccess
10 {
11 /** @var Guzzle\Http\Message\Response Raw Guzzle response from the contructor args */
12 protected $guzzleResponse;
13
14 /** @var string[] Parsed keys/values from the MeS response body */
15 protected $fields = array();
16
17 /**
18 * @param Guzzle\Http\Message\Response $guzzleResponse Raw Guzzle response from the contructor args
19 */
20 public function __construct($guzzleResponse)
21 {
22 $this->guzzleResponse = $guzzleResponse;
23 parse_str((string) $this->guzzleResponse, $this->fields);
24 }
25
26 /**
27 * Get the response body as a JSON object
28 *
29 * @return string JSON string representing the MeS API response
30 */
31 public function json()
32 {
33 return json_encode($this->fields);
34 }
35
36 /**
37 * Get the response body as a stream resource
38 *
39 * This is mostly useful for the reporting API, which returns CSV. You can use this together with fgetcsv().
40 *
41 * @return resource PHP stream resource representing the MeS response body
42 */
43 public function getResponseBodyStream()
44 {
45 $stream = $this->guzzleResponse->getStream();
46 rewind($stream);
47 return $stream;
48 }
49
50 /**
51 * Test whether a key exists in the response object
52 *
53 * @internal Exists mainly for ArrayAccess.
54 * @param string|int $offset
55 * @return bool
56 */
57 public function offsetExists($offset)
58 {
59 return isset($this->fields[$offset]);
60 }
61
62 /**
63 * Get a response value by its key
64 *
65 * @internal Exists mainly for ArrayAccess.
66 * @param string|int $offset
67 * @return string
68 */
69 public function offsetGet($offset) {
70 return $this->fields[$offset];
71 }
72
73 /**
74 * Literally does nothing.
75 *
76 * @internal Exists mainly for ArrayAccess.
77 * @ignore
78 * @codeCoverageIgnore
79 * @param string|int $offset
80 * @param mixed value
81 * @return void
82 */
83 public function offsetSet($offset, $value) {}
84
85 /**
86 * Literally does nothing.
87 *
88 * @internal Exists mainly for ArrayAccess.
89 * @ignore
90 * @codeCoverageIgnore
91 * @param string|int $offset
92 * @return void
93 */
94 public function offsetUnset($offset) {}
95
96 }
97