|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Amount |
|
4
|
|
|
* |
|
5
|
|
|
* @author Pronamic <[email protected]> |
|
6
|
|
|
* @copyright 2005-2019 Pronamic |
|
7
|
|
|
* @license GPL-3.0-or-later |
|
8
|
|
|
* @package Pronamic\WordPress\Pay\Gateways\Adyen |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Pronamic\WordPress\Pay\Gateways\Adyen; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Amount |
|
15
|
|
|
* |
|
16
|
|
|
* @link https://docs.adyen.com/developers/api-reference/common-api/amount |
|
17
|
|
|
* |
|
18
|
|
|
* @author Remco Tolsma |
|
19
|
|
|
* @version 1.0.0 |
|
20
|
|
|
* @since 1.0.0 |
|
21
|
|
|
*/ |
|
22
|
|
|
class Amount implements \JsonSerializable { |
|
23
|
|
|
/** |
|
24
|
|
|
* Currency. |
|
25
|
|
|
* |
|
26
|
|
|
* @var string |
|
27
|
|
|
*/ |
|
28
|
|
|
private $currency; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Value. |
|
32
|
|
|
* |
|
33
|
|
|
* @var int |
|
34
|
|
|
*/ |
|
35
|
|
|
private $value; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Construct amount. |
|
39
|
|
|
* |
|
40
|
|
|
* @param string $currency Currency. |
|
41
|
|
|
* @param int $value Value. |
|
42
|
|
|
* |
|
43
|
|
|
* @throws \InvalidArgumentException Throws invalid argument exception when Adyen amount requirements are not met. |
|
44
|
|
|
*/ |
|
45
|
21 |
|
public function __construct( $currency, $value ) { |
|
46
|
21 |
|
if ( 3 !== strlen( $currency ) ) { |
|
47
|
1 |
|
throw new \InvalidArgumentException( |
|
48
|
1 |
|
sprintf( |
|
49
|
1 |
|
'Given currency `%s` not a three-character ISO currency code.', |
|
50
|
|
|
$currency |
|
51
|
|
|
) |
|
52
|
|
|
); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
20 |
|
$this->currency = $currency; |
|
56
|
20 |
|
$this->value = $value; |
|
57
|
20 |
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Get currency. |
|
61
|
|
|
* |
|
62
|
|
|
* @return string |
|
63
|
|
|
*/ |
|
64
|
9 |
|
public function get_currency() { |
|
65
|
9 |
|
return $this->currency; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Get amount. |
|
70
|
|
|
* |
|
71
|
|
|
* @return int |
|
72
|
|
|
*/ |
|
73
|
9 |
|
public function get_value() { |
|
74
|
9 |
|
return $this->value; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* Get JSON. |
|
79
|
|
|
* |
|
80
|
|
|
* @return object |
|
81
|
|
|
*/ |
|
82
|
6 |
|
public function get_json() { |
|
83
|
|
|
return (object) array( |
|
84
|
6 |
|
'currency' => $this->get_currency(), |
|
85
|
6 |
|
'value' => $this->get_value(), |
|
86
|
|
|
); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* JSON serialize. |
|
91
|
|
|
* |
|
92
|
|
|
* @link https://www.php.net/manual/en/jsonserializable.jsonserialize.php |
|
93
|
|
|
* @return object |
|
94
|
|
|
*/ |
|
95
|
1 |
|
public function jsonSerialize() { |
|
96
|
1 |
|
return $this->get_json(); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* Create amount from object. |
|
101
|
|
|
* |
|
102
|
|
|
* @param object $object Object. |
|
103
|
|
|
* @return Amount |
|
104
|
|
|
* @throws \JsonSchema\Exception\ValidationException Throws validation exception when object does not contains the required properties. |
|
105
|
|
|
*/ |
|
106
|
12 |
|
public static function from_object( $object ) { |
|
107
|
12 |
|
$validator = new \JsonSchema\Validator(); |
|
108
|
|
|
|
|
109
|
12 |
|
$validator->validate( |
|
110
|
12 |
|
$object, |
|
111
|
|
|
(object) array( |
|
112
|
12 |
|
'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/amount.json' ), |
|
113
|
|
|
), |
|
114
|
12 |
|
\JsonSchema\Constraints\Constraint::CHECK_MODE_EXCEPTIONS |
|
115
|
|
|
); |
|
116
|
|
|
|
|
117
|
10 |
|
return new self( |
|
118
|
10 |
|
$object->currency, |
|
119
|
10 |
|
$object->value |
|
120
|
|
|
); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|