Exceptions
2 years ago
Formatters
1 year ago
Payments
3 months ago
Routes
3 months ago
Schemas
3 months ago
Utilities
3 months ago
Authentication.php
3 months ago
Formatters.php
2 years ago
Legacy.php
1 year ago
RoutesController.php
6 months ago
SchemaController.php
6 months ago
SessionHandler.php
9 months ago
StoreApi.php
3 months ago
deprecated.php
2 years ago
functions.php
2 years ago
Formatters.php
48 lines
| 1 | <?php |
| 2 | namespace Automattic\WooCommerce\StoreApi; |
| 3 | |
| 4 | use \Exception; |
| 5 | use Automattic\WooCommerce\StoreApi\Formatters\DefaultFormatter; |
| 6 | |
| 7 | /** |
| 8 | * Formatters class. |
| 9 | * |
| 10 | * Allows formatter classes to be registered. Formatters are exposed to extensions via the ExtendSchema class. |
| 11 | */ |
| 12 | class Formatters { |
| 13 | /** |
| 14 | * Holds an array of formatter class instances. |
| 15 | * |
| 16 | * @var array |
| 17 | */ |
| 18 | private $formatters = []; |
| 19 | |
| 20 | /** |
| 21 | * Get a new instance of a formatter class. |
| 22 | * |
| 23 | * @throws Exception An Exception is thrown if a non-existing formatter is used and the user is admin. |
| 24 | * |
| 25 | * @param string $name Name of the formatter. |
| 26 | * @return FormatterInterface Formatter class instance. |
| 27 | */ |
| 28 | public function __get( $name ) { |
| 29 | if ( ! isset( $this->formatters[ $name ] ) ) { |
| 30 | if ( defined( 'WP_DEBUG' ) && WP_DEBUG && current_user_can( 'manage_woocommerce' ) ) { |
| 31 | throw new Exception( $name . ' formatter does not exist' ); |
| 32 | } |
| 33 | return new DefaultFormatter(); |
| 34 | } |
| 35 | return $this->formatters[ $name ]; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * Register a formatter class for usage. |
| 40 | * |
| 41 | * @param string $name Name of the formatter. |
| 42 | * @param string $class A formatter class name. |
| 43 | */ |
| 44 | public function register( $name, $class ) { |
| 45 | $this->formatters[ $name ] = new $class(); |
| 46 | } |
| 47 | } |
| 48 |