-
Notifications
You must be signed in to change notification settings - Fork 3
/
klarnaconfig.php
174 lines (163 loc) · 4.1 KB
/
klarnaconfig.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
/**
* KlarnaConfig
*
* PHP Version 5.3
*
* @category Payment
* @package KlarnaAPI
* @author MS Dev <ms.modules@klarna.com>
* @copyright 2012 Klarna AB (http://klarna.com)
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2
* @link https://developers.klarna.com/
*/
/**
* Configuration class for the Klarna instance.
*
* KlarnaConfig stores added fields in JSON, it also prepends.<br>
* Loads/saves specified file, or default file, if {@link KlarnaConfig::$store}
* is set to true.<br>
*
* You add settings using the ArrayAccess:<br>
* $arr['field'] = $val or $arr->offsetSet('field', $val);<br>
*
* Available settings are:<br>
* eid - Merchant ID (int)
* secret - Shared secret (string)
* country - Country constant or code (int|string)
* language - Language constant or code (int|string)
* currency - Currency constant or code (int|string)
* mode - Klarna::BETA or Klarna::LIVE
* ssl - Use HTTPS or HTTP. (bool)
* pcStorage - Storage module, e.g. 'json'
* pcURI - URI to where the PClasses are stored, e.g.
* '/srv/shop/pclasses.json'
* xmlrpcDebug - XMLRPC debugging (bool)
* debug - Normal debugging (bool)
*
* @category Payment
* @package KlarnaAPI
* @author MS Dev <ms.modules@klarna.com>
* @copyright 2012 Klarna AB (http://klarna.com)
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2
* @link https://developers.klarna.com/
*/
class KlarnaConfig implements ArrayAccess
{
/**
* An array containing all the options for this config.
*
* @ignore Do not show in PHPDoc.
* @var array
*/
protected $options;
/**
* If set to true, saves the config.
*
* @var bool
*/
public static $store = true;
/**
* URI to the config file.
*
* @ignore Do not show in PHPDoc.
* @var string
*/
protected $file;
/**
* Class constructor
*
* Loads specified file, or default file,
* if {@link KlarnaConfig::$store} is set to true.
*
* @param string $file URI to config file, e.g. ./config.json
*/
public function __construct($file = null)
{
$this->options = array();
if ($file) {
$this->file = $file;
if (is_readable($this->file)) {
$this->options = json_decode(
file_get_contents(
$this->file
), true
);
}
}
}
/**
* Clears the config.
*
* @return void
*/
public function clear()
{
$this->options = array();
}
/**
* Class destructor
*
* Saves specified file, or default file,
* if {@link KlarnaConfig::$store} is set to true.
*/
public function __destruct()
{
if (self::$store && $this->file) {
if ((!file_exists($this->file)
&& is_writable(dirname($this->file)))
|| is_writable($this->file)
) {
file_put_contents($this->file, json_encode($this->options));
}
}
}
/**
* Returns true whether the field exists.
*
* @param mixed $offset field
*
* @return bool
*/
public function offsetExists($offset)
{
return isset($this->options[$offset]);
}
/**
* Used to get the value of a field.
*
* @param mixed $offset field
*
* @return mixed
*/
public function offsetGet($offset)
{
if (!$this->offsetExists($offset)) {
return null;
}
return $this->options[$offset];
}
/**
* Used to set a value to a field.
*
* @param mixed $offset field
* @param mixed $value value
*
* @return void
*/
public function offsetSet($offset, $value)
{
$this->options[$offset] = $value;
}
/**
* Removes the specified field.
*
* @param mixed $offset field
*
* @return void
*/
public function offsetUnset($offset)
{
unset($this->options[$offset]);
}
}