1 <?php
2 /**
3 * Copyright 2015 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 Klarna_Checkout_HTTP_Request class
18 *
19 * PHP version 5.3
20 *
21 * @category Payment
22 * @package Payment_Klarna
23 * @subpackage HTTP
24 * @author Klarna <support@klarna.com>
25 * @copyright 2015 Klarna AB
26 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
27 * @link http://developers.klarna.com/
28 */
29
30 /**
31 * Klarna HTTP Request class
32 *
33 * @category Payment
34 * @package Payment_Klarna
35 * @subpackage HTTP
36 * @author Klarna <support@klarna.com>
37 * @copyright 2015 Klarna AB
38 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
39 * @link http://developers.klarna.com/
40 */
41 class Klarna_Checkout_HTTP_Request
42 {
43 /**
44 * HTTP url
45 *
46 * @var string
47 */
48 protected $url;
49
50 /**
51 * HTTP method
52 *
53 * @var string
54 */
55 protected $method;
56
57 /**
58 * HTTP headers
59 *
60 * @var array
61 */
62 protected $headers;
63
64 /**
65 * Payload
66 *
67 * @var string
68 */
69 protected $data;
70
71 /**
72 * Initializes a new instance of the HTTP request class.
73 *
74 * @param string $url the request URL.
75 *
76 * @throws InvalidArgumentException If the specified argument
77 * is not of type string.
78 */
79 public function __construct($url)
80 {
81 $this->url = $url;
82 $this->method = 'GET';
83 $this->headers = array();
84 $this->data = '';
85 }
86
87 /**
88 * Gets the request URL.
89 *
90 * @return string the request URL.
91 */
92 public function getURL()
93 {
94 return $this->url;
95 }
96
97 /**
98 * Specifies the HTTP method used for the request.
99 *
100 * @param string $method a HTTP method.
101 *
102 * @throws InvalidArgumentException If the specified argument
103 * is not of type string.
104 * @return void
105 */
106 public function setMethod($method)
107 {
108 $this->method = strtoupper($method);
109 }
110
111 /**
112 * Gets the HTTP method used for the request.
113 *
114 * @return string a HTTP method
115 */
116 public function getMethod()
117 {
118 return $this->method;
119 }
120
121 /**
122 * Specifies a header for the request.
123 *
124 * @param string $name the header name
125 * @param mixed $value the header value
126 *
127 * @throws InvalidArgumentException If the argument name is not of type
128 * string or an empty string.
129 * @return void
130 */
131 public function setHeader($name, $value)
132 {
133 $this->headers[$name] = strval($value);
134 }
135
136 /**
137 * Gets a specific header for the request.
138 *
139 * @param string $name the header name
140 *
141 * @throws InvalidArgumentException If the specified argument
142 * is not of type string.
143 * @return string|null the header value or null if it doesn't exist
144 */
145 public function getHeader($name)
146 {
147 if (!array_key_exists($name, $this->headers)) {
148 return null;
149 }
150
151 return $this->headers[$name];
152 }
153
154 /**
155 * Gets the headers specified for the request.
156 *
157 * @return array
158 */
159 public function getHeaders()
160 {
161 return $this->headers;
162 }
163
164 /**
165 * Sets the data (payload) for the request.
166 *
167 * \code
168 * $request->setMethod('POST');
169 * $request->setData('some data');
170 * \endcode
171 *
172 * @param string $data the request payload
173 *
174 * @throws InvalidArgumentException If the specified argument
175 * is not of type string.
176 * @return void
177 */
178 public function setData($data)
179 {
180 $this->data = $data;
181 }
182
183 /**
184 * Gets the data (payload) for the request.
185 *
186 * @return string the request payload
187 */
188 public function getData()
189 {
190 return $this->data;
191 }
192 }
193