add support to detect mappers and types by slug
This commit is contained in:
parent
5af903a987
commit
3beaced591
|
@ -47,12 +47,15 @@ class Exposers {
|
||||||
* @param $class_name string | object The class name or the instance
|
* @param $class_name string | object The class name or the instance
|
||||||
*/
|
*/
|
||||||
public function register_exposer_type( $class_name ){
|
public function register_exposer_type( $class_name ){
|
||||||
|
$obj = $class_name;
|
||||||
if( is_object( $class_name ) ){
|
if( is_object( $class_name ) ){
|
||||||
$class_name = get_class( $class_name );
|
$class_name = get_class( $class_name );
|
||||||
|
} else {
|
||||||
|
$obj = new $class_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!in_array( $class_name, $this->types)){
|
if(!in_array( $class_name, $this->types)){
|
||||||
$this->types[] = $class_name;
|
$this->types[$obj->slug] = $class_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,12 +65,15 @@ class Exposers {
|
||||||
* @param $class_name string | object The class name or the object instance
|
* @param $class_name string | object The class name or the object instance
|
||||||
*/
|
*/
|
||||||
public function register_exposer_mapper( $class_name ){
|
public function register_exposer_mapper( $class_name ){
|
||||||
|
$obj = $class_name;
|
||||||
if( is_object( $class_name ) ){
|
if( is_object( $class_name ) ){
|
||||||
$class_name = get_class( $class_name );
|
$class_name = get_class( $class_name );
|
||||||
|
} else {
|
||||||
|
$obj = new $class_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!in_array( $class_name, $this->mappers)){
|
if(!in_array( $class_name, $this->mappers)){
|
||||||
$this->mappers[] = $class_name;
|
$this->mappers[$obj->slug] = $class_name;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,6 +85,13 @@ class Exposers {
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
public function check_class_name($class_name, $root = false, $prefix = 'Tainacan\Exposers\Types\\') {
|
public function check_class_name($class_name, $root = false, $prefix = 'Tainacan\Exposers\Types\\') {
|
||||||
|
if(array_key_exists($class_name, $this->types)) {
|
||||||
|
$class_name = $this->types[$class_name];
|
||||||
|
$prefix = '';
|
||||||
|
} elseif( array_key_exists($class_name, $this->mappers)) {
|
||||||
|
$class_name = $this->mappers[$class_name];
|
||||||
|
$prefix = '';
|
||||||
|
}
|
||||||
$class = $prefix.sanitize_text_field($class_name);
|
$class = $prefix.sanitize_text_field($class_name);
|
||||||
$class = str_replace(['-', ' '], ['_', '_'], $class);
|
$class = str_replace(['-', ' '], ['_', '_'], $class);
|
||||||
|
|
||||||
|
@ -218,6 +231,8 @@ class Exposers {
|
||||||
* @return boolean
|
* @return boolean
|
||||||
*/
|
*/
|
||||||
public function has_mapper($mapper) {
|
public function has_mapper($mapper) {
|
||||||
|
if(array_key_exists($mapper, $this->mappers)) return true;
|
||||||
|
|
||||||
return in_array($this->check_class_name($mapper, false, self::MAPPER_CLASS_PREFIX), $this->mappers);
|
return in_array($this->check_class_name($mapper, false, self::MAPPER_CLASS_PREFIX), $this->mappers);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Csv extends Type {
|
||||||
* @var array
|
* @var array
|
||||||
*/
|
*/
|
||||||
public $mappers = ['Value'];
|
public $mappers = ['Value'];
|
||||||
|
public $slug = 'csv'; // type slug for url safe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace Tainacan\Exposers\Types;
|
||||||
class Html extends Type {
|
class Html extends Type {
|
||||||
|
|
||||||
public $mappers = ['Value'];
|
public $mappers = ['Value'];
|
||||||
|
public $slug = 'html'; // type slug for url safe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace Tainacan\Exposers\Types;
|
||||||
class OAI_PMH extends Xml {
|
class OAI_PMH extends Xml {
|
||||||
|
|
||||||
public $mappers = ['Dublin Core'];
|
public $mappers = ['Dublin Core'];
|
||||||
|
public $slug = 'oai-pmh'; // type slug for url safe
|
||||||
|
|
||||||
const XML_OAI_DC_NAMESPACE = "http://www.openarchives.org/OAI/2.0/oai_dc/";
|
const XML_OAI_DC_NAMESPACE = "http://www.openarchives.org/OAI/2.0/oai_dc/";
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -9,6 +9,7 @@ namespace Tainacan\Exposers\Types;
|
||||||
class Txt extends Type {
|
class Txt extends Type {
|
||||||
|
|
||||||
public $mappers = ['Value'];
|
public $mappers = ['Value'];
|
||||||
|
public $slug = 'txt'; // type slug for url safe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
|
@ -10,6 +10,7 @@ abstract class Type {
|
||||||
|
|
||||||
protected $mappers = true; // List of supported mapper, leave true for all
|
protected $mappers = true; // List of supported mapper, leave true for all
|
||||||
protected $extension = 'tnc'; // extension sufix for multi operation system compatibility
|
protected $extension = 'tnc'; // extension sufix for multi operation system compatibility
|
||||||
|
public $slug = ''; // type slug for url safe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Change response after api callbacks
|
* Change response after api callbacks
|
||||||
|
|
|
@ -13,6 +13,7 @@ class Xml extends Type {
|
||||||
* @var string
|
* @var string
|
||||||
*/
|
*/
|
||||||
protected $extension = 'xml';
|
protected $extension = 'xml';
|
||||||
|
public $slug = 'xml'; // type slug for url safe
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
|
|
|
@ -166,6 +166,58 @@ class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @group exposers-slug
|
||||||
|
*/
|
||||||
|
public function test_exposer_map_by_slug() {
|
||||||
|
global $Tainacan_Fields, $Tainacan_Item_Metadata;
|
||||||
|
extract($this->create_meta_requirements());
|
||||||
|
|
||||||
|
$item__metadata_json = json_encode([
|
||||||
|
'values' => 'TestValues_exposers_slug',
|
||||||
|
]);
|
||||||
|
|
||||||
|
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/' . $this->field->get_id() );
|
||||||
|
$request->set_body($item__metadata_json);
|
||||||
|
|
||||||
|
$response = $this->server->dispatch($request);
|
||||||
|
|
||||||
|
$this->assertEquals(200, $response->get_status());
|
||||||
|
|
||||||
|
$data = $response->get_data();
|
||||||
|
|
||||||
|
$this->assertEquals($this->item->get_id(), $data['item']['id']);
|
||||||
|
$this->assertEquals('TestValues_exposers_slug', $data['value']);
|
||||||
|
|
||||||
|
$item_exposer_json = json_encode([
|
||||||
|
'exposer-map' => 'dublin-core',
|
||||||
|
]);
|
||||||
|
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata/'. $this->field->get_id() );
|
||||||
|
$request->set_body($item_exposer_json);
|
||||||
|
$response = $this->server->dispatch($request);
|
||||||
|
$this->assertEquals(200, $response->get_status());
|
||||||
|
$data = $response->get_data();
|
||||||
|
$this->assertEquals('TestValues_exposers_slug', $data['dc:language']);
|
||||||
|
|
||||||
|
$item_exposer_json = json_encode([
|
||||||
|
'exposer-type' => 'xml',
|
||||||
|
'exposer-map' => 'dublin-core',
|
||||||
|
]);
|
||||||
|
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $this->item->get_id() . '/metadata' );
|
||||||
|
$request->set_body($item_exposer_json);
|
||||||
|
$response = $this->server->dispatch($request);
|
||||||
|
$this->assertEquals(200, $response->get_status());
|
||||||
|
$data = $response->get_data();
|
||||||
|
|
||||||
|
$xml = new \SimpleXMLElement($data);
|
||||||
|
$rdf = $xml->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_RDF_NAMESPACE);
|
||||||
|
$dc = $rdf->children(\Tainacan\Exposers\Mappers\Dublin_Core::XML_DC_NAMESPACE);
|
||||||
|
|
||||||
|
$this->assertEquals('adasdasdsa', $dc->description);
|
||||||
|
$this->assertEquals('item_teste_Expose', $dc->title);
|
||||||
|
$this->assertEquals('TestValues_exposers_slug', $dc->language);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @group oai-pmh
|
* @group oai-pmh
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Reference in New Issue