Test Failed
Push — master ( 272cf6...4fb2a3 )
by Remco
21:31 queued 14:28
created

Amount::from_json()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 16
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 16
rs 9.9666
cc 2
nc 2
nop 1
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\Mollie
9
 */
10
11
namespace Pronamic\WordPress\Pay\Gateways\Mollie;
12
13
use InvalidArgumentException;
14
use stdClass;
15
use JsonSchema\Constraints\Constraint;
16
use JsonSchema\Validator;
17
18
/**
19
 * Amount
20
 *
21
 * @author  Reüel van der Steege
22
 * @version 2.1.0
23
 * @since   2.1.0
24
 */
25
class Amount {
26
	/**
27
	 * Currency.
28
	 *
29
	 * @var string
30
	 */
31
	private $currency;
32
33
	/**
34
	 * Amount value.
35
	 *
36
	 * @var string
37
	 */
38
	private $value;
39
40
	/**
41
	 * Construct an amount.
42
	 *
43
	 * @param string $currency Currency code (ISO 4217).
44
	 * @param string $value    Amount formatted with correct number of decimals for currency.
45
	 */
46
	public function __construct( $currency, $value ) {
47
		$this->currency = $currency;
48
		$this->value    = $value;
49
	}
50
51
	/**
52
	 * Get currency.
53
	 *
54
	 * @return string
55
	 */
56
	public function get_currency() {
57
		return $this->currency;
58
	}
59
60
	/**
61
	 * Get amount.
62
	 *
63
	 * @return string
64
	 */
65
	public function get_value() {
66
		return $this->value;
67
	}
68
69
	/**
70
	 * Get JSON.
71
	 *
72
	 * @return object
73
	 */
74
	public function get_json() {
75
		return (object) array(
76
			'currency' => $this->get_currency(),
77
			'value'    => $this->get_value(),
78
		);
79
	}
80
81
	/**
82
	 * Create amount from object.
83
	 *
84
	 * @param stdClass $object Object.
85
	 *
86
	 * @return Amount
87
	 * @throws InvalidArgumentException Throws invalid argument exception when object does not contains the required properties.
88
	 */
89
	public static function from_object( stdClass $object ) {
90
		if ( ! isset( $object->currency ) ) {
91
			throw new InvalidArgumentException( 'Object must contain `currency` property.' );
92
		}
93
94
		if ( ! isset( $object->value ) ) {
95
			throw new InvalidArgumentException( 'Object must contain `value` property.' );
96
		}
97
98
		return new self(
99
			$object->currency,
100
			$object->value
101
		);
102
	}
103
104
	/**
105
	 * Create amount from JSON string.
106
	 *
107
	 * @param object $json JSON object.
108
	 *
109
	 * @return Amount
110
	 *
111
	 * @throws InvalidArgumentException Throws invalid argument exception when input JSON is not an object.
112
	 * @throws \JsonSchema\Exception\ValidationException Throws JSON schema validation exception when JSON is invalid.
113
	 */
114
	public static function from_json( $json ) {
115
		if ( ! is_object( $json ) ) {
116
			throw new InvalidArgumentException( 'JSON value must be an object.' );
117
		}
118
119
		$validator = new Validator();
120
121
		$validator->validate(
122
			$json,
123
			(object) array(
124
				'$ref' => 'file://' . realpath( __DIR__ . '/../json-schemas/amount.json' ),
125
			),
126
			Constraint::CHECK_MODE_EXCEPTIONS
127
		);
128
129
		return self::from_object( $json );
130
	}
131
132
	/**
133
	 * Create string representation of amount.
134
	 *
135
	 * @return string
136
	 */
137
	public function __toString() {
138
		return sprintf( '%1$s %2$s', $this->get_currency(), $this->get_value() );
139
	}
140
}
141