Traits
4 weeks ago
AbstractDomainAbility.php
4 weeks ago
OrderAddNote.php
4 weeks ago
OrderUpdateStatus.php
4 weeks ago
OrdersQuery.php
4 weeks ago
ProductCreate.php
4 weeks ago
ProductDelete.php
4 weeks ago
ProductUpdate.php
4 weeks ago
ProductsQuery.php
4 weeks ago
AbstractDomainAbility.php
97 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Domain ability base class file. |
| 4 | */ |
| 5 | |
| 6 | declare( strict_types=1 ); |
| 7 | |
| 8 | namespace Automattic\WooCommerce\Internal\Abilities\Domain; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | /** |
| 13 | * Shared helpers for WooCommerce domain ability definitions. |
| 14 | */ |
| 15 | abstract class AbstractDomainAbility { |
| 16 | |
| 17 | /** |
| 18 | * Get a collection output schema. |
| 19 | * |
| 20 | * @param string $collection_key Collection property key. |
| 21 | * @param array $item_schema JSON schema describing a single item in the collection. |
| 22 | * @return array |
| 23 | */ |
| 24 | protected static function get_collection_output_schema( string $collection_key, array $item_schema ): array { |
| 25 | return array( |
| 26 | 'type' => 'object', |
| 27 | 'properties' => array( |
| 28 | $collection_key => array( |
| 29 | 'type' => 'array', |
| 30 | 'description' => sprintf( |
| 31 | /* translators: %s: Collection key, such as products or orders. */ |
| 32 | __( 'Returned %s for the current page.', 'woocommerce' ), |
| 33 | $collection_key |
| 34 | ), |
| 35 | 'items' => $item_schema, |
| 36 | ), |
| 37 | 'total_pages' => array( |
| 38 | 'type' => 'integer', |
| 39 | 'description' => __( 'Total number of result pages available for the current query.', 'woocommerce' ), |
| 40 | ), |
| 41 | 'page' => array( |
| 42 | 'type' => 'integer', |
| 43 | 'description' => __( 'Current result page.', 'woocommerce' ), |
| 44 | ), |
| 45 | 'per_page' => array( |
| 46 | 'type' => 'integer', |
| 47 | 'description' => __( 'Maximum number of items requested per page.', 'woocommerce' ), |
| 48 | ), |
| 49 | ), |
| 50 | 'additionalProperties' => false, |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get an entity output schema. |
| 56 | * |
| 57 | * @param string $entity_key Entity property key. |
| 58 | * @param array $item_schema JSON schema describing the entity. |
| 59 | * @return array |
| 60 | */ |
| 61 | protected static function get_entity_output_schema( string $entity_key, array $item_schema ): array { |
| 62 | return array( |
| 63 | 'type' => 'object', |
| 64 | 'properties' => array( |
| 65 | $entity_key => $item_schema, |
| 66 | ), |
| 67 | 'additionalProperties' => false, |
| 68 | ); |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get a delete output schema. |
| 73 | * |
| 74 | * @return array |
| 75 | */ |
| 76 | protected static function get_delete_output_schema(): array { |
| 77 | return array( |
| 78 | 'type' => 'object', |
| 79 | 'properties' => array( |
| 80 | 'deleted' => array( 'type' => 'boolean' ), |
| 81 | 'id' => array( 'type' => 'integer' ), |
| 82 | ), |
| 83 | 'additionalProperties' => false, |
| 84 | ); |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Get an ID value from ability input. |
| 89 | * |
| 90 | * @param mixed $input Ability input. |
| 91 | * @return int |
| 92 | */ |
| 93 | protected static function get_id_from_input( $input ): int { |
| 94 | return is_array( $input ) && ! empty( $input['id'] ) ? (int) $input['id'] : 0; |
| 95 | } |
| 96 | } |
| 97 |