add api list of mappers

This commit is contained in:
Jacson Passold 2018-05-23 15:06:29 -03:00
parent 91b8b27a5a
commit fd08babd04
5 changed files with 141 additions and 0 deletions

View File

@ -0,0 +1,70 @@
<?php
namespace Tainacan\API\EndPoints;
use \Tainacan\API\REST_Controller;
class REST_Field_Mappers_Controller extends REST_Controller {
/**
* REST_Field_Mappers_Controller constructor.
*/
public function __construct() {
$this->rest_base = 'field-mappers';
parent::__construct();
}
public function register_routes() {
register_rest_route($this->namespace, '/' . $this->rest_base,
array(
array(
'methods' => \WP_REST_Server::READABLE,
'callback' => array($this, 'get_items'),
'permission_callback' => array($this, 'get_items_permissions_check'),
)
)
);
}
/**
* @param \Tainacan\Exposers\Mappers\Mapper $mapper
* @param \WP_REST_Request $request
*map
* @return mixed|\WP_Error|\WP_REST_Response
*/
public function prepare_item_for_response( $mapper, $request ) {
$field_arr = $mapper->_toArray();
return $field_arr;
}
/**
* @param \WP_REST_Request $request
*
* @return \WP_Error|\WP_REST_Response
*/
public function get_items( $request ) {
$Tainacan_Exposers = \Tainacan\Exposers\Exposers::get_instance();
$field_mappers = $Tainacan_Exposers->get_mappers( 'OBJECT' );
$prepared = [];
foreach ($field_mappers as $field_mapper){
array_push($prepared, $this->prepare_item_for_response($field_mapper, $request));
}
return new \WP_REST_Response($prepared, 200);
}
/**
* @param \WP_REST_Request $request
*
* @return bool|\WP_Error
*/
public function get_items_permissions_check( $request ) {
return true;
}
}
?>

View File

@ -14,6 +14,7 @@ $rest_logs_controller = new \Tainacan\API\EndPoints\REST_Logs_Controlle
$rest_field_types_controller = new \Tainacan\API\EndPoints\REST_Field_Types_Controller(); $rest_field_types_controller = new \Tainacan\API\EndPoints\REST_Field_Types_Controller();
$rest_filter_types_controller = new \Tainacan\API\EndPoints\REST_Filter_Types_Controller(); $rest_filter_types_controller = new \Tainacan\API\EndPoints\REST_Filter_Types_Controller();
new \Tainacan\API\EndPoints\REST_Export_Controller(); new \Tainacan\API\EndPoints\REST_Export_Controller();
new \Tainacan\API\EndPoints\REST_Field_Mappers_Controller();
// Add here other endpoints imports // Add here other endpoints imports
?> ?>

View File

@ -240,4 +240,24 @@ class Exposers {
} }
return $i18n_strings; return $i18n_strings;
} }
/**
* Return list of registered mappers
* @param string $output output format, ARRAY_N or OBJECT
*/
public function get_mappers($output = ARRAY_N) {
$ret = [];
switch ($output) {
case OBJECT:
foreach ($this->mappers as $mapper) {
$ret[] = new $mapper;
}
break;
case ARRAY_N:
default:
return $this->mappers;
break;
}
return $ret;
}
} }

View File

@ -11,4 +11,17 @@ abstract class Mapper {
public $prefix = ''; // Tag prefix like "dc:" public $prefix = ''; // Tag prefix like "dc:"
public $sufix = ''; // Tag sufix public $sufix = ''; // Tag sufix
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 function _toArray() {
return [
'slug' => $this->slug,
'name' => $this->name,
'allow_extra_fields' => $this->allow_extra_fields,
'context_url' => $this->context_url,
'metadata' => $this->metadata,
'prefix' => $this->prefix,
'sufix' => $this->sufix,
'header' => $this->header
];
}
} }

View File

@ -0,0 +1,37 @@
<?php
namespace Tainacan\Tests;
/**
* @group api
*/
class TAINACAN_REST_Field_Mappers_Controller extends TAINACAN_UnitApiTestCase {
public function test_get_field_mappers(){
$field_mapper_request = new \WP_REST_Request('GET', $this->namespace . '/field-mappers');
$field_mapper_response = $this->server->dispatch($field_mapper_request);
$data = $field_mapper_response->get_data();
$Tainacan_Fields = \Tainacan\Exposers\Exposers::get_instance();
$field_mappers = $Tainacan_Fields->get_mappers("OBJECT");
$this->assertEquals(count($field_mappers), count($data));
for ($i = 0; $i < count($data); $i++) {
$this->assertEquals($field_mappers[$i]->slug, $data[$i]['slug']);
$this->assertEquals($field_mappers[$i]->name, $data[$i]['name']);
$this->assertEquals($field_mappers[$i]->allow_extra_fields, $data[$i]['allow_extra_fields']);
$this->assertEquals($field_mappers[$i]->context_url, $data[$i]['context_url']);
$this->assertEquals($field_mappers[$i]->metadata, $data[$i]['metadata']);
$this->assertEquals($field_mappers[$i]->prefix, $data[$i]['prefix']);
$this->assertEquals($field_mappers[$i]->sufix, $data[$i]['sufix']);
$this->assertEquals($field_mappers[$i]->header, $data[$i]['header']);
}
}
}
?>