Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
CRAP | |
100.00% |
1 / 1 |
| JsonMapper_Helper | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |
100.00% |
1 / 1 |
| build | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Get a JsonMapper instance that can decode Money and DateTimeInterface. |
| 4 | * |
| 5 | * @package brianhenryie/bh-wp-bitcoin-gateway |
| 6 | */ |
| 7 | |
| 8 | namespace BrianHenryIE\WP_Bitcoin_Gateway\API\Helpers\JsonMapper; |
| 9 | |
| 10 | use BrianHenryIE\WP_Bitcoin_Gateway\Brick\Money\Money; |
| 11 | use BrianHenryIE\WP_Bitcoin_Gateway\BtcRpcExplorer\JsonMapper\AssociativeArrayMiddleware; |
| 12 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Exception\BuilderException; |
| 13 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Exception\ClassFactoryException; |
| 14 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Handler\FactoryRegistry; |
| 15 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\Handler\PropertyMapper; |
| 16 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\JsonMapperBuilder; |
| 17 | use BrianHenryIE\WP_Bitcoin_Gateway\JsonMapper\JsonMapperInterface; |
| 18 | use DateTimeInterface; |
| 19 | |
| 20 | /** |
| 21 | * @see JsonMapperBuilder::build() |
| 22 | */ |
| 23 | class JsonMapper_Helper { |
| 24 | |
| 25 | /** |
| 26 | * Get a JSONMapper instance configured with DateTimeInterface and Money helpers. |
| 27 | * |
| 28 | * @throws ClassFactoryException Something must be wrong with one of our factory implementations. Definitely should not be a run-time error to just register them. |
| 29 | * @throws BuilderException Would suggest something went wrong inside JsonMapper itself. |
| 30 | */ |
| 31 | public function build(): JsonMapperInterface { |
| 32 | |
| 33 | $factory_registry = new FactoryRegistry(); |
| 34 | |
| 35 | $factory_registry->addFactory( |
| 36 | DateTimeInterface::class, |
| 37 | new JsonMapper_DateTimeInterface() |
| 38 | ); |
| 39 | |
| 40 | $factory_registry->addFactory( |
| 41 | Money::class, |
| 42 | new JsonMapper_Money() |
| 43 | ); |
| 44 | |
| 45 | // TODO: after testing, see what -> are unnecessary. |
| 46 | $property_mapper = new PropertyMapper( $factory_registry ); |
| 47 | $mapper = JsonMapperBuilder::new() |
| 48 | ->withPropertyMapper( $property_mapper ) |
| 49 | ->withAttributesMiddleware() |
| 50 | ->withDocBlockAnnotationsMiddleware() |
| 51 | ->withTypedPropertiesMiddleware() |
| 52 | ->withNamespaceResolverMiddleware() |
| 53 | ->withObjectConstructorMiddleware( $factory_registry ) |
| 54 | ->build(); |
| 55 | $mapper->push( new AssociativeArrayMiddleware() ); |
| 56 | |
| 57 | return $mapper; |
| 58 | } |
| 59 | } |