1 <?php
2 /**
3 * Copyright 2019 Klarna AB
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 * File containing the ApiResponse class.
18 */
19
20 namespace Klarna\Rest\Transport;
21
22 /**
23 * General HTTP response instance.
24 */
25 class ApiResponse
26 {
27 /**
28 * HTTP response Status code
29 */
30 private $status;
31
32 /**
33 * HTTP Response headers
34 */
35 private $headers = [];
36
37 /**
38 * HTTP body binary payout
39 */
40 private $body = null;
41
42
43 public function __construct($status = null, $body = null, $headers = [])
44 {
45 $this->setStatus($status);
46 $this->setBody($body);
47 $this->setHeaders($headers);
48 }
49 /**
50 * Sets HTTP Status code.
51 *
52 * @param status HTTP status
53 * @return self
54 */
55 public function setStatus($status)
56 {
57 $this->status = $status;
58 return $this;
59 }
60
61 /**
62 * Gets HTTP Status code.
63 *
64 * @return Status code
65 */
66 public function getStatus()
67 {
68 return $this->status;
69 }
70
71 /**
72 * Sets binary body payload.
73 *
74 * @param body Payout
75 * @return self
76 */
77 public function setBody($body)
78 {
79 $this->body = $body;
80 return $this;
81 }
82
83 /**
84 * Gets binary body payload.
85 *
86 * @return Payout
87 */
88 public function getBody()
89 {
90 return $this->body;
91 }
92
93 /**
94 * Sets HTTP headers map
95 *
96 * @param headers Headers
97 * @return self
98 */
99 public function setHeaders($headers)
100 {
101 $this->headers = $headers;
102 return $this;
103 }
104
105 /**
106 * Sets single HTTP header value.
107 *
108 * @param name Header name
109 * @param values Header values
110 * @return self
111 */
112 public function setHeader($name, $values)
113 {
114 $this->headers[$name] = $values;
115 return $this;
116 }
117
118 /**
119 * Gets HTTP Headers map
120 *
121 * @return Headers
122 */
123 public function getHeaders()
124 {
125 return $this->headers;
126 }
127
128 /**
129 * Gets single header value
130 *
131 * @param name Header name
132 * @return Header values
133 */
134 public function getHeader($name)
135 {
136 return isset($this->headers[$name]) ? $this->headers[$name] : null;
137 }
138
139 /**
140 * Gets the Location header helper.
141 *
142 * @return string Location if exists, null otherwise
143 */
144 public function getLocation()
145 {
146 return empty($this->headers['Location']) ? null : $this->headers['Location'][0];
147 }
148 }
149