-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWMXIResult.php
169 lines (118 loc) · 4.74 KB
/
WMXIResult.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
<?php
################################################################################
# #
# Webmoney XML Interfaces by DKameleon (http://dkameleon.com) #
# #
# Updates and new versions: http://my-tools.net/wmxi/ #
# #
# Server requirements: #
# - SimpleXML #
# #
################################################################################
# including classes
if (!defined('__DIR__')) { define('__DIR__', dirname(__FILE__)); }
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'WMXILogger.php');
class WMXIResult {
private $req = array();
private $res = array();
private $scope = '';
private $i18n = null;
public function __construct($req, $res, $scope) {
$this->req['str'] = $req;
$this->res['str'] = $res;
$this->scope = $scope;
try {
$this->req['obj'] = new SimpleXMLElement($req);
} catch (Exception $e) {
$this->req['obj'] = null;
}
try {
$this->res['obj'] = new SimpleXMLElement($res);
} catch (Exception $e) {
$this->res['obj'] = null;
}
$this->i18n();
}
private function i18n() {
if (!defined('WMXI_LOCALE')) { define('WMXI_LOCALE', 'en_US'); };
$fname = __DIR__ . DIRECTORY_SEPARATOR . 'i18n' . DIRECTORY_SEPARATOR . WMXI_LOCALE . DIRECTORY_SEPARATOR . 'WMXIErrors.xml';
$this->i18n = file_exists($fname) ? new SimpleXMLElement(file_get_contents($fname)) : null;
}
public function toString() {
return $this->res['str'];
}
public function toObject() {
return $this->res['obj'];
}
private function toArrayMap($data) {
if (is_object($data)) { $data = get_object_vars($data); }
return is_array($data) ? array_map(array($this, __METHOD__), $data) : $data;
}
public function toArray() {
return $this->toArrayMap($this->res['obj']);
}
public function GetRequest($plain = true) {
return $this->req[$plain ? 'str' : 'obj'];
}
public function GetResponse($plain = true) {
return $this->res[$plain ? 'str' : 'obj'];
}
public function ErrorCode() {
if (!$this->res['obj']) { return false; }
$obj = $this->res['obj'];
# cURL error code
if (isset($obj->errno )) { return intval($obj->errno ); }
# WMXI error code
if (isset($obj->retval)) { return intval($obj->retval); }
if (isset($obj['retval'])) { return intval($obj['retval']); }
# no suitable error code detected
return false;
}
public function ErrorText($code = false) {
if ($code === false) { $code = $this->ErrorCode(); }
if ($code === false) { return false; }
$obj = $this->res['obj'];
$message = '';
if (isset($obj->error )) { $message = strval($obj->error ); }
if (isset($obj->retdesc)) { $message = strval($obj->retdesc); }
$message = !empty($message) ? " [$message]" : '';
$result = array();
foreach($this->i18n as $k => $v) {
$scope = strval($v['scope']);
$value = strval($v[0]);
if (strval($v['code']) == '') { $result[''] = $value; }
if (strval($v['code']) == strval($code)) { $result[$scope] = $value; }
}
if (isset($result[$this->scope])) { return $result[$this->scope].$message; }
if (isset($result['*'])) { return $result['*'].$message; }
if (isset($result[''])) { return $result[''].$message; }
return false;
}
private function SortASC($a, $b) {
if (!is_array($a) || !is_array($b)) { return 0; }
$delta = intval($a['@attributes']['id']) - intval($b['@attributes']['id']);
if ($delta == 0) { return 0; }
return $delta / abs($delta);
}
private function SortDSC($a, $b) {
return - $this->SortASC($a, $b);
}
public function Sort($ascending = true) {
$sort_func = array($this, __METHOD__ . ($ascending ? 'ASC' : 'DSC'));
$res = $this->toArray();
# X3
$cnt = isset($res['operations']['@attributes']['cnt']) ? $res['operations']['@attributes']['cnt'] : 0;
if ($cnt > 1) { usort($res['operations']['operation'], $sort_func); }
# X4
$cnt = isset($res['outinvoices']['@attributes']['cnt']) ? $res['outinvoices']['@attributes']['cnt'] : 0;
if ($cnt > 1) { usort($res['outinvoices']['outinvoice'], $sort_func); }
# X10
$cnt = isset($res['ininvoices']['@attributes']['cnt']) ? $res['ininvoices']['@attributes']['cnt'] : 0;
if ($cnt > 1) { usort($res['ininvoices']['ininvoice'], $sort_func); }
# X15
$cnt = isset($res['trustlist']['@attributes']['cnt']) ? $res['trustlist']['@attributes']['cnt'] : 0;
if ($cnt > 1) { usort($res['trustlist']['trust'], $sort_func); }
return $res;
}
}
?>