feat: add custom mapper in response

This commit is contained in:
vnmedeiros 2023-10-11 16:22:58 -03:00
parent e24549fe72
commit dc804a7cba
3 changed files with 36 additions and 4 deletions

View File

@ -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 * @param \WP_REST_Request $request
*map *map
* @return mixed|\WP_Error|\WP_REST_Response * @return mixed|\WP_Error|\WP_REST_Response
*/ */
public function prepare_item_for_response( $mapper, $request ) { 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; return $metadatum_arr;
} }

View File

@ -36,14 +36,14 @@ abstract class Mapper {
public $header = false; // API response header or file header to be used with 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 $show_ui = true; // Show mapper in ui and api calls
public function _toArray() { public function _toArray($collection_id = null) {
return [ return [
'slug' => $this->slug, 'slug' => $this->slug,
'name' => $this->name, 'name' => $this->name,
'description' => $this->description, 'description' => $this->description,
'allow_extra_metadata' => $this->allow_extra_metadata, 'allow_extra_metadata' => $this->allow_extra_metadata,
'context_url' => $this->context_url, 'context_url' => $this->context_url,
'metadata' => $this->metadata, 'metadata' => $this->get_metadata($collection_id),
'prefix' => $this->prefix, 'prefix' => $this->prefix,
'sufix' => $this->sufix, 'sufix' => $this->sufix,
'header' => $this->header, 'header' => $this->header,
@ -51,6 +51,10 @@ abstract class Mapper {
]; ];
} }
public function get_metadata($collection_id = null) {
return $this->metadata;
}
/** /**
* Gets the semantic URL for a given metadatum of this mapper. * Gets the semantic URL for a given metadatum of this mapper.
* Basically it identifies the property prefix and replace it with the URL of that prefix * Basically it identifies the property prefix and replace it with the URL of that prefix

View File

@ -20,4 +20,31 @@ class Wiki_Data extends Mapper {
public $metadata = [ 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);
}
} }