1<?php
2namespace GuzzleHttp\Message;
3
4use GuzzleHttp\Ring\Future\MagicFutureTrait;
5use GuzzleHttp\Ring\Future\FutureInterface;
6use GuzzleHttp\Stream\StreamInterface;
7
8/**
9 * Represents a response that has not been fulfilled.
10 *
11 * When created, you must provide a function that is used to dereference the
12 * future result and return it's value. The function has no arguments and MUST
13 * return an instance of a {@see GuzzleHttp\Message\ResponseInterface} object.
14 *
15 * You can optionally provide a function in the constructor that can be used to
16 * cancel the future from completing if possible. This function has no
17 * arguments and returns a boolean value representing whether or not the
18 * response could be cancelled.
19 *
20 * @property ResponseInterface $_value
21 */
22class FutureResponse implements ResponseInterface, FutureInterface
23{
24    use MagicFutureTrait;
25
26    /**
27     * Returns a FutureResponse that wraps another future.
28     *
29     * @param FutureInterface $future      Future to wrap with a new future
30     * @param callable        $onFulfilled Invoked when the future fulfilled
31     * @param callable        $onRejected  Invoked when the future rejected
32     * @param callable        $onProgress  Invoked when the future progresses
33     *
34     * @return FutureResponse
35     */
36    public static function proxy(
37        FutureInterface $future,
38        callable $onFulfilled = null,
39        callable $onRejected = null,
40        callable $onProgress = null
41    ) {
42        return new FutureResponse(
43            $future->then($onFulfilled, $onRejected, $onProgress),
44            [$future, 'wait'],
45            [$future, 'cancel']
46        );
47    }
48
49    public function getStatusCode()
50    {
51        return $this->_value->getStatusCode();
52    }
53
54    public function setStatusCode($code)
55    {
56        $this->_value->setStatusCode($code);
57    }
58
59    public function getReasonPhrase()
60    {
61        return $this->_value->getReasonPhrase();
62    }
63
64    public function setReasonPhrase($phrase)
65    {
66        $this->_value->setReasonPhrase($phrase);
67    }
68
69    public function getEffectiveUrl()
70    {
71        return $this->_value->getEffectiveUrl();
72    }
73
74    public function setEffectiveUrl($url)
75    {
76        $this->_value->setEffectiveUrl($url);
77    }
78
79    public function json(array $config = [])
80    {
81        return $this->_value->json($config);
82    }
83
84    public function xml(array $config = [])
85    {
86        return $this->_value->xml($config);
87    }
88
89    public function __toString()
90    {
91        try {
92            return $this->_value->__toString();
93        } catch (\Exception $e) {
94            trigger_error($e->getMessage(), E_USER_WARNING);
95            return '';
96        }
97    }
98
99    public function getProtocolVersion()
100    {
101        return $this->_value->getProtocolVersion();
102    }
103
104    public function setBody(StreamInterface $body = null)
105    {
106        $this->_value->setBody($body);
107    }
108
109    public function getBody()
110    {
111        return $this->_value->getBody();
112    }
113
114    public function getHeaders()
115    {
116        return $this->_value->getHeaders();
117    }
118
119    public function getHeader($header)
120    {
121        return $this->_value->getHeader($header);
122    }
123
124    public function getHeaderAsArray($header)
125    {
126        return $this->_value->getHeaderAsArray($header);
127    }
128
129    public function hasHeader($header)
130    {
131        return $this->_value->hasHeader($header);
132    }
133
134    public function removeHeader($header)
135    {
136        $this->_value->removeHeader($header);
137    }
138
139    public function addHeader($header, $value)
140    {
141        $this->_value->addHeader($header, $value);
142    }
143
144    public function addHeaders(array $headers)
145    {
146        $this->_value->addHeaders($headers);
147    }
148
149    public function setHeader($header, $value)
150    {
151        $this->_value->setHeader($header, $value);
152    }
153
154    public function setHeaders(array $headers)
155    {
156        $this->_value->setHeaders($headers);
157    }
158}
159