Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| JsonMapper_Money | |
100.00% |
19 / 19 |
|
100.00% |
2 / 2 |
9 | |
100.00% |
1 / 1 |
| __invoke | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
| validate | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
8 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * JsonMapper deserializer for Money objects. |
| 4 | * |
| 5 | * @see FactoryRegistry::addFactory() |
| 6 | * |
| 7 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 8 | */ |
| 9 | |
| 10 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Helpers\JsonMapper; |
| 11 | |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\API\Model\Exceptions\BH_WP_Bitcoin_Gateway_Exception; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Math\Exception\NumberFormatException; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Exception\UnknownCurrencyException; |
| 15 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money; |
| 16 | |
| 17 | /** |
| 18 | * @see Money::jsonSerialize() |
| 19 | */ |
| 20 | class JsonMapper_Money { |
| 21 | |
| 22 | /** |
| 23 | * Callable function for parsing Money by JsonMapper factory. |
| 24 | * |
| 25 | * @param object{amount?:string|mixed, currency?:string|mixed} $json_object The JSON object to parse to Money. |
| 26 | * |
| 27 | * @throws BH_WP_Bitcoin_Gateway_Exception If the JSON object doesn't contain the expected properties:types. |
| 28 | * @throws UnknownCurrencyException If `$json_object->currency` isn't a known brick/money currency. |
| 29 | */ |
| 30 | public function __invoke( object $json_object ): Money { |
| 31 | |
| 32 | $this->validate( $json_object ); |
| 33 | /** @var object{amount:string, currency:string} $json_object */ |
| 34 | |
| 35 | return Money::of( |
| 36 | amount: $json_object->amount, |
| 37 | currency:$json_object->currency |
| 38 | ); |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Confirm the JSON object has the expected keys in their expected types. |
| 43 | * |
| 44 | * @param object{amount?:string|mixed, currency?:string|mixed} $json_object The JSON string (as object) that JsonMapper has been told will parse to Money. |
| 45 | * |
| 46 | * @throws BH_WP_Bitcoin_Gateway_Exception If the JSON object doesn't contain the expected properties:types. |
| 47 | */ |
| 48 | protected function validate( object $json_object ): void { |
| 49 | if ( |
| 50 | property_exists( $json_object, 'amount' ) |
| 51 | && property_exists( $json_object, 'currency' ) |
| 52 | && is_numeric( $json_object->amount ) |
| 53 | && is_string( $json_object->currency ) |
| 54 | ) { |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | $previous_exception = null; |
| 59 | if ( property_exists( $json_object, 'amount' ) && ! is_numeric( $json_object->amount ) ) { |
| 60 | $previous_exception = new NumberFormatException( |
| 61 | message: is_string( $json_object->amount ) ? $json_object->amount : '' |
| 62 | ); |
| 63 | } |
| 64 | |
| 65 | throw new BH_WP_Bitcoin_Gateway_Exception( |
| 66 | message: 'Invalid json encoded money object.', |
| 67 | previous: $previous_exception |
| 68 | ); |
| 69 | } |
| 70 | } |