refactoring exposer type and mapping params to use global const to easy

change names
This commit is contained in:
Jacson Passold 2018-07-12 15:22:35 -03:00
parent 1d0fb3fa97
commit d0466cd2e7
8 changed files with 53 additions and 46 deletions

View File

@ -179,6 +179,8 @@ class Admin {
'admin_url' => admin_url(),
'custom_header_support' => get_theme_support('custom-header'),
'registered_view_modes' => \Tainacan\Theme_Helper::get_instance()->get_registered_view_modes(),
'exposer_mapper_param' => \Tainacan\Exposers\Exposers::MAPPER_PARAM,
'exposer_type_param' => \Tainacan\Exposers\Exposers::TYPE_PARAM
];
$maps = [

View File

@ -101,7 +101,7 @@ class REST_Metadatum_Mappers_Controller extends REST_Controller {
array_key_exists('metadata_mappers', $body) &&
is_array($body['metadata_mappers']) &&
count($body['metadata_mappers']) > 0 &&
array_key_exists('exposer_map', $body)
\Tainacan\Exposers\Exposers::request_has_mapper($request)
) {
$metadatum_mapper = $body['metadata_mappers'][0];
$metadatum = \Tainacan\Repositories\Repository::get_entity_by_post($metadatum_mapper['metadatum_id']);

View File

@ -14,7 +14,10 @@ class Exposers {
protected $mappers = [];
private static $instance = null;
private static $request = null;
const MAPPER_CLASS_PREFIX = 'Tainacan\Exposers\Mappers\\';
const MAPPER_PARAM = 'mapper';
const TYPE_PARAM = 'exposer';
public static function get_instance() {
if ( ! isset( self::$instance ) ) {
@ -199,8 +202,8 @@ class Exposers {
$Tainacan_Exposers = self::get_instance();
$query_url_params = $request->get_query_params();
if (
is_array($query_url_params) && array_key_exists('exposer_type', $query_url_params) &&
$Tainacan_Exposers->has_type($query_url_params['exposer_type'])
is_array($query_url_params) && array_key_exists(self::TYPE_PARAM, $query_url_params) &&
$Tainacan_Exposers->has_type($query_url_params[self::TYPE_PARAM])
) {
return true;
}
@ -254,16 +257,16 @@ class Exposers {
$query_url_params = $request->get_query_params();
$Tainacan_Exposers = self::get_instance();
if(
is_array($body) && array_key_exists('exposer_type', $body) &&
$Tainacan_Exposers->has_type($body['exposer_type'])
is_array($body) && array_key_exists(self::TYPE_PARAM, $body) &&
$Tainacan_Exposers->has_type($body[self::TYPE_PARAM])
) {
$type = $Tainacan_Exposers->check_class_name($body['exposer_type'], true);
$type = $Tainacan_Exposers->check_class_name($body[self::TYPE_PARAM], true);
return new $type;
} elseif (
is_array($query_url_params) && array_key_exists('exposer_type', $query_url_params) &&
$Tainacan_Exposers->has_type($query_url_params['exposer_type'])
is_array($query_url_params) && array_key_exists(self::TYPE_PARAM, $query_url_params) &&
$Tainacan_Exposers->has_type($query_url_params[self::TYPE_PARAM])
){
$type = $Tainacan_Exposers->check_class_name($query_url_params['exposer_type'], true);
$type = $Tainacan_Exposers->check_class_name($query_url_params[self::TYPE_PARAM], true);
return new $type;
}
return false;
@ -290,27 +293,27 @@ class Exposers {
$type = self::request_has_type($request);
if( // There are a defined mapper
is_array($body) && array_key_exists('exposer_map', $body) &&
$Tainacan_Exposers->has_mapper($body['exposer_map'])
is_array($body) && array_key_exists(self::MAPPER_PARAM, $body) &&
$Tainacan_Exposers->has_mapper($body[self::MAPPER_PARAM])
) {
if(
$type === false || // do not have a exposer type
$type->get_mappers() === true || // the type accept all mappers
( is_array($type->get_mappers()) && in_array($body['exposer_map'], $type->get_mappers()) )
( is_array($type->get_mappers()) && in_array($body[self::MAPPER_PARAM], $type->get_mappers()) )
) { // the current mapper is accepted by type
$mapper = $Tainacan_Exposers->check_class_name($body['exposer_map'], true, self::MAPPER_CLASS_PREFIX);
$mapper = $Tainacan_Exposers->check_class_name($body[self::MAPPER_PARAM], true, self::MAPPER_CLASS_PREFIX);
return new $mapper;
}
} elseif(
is_array($query_url_params) && array_key_exists('exposer_map', $query_url_params) &&
$Tainacan_Exposers->has_mapper($query_url_params['exposer_map'])
is_array($query_url_params) && array_key_exists(self::MAPPER_PARAM, $query_url_params) &&
$Tainacan_Exposers->has_mapper($query_url_params[self::MAPPER_PARAM])
) {
if(
$type === false || // do not have a exposer type
$type->get_mappers() === true || // the type accept all mappers
( is_array($type->get_mappers()) && in_array($query_url_params['exposer_map'], $type->get_mappers()) )
( is_array($type->get_mappers()) && in_array($query_url_params[self::MAPPER_PARAM], $type->get_mappers()) )
) { // the current mapper is accepted by type
$mapper = $Tainacan_Exposers->check_class_name($query_url_params['exposer_map'], true, self::MAPPER_CLASS_PREFIX);
$mapper = $Tainacan_Exposers->check_class_name($query_url_params[self::MAPPER_PARAM], true, self::MAPPER_CLASS_PREFIX);
return new $mapper;
}
} elseif( is_object($type) && is_array($type->get_mappers()) && count($type->get_mappers()) > 0 ) { //there are no defined mapper, let use the first one o list if has a list

View File

@ -217,12 +217,13 @@ export const updateCollection = ({ commit }, {
export const sendCollection = ( { commit }, { name, description, status, mapper }) => {
return new Promise(( resolve, reject ) => {
axios.tainacan.post('/collections/', {
var param = {
name: name,
description: description,
status: status,
exposer_map: mapper
})
};
param[tainacan_plugin.exposer_mapper_param] = mapper;
axios.tainacan.post('/collections/', param)
.then( res => {
commit('setCollection', { name: name, description: description, status: status, mapper: mapper });
resolve( res.data );

View File

@ -170,10 +170,11 @@ export const fetchMetadatumMappers = ({commit}) => {
export const updateMetadataMapperMetadata = ({ dispatch }, {metadataMapperMetadata, mapper}) => {
return new Promise((resolve, reject) => {
axios.tainacan.post('/metadatum-mappers', {
var param = {
metadata_mappers: metadataMapperMetadata,
exposer_map: mapper
}).then((res) => {
};
param[tainacan_plugin.exposer_mapper_param] = mapper;
axios.tainacan.post('/metadatum-mappers', param).then((res) => {
resolve(res.data);
})
.catch((error) => {

View File

@ -193,9 +193,9 @@ class TAINACAN_REST_Export_Controller extends TAINACAN_UnitApiTestCase {
extract($this->create_requirements());
$item_exposer_json = json_encode([
'exposer_type' => 'Xml',
'exposer_map' => 'Value',
'export-background' => false
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Xml',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Value',
'export-background' => false
]);
$query = [
@ -232,7 +232,7 @@ class TAINACAN_REST_Export_Controller extends TAINACAN_UnitApiTestCase {
extract($this->create_requirements());
$item_exposer_json = json_encode([
'exposer_type' => 'Xml',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Xml',
'exposer-map' => 'Value',
'export-background' => false
]);

View File

@ -83,7 +83,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
'exposer_map' => 'Value',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
$request->set_body($item_exposer_json);
@ -129,7 +129,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
'exposer_type' => 'Xml',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Xml',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
@ -141,7 +141,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertInstanceOf('SimpleXMLElement', @simplexml_load_string($data));
$item_exposer_json = json_encode([
'exposer_map' => 'Dublin Core',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Dublin Core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
$request->set_body($item_exposer_json);
@ -151,8 +151,8 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers', $data['dc:language']);
$item_exposer_json = json_encode([
'exposer_type' => 'Xml',
'exposer_map' => 'Dublin Core',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Xml',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Dublin Core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
@ -194,7 +194,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers_slug', $data['value']);
$item_exposer_json = json_encode([
'exposer_map' => 'dublin-core',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'dublin-core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->metadatum->get_id() );
$request->set_body($item_exposer_json);
@ -204,8 +204,8 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers_slug', $data['dc:language']);
$item_exposer_json = json_encode([
'exposer_type' => 'xml',
'exposer_map' => 'dublin-core',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'xml',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'dublin-core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
@ -231,7 +231,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
extract($this->create_meta_requirements());
$item_exposer_json = json_encode([
'exposer_type' => 'OAI-PMH',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'OAI-PMH',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
@ -254,8 +254,8 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
extract($this->create_meta_requirements());
$item_exposer_json = json_encode([
'exposer_type' => 'Html',
'exposer_map' => 'Value'
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Html',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Value'
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
@ -314,7 +314,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals(200, $response->get_status());
$item_exposer_json = json_encode([
'exposer_type' => 'Csv',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'Csv',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);
@ -387,7 +387,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
);
$item_exposer_json = json_encode([
'exposer_map' => 'Value',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Value',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/items/' . $this->item->get_id() );
@ -406,7 +406,7 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
*/
public function test_mapped_new_collection() {
$collection_JSON = json_encode([
'exposer_map' => 'Dublin Core',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'Dublin Core',
'name' => 'TesteJsonAddDublin_Core',
'description' => 'Teste JSON Dublin Core mapped',
'status' => 'publish'
@ -462,8 +462,8 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
$this->assertEquals('TestValues_exposers', $data['value']);
$item_exposer_json = json_encode([
'exposer_type' => 'json-ld',
'exposer_map' => 'dublin-core',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'json-ld',
\Tainacan\Exposers\Exposers::MAPPER_PARAM => 'dublin-core',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);

View File

@ -104,7 +104,7 @@ class TAINACAN_REST_Metadatum_Mappers_Controller extends TAINACAN_UnitApiTestCas
['metadatum_id' => $metadatum->get_id(), 'mapper_metadata' => 'contributor'],
['metadatum_id' => $metadatum2->get_id(), 'mapper_metadata' => 'coverage']
],
'exposer_map' => $dc->slug
\Tainacan\Exposers\Exposers::MAPPER_PARAM => $dc->slug
]);
$metadatum_mapper_request->set_body($metadatum_mapper_json);
$metadatum_mapper_response = $this->server->dispatch($metadatum_mapper_request);
@ -144,7 +144,7 @@ class TAINACAN_REST_Metadatum_Mappers_Controller extends TAINACAN_UnitApiTestCas
['metadatum_id' => $metadatum->get_id(), 'mapper_metadata' => 'contributor'],
['metadatum_id' => $metadatum2->get_id(), 'mapper_metadata' => $new_metadatum_mapper ]
],
'exposer_map' => $dc->slug
\Tainacan\Exposers\Exposers::MAPPER_PARAM => $dc->slug
]);
$metadatum_mapper_request->set_body($metadatum_mapper_json);
$metadatum_mapper_response = $this->server->dispatch($metadatum_mapper_request);
@ -168,7 +168,7 @@ class TAINACAN_REST_Metadatum_Mappers_Controller extends TAINACAN_UnitApiTestCas
$this->assertEquals(200, $response->get_status());
$item_exposer_json = json_encode([
'exposer_type' => 'OAI-PMH',
\Tainacan\Exposers\Exposers::TYPE_PARAM => 'OAI-PMH',
]);
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata' );
$request->set_body($item_exposer_json);