1
|
|
|
<?php |
2
|
|
|
/* |
3
|
|
|
* This file is part of the ReCaptcha Library. |
4
|
|
|
* |
5
|
|
|
* (c) Ilya Pokamestov <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace DS\Library\ReCaptcha\Http; |
12
|
|
|
|
13
|
|
|
use Psr\Http\Message\ResponseInterface; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* ReCaptcha library, PSR7 response representation. |
17
|
|
|
* |
18
|
|
|
* @author Ilya Pokamestov <[email protected]> |
19
|
|
|
* |
20
|
|
|
* Class Response |
21
|
|
|
* @package DS\Library\ReCaptcha\Http |
22
|
|
|
*/ |
23
|
|
|
class Response extends Message implements ResponseInterface |
24
|
|
|
{ |
25
|
|
|
/** @var null|string */ |
26
|
|
|
protected $reasonPhrase = ''; |
27
|
|
|
/** @var int */ |
28
|
|
|
protected $statusCode = 200; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param int $status Status code for the response, if any. |
32
|
|
|
* @param array $headers Headers for the response, if any. |
33
|
|
|
* @param mixed $body Stream body. |
34
|
|
|
*/ |
35
|
10 |
|
public function __construct($status = 200, array $headers = array(), $body = null) |
36
|
|
|
{ |
37
|
10 |
|
$this->statusCode = (int)$status; |
38
|
10 |
|
if ($body) { |
39
|
5 |
|
$this->stream = new Stream($body); |
40
|
5 |
|
} |
41
|
|
|
|
42
|
10 |
|
foreach ($headers as $headerName => $header) { |
43
|
|
|
//Some HTTP clients can provide header value in array format. |
44
|
3 |
|
if (is_array($header)) { |
45
|
1 |
|
$name = $headerName; |
46
|
1 |
|
$value = current($header); |
47
|
1 |
|
} else { |
48
|
2 |
|
list($name, $value) = explode(':', $header); |
49
|
|
|
} |
50
|
|
|
|
51
|
3 |
|
$values = explode(';', trim($value)); |
52
|
3 |
|
foreach ($values as $headerValue) { |
53
|
3 |
|
$this->withAddedHeader($name, trim($headerValue)); |
54
|
3 |
|
} |
55
|
10 |
|
} |
56
|
|
|
|
57
|
10 |
|
if (isset(Protocol::$phrases[$this->statusCode])) { |
58
|
10 |
|
$this->reasonPhrase = Protocol::$phrases[$this->statusCode]; |
59
|
10 |
|
} |
60
|
10 |
|
} |
61
|
|
|
|
62
|
|
|
/** {@inheritdoc} */ |
63
|
3 |
|
public function getStatusCode() |
64
|
|
|
{ |
65
|
3 |
|
return $this->statusCode; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** {@inheritdoc} */ |
69
|
2 |
|
public function getReasonPhrase() |
70
|
|
|
{ |
71
|
2 |
|
return $this->reasonPhrase; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** {@inheritdoc} */ |
75
|
1 |
|
public function withStatus($code, $reasonPhrase = '') |
76
|
|
|
{ |
77
|
1 |
|
$this->statusCode = (int)$code; |
78
|
1 |
|
if (!$reasonPhrase && isset(Protocol::$phrases[$this->statusCode])) { |
79
|
1 |
|
$reasonPhrase = Protocol::$phrases[$this->statusCode]; |
80
|
1 |
|
} |
81
|
1 |
|
$this->reasonPhrase = $reasonPhrase; |
82
|
|
|
|
83
|
1 |
|
return $this; |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|