1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Money |
4
|
|
|
* |
5
|
|
|
* @author Pronamic <[email protected]> |
6
|
|
|
* @copyright 2005-2021 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
|
|
|
/** |
14
|
|
|
* Money |
15
|
|
|
* |
16
|
|
|
* @author Remco Tolsma |
17
|
|
|
* @version 2.1.8 |
18
|
|
|
* @since 2.0.2 |
19
|
|
|
*/ |
20
|
|
|
class Money implements \JsonSerializable { |
21
|
|
|
/** |
22
|
|
|
* Currency. |
23
|
|
|
* |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
private $currency; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Amount. |
30
|
|
|
* |
31
|
|
|
* @var int |
32
|
|
|
*/ |
33
|
|
|
private $amount; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Construct Money. |
37
|
|
|
* |
38
|
|
|
* @param string $currency Currency. |
39
|
|
|
* @param int $amount Amount in cents. |
40
|
|
|
*/ |
41
|
7 |
|
public function __construct( $currency, $amount ) { |
42
|
7 |
|
$this->currency = $currency; |
43
|
7 |
|
$this->amount = $amount; |
44
|
7 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get currency. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
3 |
|
public function get_currency() { |
52
|
3 |
|
return $this->currency; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Get amount. |
57
|
|
|
* |
58
|
|
|
* @return int |
59
|
|
|
*/ |
60
|
3 |
|
public function get_amount() { |
61
|
3 |
|
return $this->amount; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get JSON. |
66
|
|
|
* |
67
|
|
|
* @return object |
68
|
|
|
*/ |
69
|
|
|
public function jsonSerialize() { |
70
|
|
|
return (object) array( |
71
|
|
|
'currency' => $this->get_currency(), |
72
|
|
|
'amount' => $this->get_amount(), |
73
|
|
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Create money from object. |
78
|
|
|
* |
79
|
|
|
* @param \stdClass $object Object. |
80
|
|
|
* @return Money |
81
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception when object does not contains the required properties. |
82
|
|
|
*/ |
83
|
1 |
|
public static function from_object( \stdClass $object ) { |
84
|
1 |
|
if ( ! isset( $object->currency ) ) { |
85
|
|
|
throw new \InvalidArgumentException( 'Object must contain `currency` property.' ); |
86
|
|
|
} |
87
|
|
|
|
88
|
1 |
|
if ( ! isset( $object->amount ) ) { |
89
|
|
|
throw new \InvalidArgumentException( 'Object must contain `amount` property.' ); |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
return new self( $object->currency, $object->amount ); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Create money from JSON string. |
97
|
|
|
* |
98
|
|
|
* @param string $json JSON string. |
99
|
|
|
* @return Money |
100
|
|
|
* @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid. |
101
|
|
|
*/ |
102
|
|
|
public static function from_json( $json ) { |
103
|
|
|
$data = \json_decode( $json ); |
104
|
|
|
|
105
|
|
|
$validator = new \JsonSchema\Validator(); |
106
|
|
|
|
107
|
|
|
$validator->validate( |
108
|
|
|
$data, |
109
|
|
|
(object) array( |
110
|
|
|
'$ref' => 'file://' . \realpath( __DIR__ . '/../json-schemas/json-schema-money.json' ), |
111
|
|
|
), |
112
|
|
|
\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS |
113
|
|
|
); |
114
|
|
|
|
115
|
|
|
return self::from_object( $data ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get signature fields. |
120
|
|
|
* |
121
|
|
|
* @param array<string> $fields Fields. |
122
|
|
|
* @return array<string> |
123
|
|
|
*/ |
124
|
3 |
|
public function get_signature_fields( $fields = array() ) { |
125
|
3 |
|
$fields[] = $this->get_currency(); |
126
|
3 |
|
$fields[] = \strval( $this->get_amount() ); |
127
|
|
|
|
128
|
3 |
|
return $fields; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|