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_Resource class
18 *
19 * PHP version 5.3
20 *
21 * @category Payment
22 * @package Klarna_Checkout
23 * @author Klarna <support@klarna.com>
24 * @copyright 2015 Klarna AB
25 * @license http://www.apache.org/licenses/LICENSE-2.0 Apache license v2.0
26 * @link http://developers.klarna.com/
27 */
28
29 /**
30 * Implementation of the order resource
31 *
32 * @category Payment
33 * @package Klarna_Checkout
34 * @author Majid G. <majid.garmaroudi@klarna.com>
35 * @author David K. <david.keijser@klarna.com>
36 * @author Matthias Feist <matthias.feist@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 abstract class Klarna_Checkout_Resource
42 implements Klarna_Checkout_ResourceInterface, ArrayAccess
43 {
44 /**
45 * Path that is used to create resources
46 *
47 * @var string
48 */
49 protected $relativePath = null;
50
51 /**
52 * Content Type to use
53 *
54 * @var string
55 */
56 protected $contentType = null;
57
58 /**
59 * Accept header to use
60 *
61 * @var string
62 */
63 protected $accept = null;
64
65 /**
66 * URI of remote resource
67 *
68 * @var string
69 */
70 protected $location;
71
72 /**
73 * Order data
74 *
75 * @var array
76 */
77 protected $data = array();
78
79 /**
80 * Connector
81 *
82 * @var Klarna_Checkout_ConnectorInterface
83 */
84 protected $connector;
85
86 /**
87 * Create a new Resource object
88 *
89 * @param Klarna_Checkout_ConnectorInterface $connector connector to use
90 */
91 public function __construct(Klarna_Checkout_ConnectorInterface $connector)
92 {
93 $this->connector = $connector;
94 }
95
96 /**
97 * Get the URL of the resource
98 *
99 * @return string
100 */
101 public function getLocation()
102 {
103 return $this->location;
104 }
105
106 /**
107 * Set the URL of the resource
108 *
109 * @param string $location URL of the resource
110 *
111 * @return void
112 */
113 public function setLocation($location)
114 {
115 $this->location = strval($location);
116 }
117
118 /**
119 * Return content type of the resource
120 *
121 * @return string Content type
122 */
123 public function getContentType()
124 {
125 return $this->contentType;
126 }
127
128 /**
129 * Return accept header of the resource
130 *
131 * @return string Accept header
132 */
133 public function getAccept()
134 {
135 return $this->accept;
136 }
137
138 /**
139 * Set the content type
140 *
141 * @param string $contentType Content type
142 *
143 * @return void
144 */
145 public function setContentType($contentType)
146 {
147 $this->contentType = $contentType;
148 }
149
150 /**
151 * Set the accept type
152 *
153 * @param string $accept Accept type
154 *
155 * @return void
156 */
157 public function setAccept($accept)
158 {
159 $this->accept = $accept;
160 }
161
162 /**
163 * Replace resource data
164 *
165 * @param array $data data
166 *
167 * @return void
168 */
169 public function parse(array $data)
170 {
171 $this->data = $data;
172 }
173
174 /**
175 * Basic representation of the object
176 *
177 * @return array Data
178 */
179 public function marshal()
180 {
181 return $this->data;
182 }
183
184 /**
185 * Get value of a key
186 *
187 * @param string $key Key
188 *
189 * @return mixed data
190 */
191 public function offsetGet($key)
192 {
193 if (!is_string($key)) {
194 throw new InvalidArgumentException("Key must be string");
195 }
196
197 return isset($this->data[$key]) ? $this->data[$key] : null;
198 }
199
200 /**
201 * Set value of a key
202 *
203 * @param string $key Key
204 * @param mixed $value Value of the key
205 *
206 * @return void
207 */
208 public function offsetSet($key, $value)
209 {
210 if (!is_string($key)) {
211 throw new InvalidArgumentException("Key must be string");
212 }
213
214 $value = print_r($value, true);
215 throw new RuntimeException(
216 "Use update function to change values. trying to set $key to $value"
217 );
218 }
219
220 /**
221 * Check if a key exists in the resource
222 *
223 * @param string $key key
224 *
225 * @return boolean
226 */
227 public function offsetExists($key)
228 {
229 return array_key_exists($key, $this->data);
230 }
231
232 /**
233 * Unset the value of a key
234 *
235 * @param string $key key
236 *
237 * @return void
238 */
239 public function offsetUnset($key)
240 {
241 throw new RuntimeException(
242 "unset of fields not supported. trying to unset $key"
243 );
244 }
245 }
246