*/ private $query_args; /** * Constructor. * * @since 0.1.0 * * @param Services_API $services_api The services API instance. * @param array $query_args { * The query arguments. * * @type string[] $slugs Array of service slugs to limit results to. Default empty array. * @type string $orderby Field to order results by. Currently only 'slug' is supported. Default 'slug'. * @type string $order Order of results. Either 'ASC' or 'DESC'. Default 'ASC'. * @type int $number Number of services to limit results to. Default 10. * @type int $offset Offset at which to start with the results. Default 0. * } */ public function __construct( Services_API $services_api, array $query_args ) { $this->services_api = $services_api; $this->query_args = $this->parse_defaults( $query_args ); } /** * Queries services. * * @since 0.1.0 * * @return Service_Entity[] List of service entities. */ public function get_entities(): array { $slugs = $this->services_api->get_registered_service_slugs(); if ( count( $this->query_args['slugs'] ) > 0 ) { $slugs = array_intersect( $slugs, $this->query_args['slugs'] ); } // For now the only supported orderby is 'slug', so we can just sort the slugs. if ( 'DESC' === $this->query_args['order'] ) { rsort( $slugs ); } else { sort( $slugs ); } $slugs = array_slice( $slugs, $this->query_args['offset'], $this->query_args['number'] ); return array_map( function ( $slug ) { return new Service_Entity( $this->services_api, $slug ); }, $slugs ); } /** * Queries service IDs. * * @since 0.1.0 * * @return int[] Empty array, as services do not have IDs. */ public function get_ids(): array { return array(); } /** * Queries the service count. * * @since 0.1.0 * * @return int Service count. */ public function get_count(): int { $slugs = $this->services_api->get_registered_service_slugs(); if ( count( $this->query_args['slugs'] ) > 0 ) { $slugs = array_intersect( $slugs, $this->query_args['slugs'] ); } return count( $slugs ); } /** * Parses and sanitizes the given query arguments. * * @since 0.1.0 * * @param array $query_args Query arguments. * @return array Query arguments parsed with defaults. */ private function parse_defaults( array $query_args ): array { $defaults = array( 'slugs' => array(), 'orderby' => 'slug', 'order' => 'ASC', 'number' => 10, 'offset' => 0, ); // Parse defaults and strip any keys that are not allowed. $query_args = wp_parse_args( $query_args, $defaults ); $query_args = wp_array_slice_assoc( $query_args, array_keys( $defaults ) ); $query_args['order'] = strtoupper( $query_args['order'] ) === 'DESC' ? 'DESC' : 'ASC'; return $query_args; } }