1
|
|
|
<?php |
|
|
|
|
2
|
|
|
/** |
3
|
|
|
* JsonStorage |
4
|
|
|
* |
5
|
|
|
* PHP Version 5.3 |
6
|
|
|
* |
7
|
|
|
* @category Payment |
8
|
|
|
* @package KlarnaAPI |
9
|
|
|
* @author MS Dev <[email protected]> |
10
|
|
|
* @copyright 2012 Klarna AB (http://klarna.com) |
11
|
|
|
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2 |
12
|
|
|
* @link https://developers.klarna.com/ |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Include the {@link PCStorage} interface. |
17
|
|
|
*/ |
18
|
|
|
require_once 'storage.intf.php'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* JSON storage class for KlarnaPClass |
22
|
|
|
* |
23
|
|
|
* This class is an JSON implementation of the PCStorage interface. |
24
|
|
|
* |
25
|
|
|
* @category Payment |
26
|
|
|
* @package KlarnaAPI |
27
|
|
|
* @author MS Dev <[email protected]> |
28
|
|
|
* @copyright 2012 Klarna AB (http://klarna.com) |
29
|
|
|
* @license http://opensource.org/licenses/BSD-2-Clause BSD-2 |
30
|
|
|
* @link https://developers.klarna.com/ |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
class JSONStorage extends PCStorage |
34
|
|
|
{ |
35
|
|
|
|
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* return the name of the storage type |
39
|
|
|
* |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getName() |
43
|
|
|
{ |
44
|
|
|
return "json"; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Checks if the file is writeable, readable or if the directory is. |
49
|
|
|
* |
50
|
|
|
* @param string $jsonFile json file that holds the pclasses |
51
|
|
|
* |
52
|
|
|
* @throws error |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
View Code Duplication |
protected function checkURI($jsonFile) |
|
|
|
|
56
|
|
|
{ |
57
|
|
|
//If file doesn't exist, check the directory. |
58
|
|
|
if (!file_exists($jsonFile)) { |
59
|
|
|
$jsonFile = dirname($jsonFile); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (!is_writable($jsonFile)) { |
63
|
|
|
throw new Klarna_FileNotWritableException($jsonFile); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!is_readable($jsonFile)) { |
67
|
|
|
throw new Klarna_FileNotReadableException($jsonFile); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Clear the pclasses |
73
|
|
|
* |
74
|
|
|
* @param string $uri uri to file to clear |
75
|
|
|
* |
76
|
|
|
* @throws KlarnaException |
77
|
|
|
* @return void |
78
|
|
|
*/ |
79
|
|
|
public function clear($uri) |
80
|
|
|
{ |
81
|
|
|
$this->checkURI($uri); |
82
|
|
|
unset($this->pclasses); |
83
|
|
|
if (file_exists($uri)) { |
84
|
|
|
unlink($uri); |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Load pclasses from file |
90
|
|
|
* |
91
|
|
|
* @param string $uri uri to file to load |
92
|
|
|
* |
93
|
|
|
* @throws KlarnaException |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function load($uri) |
97
|
|
|
{ |
98
|
|
|
$this->checkURI($uri); |
99
|
|
|
if (!file_exists($uri)) { |
100
|
|
|
//Do not fail, if file doesn't exist. |
101
|
|
|
return; |
102
|
|
|
} |
103
|
|
|
$arr = json_decode(file_get_contents($uri), true); |
104
|
|
|
|
105
|
|
|
if (count($arr) == 0) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
foreach ($arr as $pclasses) { |
110
|
|
|
if (count($pclasses) == 0) { |
111
|
|
|
continue; |
112
|
|
|
} |
113
|
|
|
foreach ($pclasses as $pclass) { |
114
|
|
|
$this->addPClass(new KlarnaPClass($pclass)); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Save pclasses to file |
121
|
|
|
* |
122
|
|
|
* @param string $uri uri to file to save |
123
|
|
|
* |
124
|
|
|
* @throws KlarnaException |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
public function save($uri) |
128
|
|
|
{ |
129
|
|
|
try { |
130
|
|
|
$this->checkURI($uri); |
131
|
|
|
|
132
|
|
|
$output = array(); |
133
|
|
|
foreach ($this->pclasses as $eid => $pclasses) { |
134
|
|
|
foreach ($pclasses as $pclass) { |
135
|
|
|
if (!isset($output[$eid])) { |
136
|
|
|
$output[$eid] = array(); |
137
|
|
|
} |
138
|
|
|
$output[$eid][] = $pclass->toArray(); |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
if (count($this->pclasses) > 0) { |
142
|
|
|
file_put_contents($uri, json_encode($output)); |
143
|
|
|
} else { |
144
|
|
|
file_put_contents($uri, ""); |
145
|
|
|
} |
146
|
|
|
} catch(Exception $e) { |
|
|
|
|
147
|
|
|
throw new KlarnaException($e->getMessage()); |
148
|
|
|
} |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.