add xml exposer prototype
This commit is contained in:
parent
e7e028081d
commit
fa646da2c8
|
@ -10,6 +10,7 @@ const TAPI_DIR = __DIR__ . '/../api/';
|
|||
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
|
||||
const HELPERS_DIR = __DIR__ . '/../helpers/';
|
||||
const IMPORTER_DIR = __DIR__ . '/../importer/';
|
||||
const EXPOSERS_DIR = __DIR__ . '/../exposers/';
|
||||
|
||||
const DIRS = [
|
||||
CLASSES_DIR,
|
||||
|
@ -20,12 +21,14 @@ const DIRS = [
|
|||
TRAITS_DIR,
|
||||
TAPI_DIR,
|
||||
ENDPOINTS_DIR,
|
||||
IMPORTER_DIR
|
||||
IMPORTER_DIR,
|
||||
EXPOSERS_DIR
|
||||
];
|
||||
|
||||
require_once(VENDOR_DIR . 'autoload.php');
|
||||
require_once(HELPERS_DIR . 'class-tainacan-helpers-html.php');
|
||||
require_once(IMPORTER_DIR . 'class-tainacan-importer.php');
|
||||
require_once(EXPOSERS_DIR . 'class-tainacan-exposers.php');
|
||||
|
||||
spl_autoload_register('tainacan_autoload');
|
||||
|
||||
|
@ -47,21 +50,24 @@ function tainacan_autoload($class_name){
|
|||
|
||||
if( isset( $class_path[1] ) && $class_path[1] === 'Importer' ){
|
||||
$dir = IMPORTER_DIR;
|
||||
} else if($sliced) {
|
||||
} else if( isset( $class_path[1] ) && $class_path[1] === 'Exposers' ){
|
||||
$dir = EXPOSERS_DIR;
|
||||
if(count($class_path) > 3) $dir .= strtolower($class_path[2]).DIRECTORY_SEPARATOR;
|
||||
} else if($sliced) {
|
||||
$lower = $sliced[0];
|
||||
$sliced[0] = strtolower( $lower );
|
||||
|
||||
$dir = implode( DIRECTORY_SEPARATOR, $sliced ) . '/';
|
||||
$dir = implode( DIRECTORY_SEPARATOR, $sliced ) . DIRECTORY_SEPARATOR;
|
||||
$dir = CLASSES_DIR . str_replace( '_', '-', $dir );
|
||||
} else {
|
||||
$dir = CLASSES_DIR;
|
||||
}
|
||||
|
||||
if( in_array('Field_Types', $class_path) || in_array('Filter_Types', $class_path) ){
|
||||
if( in_array('Field_Types', $class_path) || in_array('Filter_Types', $class_path) ){
|
||||
if( in_array('Filter_Types', $class_path) && in_array('Category', $class_path) ){
|
||||
$dir = strtolower( $dir );
|
||||
} else {
|
||||
$dir.= strtolower(str_replace('_', '-' , $class_name)).'/';
|
||||
$dir.= strtolower(str_replace('_', '-' , $class_name)).DIRECTORY_SEPARATOR;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,4 +122,9 @@ $Tainacan_Terms = new \Tainacan\Repositories\Terms();
|
|||
global $Tainacan_Logs;
|
||||
$Tainacan_Logs = new \Tainacan\Repositories\Logs();
|
||||
|
||||
global $Tainacan_Exposers;
|
||||
$Tainacan_Exposers = new \Tainacan\Exposers\Exposers();
|
||||
|
||||
$Tainacan_Exposers->register_exposer_type('Tainacan\Exposers\Types\Xml');
|
||||
|
||||
?>
|
|
@ -0,0 +1,58 @@
|
|||
<?php
|
||||
namespace Tainacan\Exposers;
|
||||
|
||||
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
|
||||
|
||||
/**
|
||||
* Load exposers classes
|
||||
*/
|
||||
class Exposers {
|
||||
|
||||
private $types = [];
|
||||
private $mappers = [];
|
||||
|
||||
public function __construct() {
|
||||
add_filter( 'rest_request_after_callbacks', [$this, 'rest_request_after_callbacks'], 10, 3 );
|
||||
add_filter( 'tainacan-rest-response', [$this, 'rest_response'], 10, 2 );
|
||||
}
|
||||
|
||||
/**
|
||||
* register exposers types class on array of types
|
||||
*
|
||||
* @param $class_name string | object The class name or the instance
|
||||
*/
|
||||
public function register_exposer_type( $class_name ){
|
||||
if( is_object( $class_name ) ){
|
||||
$class_name = get_class( $class_name );
|
||||
}
|
||||
|
||||
if(!in_array( $class_name, $this->types)){
|
||||
$this->types[] = $class_name;
|
||||
}
|
||||
}
|
||||
|
||||
public function rest_response($item_arr, $request) {
|
||||
return $item_arr;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param \WP_REST_Response $response
|
||||
* @param \WP_REST_Server $handler
|
||||
* @param \WP_REST_Request $request
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public function rest_request_after_callbacks( $response, $handler, $request ) {
|
||||
if($request->get_method() == 'GET' && substr($request->get_route(), 0, strlen('/tainacan/v2')) == '/tainacan/v2') {
|
||||
$body = json_decode( $request->get_body(), true );
|
||||
if(is_array($body) && array_key_exists('exposer-type', $body) &&
|
||||
in_array('Tainacan\Exposers\Types\\'.sanitize_text_field($body['exposer-type']), $this->types) ) {
|
||||
$type = '\Tainacan\Exposers\Types\\'.sanitize_text_field($body['exposer-type']);
|
||||
$exposer = new $type;
|
||||
return $exposer->rest_request_after_callbacks($response, $handler, $request);
|
||||
}
|
||||
}
|
||||
// default JSON response
|
||||
return $response;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Exposers\Types;
|
||||
|
||||
abstract class Type {
|
||||
/**
|
||||
* @param \WP_REST_Response $response
|
||||
* @param \WP_REST_Server $handler
|
||||
* @param \WP_REST_Request $request
|
||||
* @return \WP_REST_Response
|
||||
*/
|
||||
public abstract function rest_request_after_callbacks( $response, $handler, $request );
|
||||
}
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Exposers\Types;
|
||||
|
||||
class Xml extends Type {
|
||||
/**
|
||||
*
|
||||
* {@inheritDoc}
|
||||
* @see \Tainacan\Exposers\Types\Type::rest_request_after_callbacks()
|
||||
*/
|
||||
public function rest_request_after_callbacks( $response, $handler, $request ) {
|
||||
$response->set_headers( ['Content-Type: application/xml; charset=' . get_option( 'blog_charset' )] );
|
||||
$xml = new \SimpleXMLElement('<?xml version="1.0"?><data></data>');
|
||||
$xml = $this->array_to_xml($response->get_data(), $xml);
|
||||
$response->set_data($xml->asXml());
|
||||
return $response;
|
||||
}
|
||||
|
||||
protected function array_to_xml( $data, $xml_data ) {
|
||||
foreach( $data as $key => $value ) {
|
||||
if( is_numeric($key) ){
|
||||
$key = 'item'.$key; //dealing with <0/>..<n/> issues
|
||||
}
|
||||
if( is_array($value) ) {
|
||||
$subnode = $xml_data->addChild($key);
|
||||
$this->array_to_xml($value, $subnode);
|
||||
} else {
|
||||
$xml_data->addChild("$key",htmlspecialchars("$value"));
|
||||
}
|
||||
}
|
||||
return $xml_data;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,94 @@
|
|||
<?php
|
||||
|
||||
namespace Tainacan\Tests;
|
||||
|
||||
/**
|
||||
* @group api_exposers
|
||||
*/
|
||||
class TAINACAN_REST_Exposers extends TAINACAN_UnitApiTestCase {
|
||||
protected $item;
|
||||
protected $collection;
|
||||
protected $field;
|
||||
|
||||
protected function create_meta_requirements() {
|
||||
$collection = $this->tainacan_entity_factory->create_entity(
|
||||
'collection',
|
||||
array(
|
||||
'name' => 'testeItemExpose',
|
||||
'description' => 'No description',
|
||||
),
|
||||
true,
|
||||
true
|
||||
);
|
||||
|
||||
$type = $this->tainacan_field_factory->create_field('text');
|
||||
|
||||
$field = $this->tainacan_entity_factory->create_entity(
|
||||
'field',
|
||||
array(
|
||||
'name' => 'teste_Expose',
|
||||
'description' => 'descricao',
|
||||
'collection' => $collection,
|
||||
'field_type' => $type,
|
||||
'accept_suggestion' => true
|
||||
),
|
||||
true,
|
||||
true
|
||||
);
|
||||
|
||||
$item = $this->tainacan_entity_factory->create_entity(
|
||||
'item',
|
||||
array(
|
||||
'title' => 'item_teste_Expose',
|
||||
'description' => 'adasdasdsa',
|
||||
'collection' => $collection
|
||||
),
|
||||
true,
|
||||
true
|
||||
);
|
||||
$this->collection = $collection;
|
||||
$this->item = $item;
|
||||
$this->field = $field;
|
||||
return ['collection' => $collection, 'item' => $item, 'field' => $field];
|
||||
}
|
||||
|
||||
public function test_exposers() {
|
||||
$this->create_meta_requirements();
|
||||
global $Tainacan_Fields, $Tainacan_Item_Metadata;
|
||||
|
||||
extract($this->create_meta_requirements());
|
||||
|
||||
$item__metadata_json = json_encode([
|
||||
'values' => 'TestValues_exposers',
|
||||
]);
|
||||
|
||||
$request = new \WP_REST_Request('POST', $this->namespace . '/item/' . $item->get_id() . '/metadata/' . $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($item->get_id() , $data['item']['id']);
|
||||
$this->assertEquals('TestValues_exposers', $data['value']);
|
||||
|
||||
$item_exposer_json = json_encode([
|
||||
'exposer-type' => 'Xml',
|
||||
]);
|
||||
|
||||
$request = new \WP_REST_Request('GET', $this->namespace . '/item/' . $item->get_id() . '/metadata/'. $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->assertInstanceOf('SimpleXMLElement', @simplexml_load_string($data));
|
||||
|
||||
$xml = simplexml_load_string($data);
|
||||
$xml->asXML('/tmp/1.xml');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
Loading…
Reference in New Issue