From 3beaced59110fae79fdf56e1361914d021aee5f0 Mon Sep 17 00:00:00 2001 From: Jacson Passold Date: Tue, 29 May 2018 20:15:52 -0300 Subject: [PATCH] add support to detect mappers and types by slug --- src/exposers/class-tainacan-exposers.php | 19 ++++++- src/exposers/types/class-tainacan-csv.php | 3 +- src/exposers/types/class-tainacan-html.php | 3 +- src/exposers/types/class-tainacan-oai-pmh.php | 1 + src/exposers/types/class-tainacan-txt.php | 1 + src/exposers/types/class-tainacan-type.php | 1 + src/exposers/types/class-tainacan-xml.php | 1 + tests/test-api-exposers.php | 52 +++++++++++++++++++ 8 files changed, 77 insertions(+), 4 deletions(-) diff --git a/src/exposers/class-tainacan-exposers.php b/src/exposers/class-tainacan-exposers.php index 2560d9b7f..0dc0ed6fa 100644 --- a/src/exposers/class-tainacan-exposers.php +++ b/src/exposers/class-tainacan-exposers.php @@ -47,12 +47,15 @@ class Exposers { * @param $class_name string | object The class name or the instance */ public function register_exposer_type( $class_name ){ + $obj = $class_name; if( is_object( $class_name ) ){ $class_name = get_class( $class_name ); + } else { + $obj = new $class_name; } 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 */ public function register_exposer_mapper( $class_name ){ + $obj = $class_name; if( is_object( $class_name ) ){ $class_name = get_class( $class_name ); + } else { + $obj = new $class_name; } 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 */ 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 = str_replace(['-', ' '], ['_', '_'], $class); @@ -218,6 +231,8 @@ class Exposers { * @return boolean */ 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); } diff --git a/src/exposers/types/class-tainacan-csv.php b/src/exposers/types/class-tainacan-csv.php index 48e9c389f..b2d02bccc 100644 --- a/src/exposers/types/class-tainacan-csv.php +++ b/src/exposers/types/class-tainacan-csv.php @@ -12,7 +12,8 @@ class Csv extends Type { * List of supported mappers * @var array */ - public $mappers = ['Value']; + public $mappers = ['Value']; + public $slug = 'csv'; // type slug for url safe /** * diff --git a/src/exposers/types/class-tainacan-html.php b/src/exposers/types/class-tainacan-html.php index 101b7e985..0e0506f92 100644 --- a/src/exposers/types/class-tainacan-html.php +++ b/src/exposers/types/class-tainacan-html.php @@ -8,7 +8,8 @@ namespace Tainacan\Exposers\Types; */ class Html extends Type { - public $mappers = ['Value']; + public $mappers = ['Value']; + public $slug = 'html'; // type slug for url safe /** * diff --git a/src/exposers/types/class-tainacan-oai-pmh.php b/src/exposers/types/class-tainacan-oai-pmh.php index 94a5877b8..914234217 100644 --- a/src/exposers/types/class-tainacan-oai-pmh.php +++ b/src/exposers/types/class-tainacan-oai-pmh.php @@ -9,6 +9,7 @@ namespace Tainacan\Exposers\Types; class OAI_PMH extends Xml { 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/"; /** diff --git a/src/exposers/types/class-tainacan-txt.php b/src/exposers/types/class-tainacan-txt.php index 133e3167e..1ca0f1d9f 100644 --- a/src/exposers/types/class-tainacan-txt.php +++ b/src/exposers/types/class-tainacan-txt.php @@ -9,6 +9,7 @@ namespace Tainacan\Exposers\Types; class Txt extends Type { public $mappers = ['Value']; + public $slug = 'txt'; // type slug for url safe /** * diff --git a/src/exposers/types/class-tainacan-type.php b/src/exposers/types/class-tainacan-type.php index 967f59a1d..f86f14d83 100644 --- a/src/exposers/types/class-tainacan-type.php +++ b/src/exposers/types/class-tainacan-type.php @@ -10,6 +10,7 @@ abstract class Type { protected $mappers = true; // List of supported mapper, leave true for all protected $extension = 'tnc'; // extension sufix for multi operation system compatibility + public $slug = ''; // type slug for url safe /** * Change response after api callbacks diff --git a/src/exposers/types/class-tainacan-xml.php b/src/exposers/types/class-tainacan-xml.php index 9943a76cd..457ef3347 100644 --- a/src/exposers/types/class-tainacan-xml.php +++ b/src/exposers/types/class-tainacan-xml.php @@ -13,6 +13,7 @@ class Xml extends Type { * @var string */ protected $extension = 'xml'; + public $slug = 'xml'; // type slug for url safe /** * diff --git a/tests/test-api-exposers.php b/tests/test-api-exposers.php index 9a3fbc925..2cec5bfb1 100644 --- a/tests/test-api-exposers.php +++ b/tests/test-api-exposers.php @@ -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 */