Limitation of attributes returned from API GET Collections

This commit is contained in:
weryques 2018-01-24 14:08:17 -02:00
parent fe041b17da
commit 49df60f989
3 changed files with 38 additions and 7 deletions

View File

@ -101,6 +101,20 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
return new WP_REST_Response($response, 200);
}
protected function get_only_needed_attributes($collection){
$collection_temp = [
'id' => $collection->get_id(),
'name' => $collection->get_name(),
'description' => $collection->get_description(),
'featured_image' => $collection->get_featured_img(),
'columns' => $collection->get_columns(),
'creation_date' => $collection->get_creation_date(),
'modification_date' => $collection->get_modification_date(),
];
return $collection_temp;
}
/**
*
* Receive a WP_Query or a Collection object and return both in JSON
@ -112,22 +126,25 @@ class TAINACAN_REST_Collections_Controller extends WP_REST_Controller {
*/
public function prepare_item_for_response($item, $request){
if($item instanceof WP_Query){
$collections_as_json = [];
$collections = [];
if ($item->have_posts()) {
while ( $item->have_posts() ) {
$item->the_post();
$collection = new Entities\Collection($item->post);
array_push($collections_as_json, $collection->__toArray());
$collection_resumed = $this->get_only_needed_attributes($collection);
array_push($collections, $collection_resumed);
}
wp_reset_postdata();
}
return $collections_as_json;
return $collections;
}
elseif(!empty($item)){
return $item->__toArray();
return $this->get_only_needed_attributes($item);
}
return $item;

View File

@ -9,7 +9,7 @@ const REPOSITORIES_DIR = __DIR__ . '/repositories/';
const TRAITS_DIR = __DIR__ . '/traits/';
const VENDOR_DIR = __DIR__ . '/../vendor/';
const ENDPOINTS_DIR = __DIR__ . '/../api/endpoints/';
const HELPERS_DIR = __DIR__ . '/../helpers/';
const HELPERS_DIR = __DIR__ . '/../helpers/';
const DIRS = [
CLASSES_DIR,

View File

@ -55,7 +55,7 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
}
public function test_fetch_collections(){
$x = $this->tainacan_entity_factory->create_entity(
$this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'testeApi',
@ -66,6 +66,17 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
true
);
$this->tainacan_entity_factory->create_entity(
'collection',
array(
'name' => 'Other',
'description' => 'adasdasdsa',
'default_order' => 'DESC',
'status' => 'publish'
),
true
);
$request = new \WP_REST_Request( 'GET', $this->namespace . '/collections' );
$response = $this->server->dispatch( $request );
@ -76,8 +87,11 @@ class TAINACAN_REST_Collections_Controller extends TAINACAN_UnitApiTestCase {
//$data is a valid json?
//$this->assertTrue(json_last_error() === JSON_ERROR_NONE);
$one_collection = $data[0];
$other_collection = $data[0];
$one_collection = $data[1];
$this->assertEquals('testeApi', $one_collection['name']);
$this->assertEquals('Other', $other_collection['name']);
}
public function test_delete_or_trash_a_collection(){