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