From dc804a7cba52ba03019eb9dc0cea7692bff845e5 Mon Sep 17 00:00:00 2001 From: vnmedeiros Date: Wed, 11 Oct 2023 16:22:58 -0300 Subject: [PATCH] feat: add custom mapper in response --- ...acan-rest-metadatum-mappers-controller.php | 5 ++-- src/classes/mappers/class-tainacan-mapper.php | 8 ++++-- .../mappers/class-tainacan-wiki-data.php | 27 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php index 7f4512ff9..20b47257d 100644 --- a/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php +++ b/src/classes/api/endpoints/class-tainacan-rest-metadatum-mappers-controller.php @@ -68,14 +68,15 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller { } /** - * @param \Tainacan\Exposers\Mappers\Mapper $mapper + * @param \Tainacan\Mappers\Mapper $mapper * @param \WP_REST_Request $request *map * @return mixed|\WP_Error|\WP_REST_Response */ public function prepare_item_for_response( $mapper, $request ) { - $metadatum_arr = $mapper->_toArray(); + $collection_id = isset($request['collection_id']) && $request['collection_id'] != 'default' ? $request['collection_id'] : null; + $metadatum_arr = $mapper->_toArray($collection_id); return $metadatum_arr; } diff --git a/src/classes/mappers/class-tainacan-mapper.php b/src/classes/mappers/class-tainacan-mapper.php index d9e8f7543..3a6dcc5e5 100644 --- a/src/classes/mappers/class-tainacan-mapper.php +++ b/src/classes/mappers/class-tainacan-mapper.php @@ -36,20 +36,24 @@ abstract class Mapper { public $header = false; // API response header or file header to be used with public $show_ui = true; // Show mapper in ui and api calls - public function _toArray() { + public function _toArray($collection_id = null) { return [ 'slug' => $this->slug, 'name' => $this->name, 'description' => $this->description, 'allow_extra_metadata' => $this->allow_extra_metadata, 'context_url' => $this->context_url, - 'metadata' => $this->metadata, + 'metadata' => $this->get_metadata($collection_id), 'prefix' => $this->prefix, 'sufix' => $this->sufix, 'header' => $this->header, 'add_meta_form' => '' ]; } + + public function get_metadata($collection_id = null) { + return $this->metadata; + } /** * Gets the semantic URL for a given metadatum of this mapper. diff --git a/src/classes/mappers/class-tainacan-wiki-data.php b/src/classes/mappers/class-tainacan-wiki-data.php index 780881c9f..1c8ae8fd9 100644 --- a/src/classes/mappers/class-tainacan-wiki-data.php +++ b/src/classes/mappers/class-tainacan-wiki-data.php @@ -20,4 +20,31 @@ class Wiki_Data extends Mapper { public $metadata = [ ]; + + public function get_metadata($collection_id = null) { + if($collection_id == null) + return $this->metadata; + + $metadatum_repository = \tainacan_metadata(); + $collection = new \Tainacan\Entities\Collection( $collection_id ); + $args = [ + 'meta_query' => [ + [ + 'key' => 'exposer_mapping', + 'compare' => 'EXISTS', + ] + ], + 'posts_per_page' => -1 + ]; + $metadata = $metadatum_repository->fetch_by_collection( $collection, $args ); + $prepared_custom_mapper = []; + foreach ( $metadata as $item ) { + $exposer_mapping = $item->get_exposer_mapping(); + if(isset($exposer_mapping[$this->slug])) { + $slug = $exposer_mapping[$this->slug]['slug']; + $prepared_custom_mapper[$slug] = $exposer_mapping[$this->slug]; + } + } + return array_merge( $this->metadata, $prepared_custom_mapper); + } } \ No newline at end of file