This repository has been archived by the owner on Apr 3, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponse.php
103 lines (90 loc) · 1.95 KB
/
Response.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
<?php
/**
* Copyright (C) GrizzIT, Inc. All rights reserved.
* See LICENSE for license details.
*/
namespace Ulrack\Transaction\Component;
use Ulrack\Transaction\Common\ResponseInterface;
use Ulrack\Transaction\Exception\HeaderNotFoundException;
class Response implements ResponseInterface
{
/** @var bool */
private $success;
/** @var mixed */
private $body;
/** @var int */
private $statusCode;
/** @var array */
private $headers;
/**
* Constructor
*
* @param bool $success
* @param mixed $body
* @param int $statusCode
* @param array $headers
*/
public function __construct(
bool $success,
$body,
int $statusCode,
array $headers = []
) {
$this->success = $success;
$this->body = $body;
$this->statusCode = $statusCode;
$this->headers = $headers;
}
/**
* Determines if the request was a success.
*
* @return bool
*/
public function isSuccess(): bool
{
return $this->success;
}
/**
* Retrieves the body of the response.
*
* @return mixed
*/
public function getBody()
{
return $this->body;
}
/**
* Retrieves the status code of the response.
*
* @return int
*/
public function getStatusCode(): int
{
return $this->statusCode;
}
/**
* Retrieves the response headers.
*
* @return string[]
*/
public function getHeaders(): array
{
return $this->headers;
}
/**
* Retrieves a header by its' name.
*
* @param string $name
*
* @return string
*
* @throws HeaderNotFoundException If the header is not set.
*/
public function getHeader(string $name): string
{
if (isset($this->headers[$name])) {
return $this->headers[$name];
}
throw new HeaderNotFoundException($name);
}
}