1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Error |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2018 Pronamic |
7
|
|
|
* @license GPL-3.0-or-later |
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\OmniKassa2 |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\OmniKassa2; |
12
|
|
|
|
13
|
|
|
use Exception; |
14
|
|
|
use InvalidArgumentException; |
15
|
|
|
use stdClass; |
16
|
|
|
use JsonSchema\Constraints\Constraint; |
17
|
|
|
use JsonSchema\Exception\ValidationException; |
18
|
|
|
use JsonSchema\Validator; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Error |
22
|
|
|
* |
23
|
|
|
* @author Remco Tolsma |
24
|
|
|
* @version 2.0.2 |
25
|
|
|
* @since 2.0.2 |
26
|
|
|
*/ |
27
|
|
|
class Error { |
28
|
|
|
/** |
29
|
|
|
* Code. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
private $code; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Message. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $message; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Construct error. |
44
|
|
|
* |
45
|
|
|
* @param string $code Code. |
46
|
|
|
* @param string $message Message. |
47
|
|
|
*/ |
48
|
|
|
public function __construct( $code, $message ) { |
49
|
|
|
$this->code = $code; |
50
|
|
|
$this->message = $message; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get codecode. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function get_code() { |
59
|
|
|
return $this->code; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Get amount. |
64
|
|
|
* |
65
|
|
|
* @return int |
66
|
|
|
*/ |
67
|
|
|
public function get_message() { |
68
|
|
|
return $this->message; |
|
|
|
|
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Create error from object. |
73
|
|
|
* |
74
|
|
|
* @param stdClass $object Object. |
75
|
|
|
* @return Error |
76
|
|
|
* @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties. |
77
|
|
|
*/ |
78
|
|
|
public static function from_object( stdClass $object ) { |
79
|
|
|
if ( ! isset( $object->errorCode ) ) { |
80
|
|
|
throw new InvalidArgumentException( 'Object must contain `errorCode` property.' ); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
$message = null; |
84
|
|
|
|
85
|
|
|
if ( isset( $object->errorMessage ) ) { |
86
|
|
|
$message = $object->errorMessage; |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if ( isset( $object->consumerMessage ) ) { |
90
|
|
|
$message = $object->consumerMessage; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return new self( |
94
|
|
|
$object->errorCode, |
95
|
|
|
$message |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Create error from JSON string. |
101
|
|
|
* |
102
|
|
|
* @param string $json JSON string. |
103
|
|
|
* @return Error |
104
|
|
|
* @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid. |
105
|
|
|
*/ |
106
|
|
|
public static function from_json( $json ) { |
107
|
|
|
$data = json_decode( $json ); |
108
|
|
|
|
109
|
|
|
$validator = new Validator(); |
110
|
|
|
|
111
|
|
|
$validator->validate( $data, (object) array( |
112
|
|
|
'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/json-schema-error.json' ), |
113
|
|
|
), Constraint::CHECK_MODE_EXCEPTIONS ); |
114
|
|
|
|
115
|
|
|
return self::from_object( $data ); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|