Method toJSON

All entities inherit the method __toJSON()
This commit is contained in:
weryques 2017-12-01 08:46:03 -02:00
parent 185e0dd5ea
commit 5de5e4a936
2 changed files with 26 additions and 20 deletions

View File

@ -45,17 +45,6 @@ class Collection extends Entity {
return 'Hello, I\'m the Collection Entity';
}
public function __toJSON(){
return json_encode(
[
'id' => $this->get_id(),
'name' => $this->get_name(),
'description' => $this->get_description(),
],
JSON_NUMERIC_CHECK,
JSON_UNESCAPED_UNICODE
);
}
/**
* Register the post type for this collection
*
@ -210,15 +199,15 @@ class Collection extends Entity {
return $this->get_id() ? 'tnc_col_' . $this->get_id() : false;
}
/**
* Get collection metadata.
*
* Returns an array of \Entity\Metadata objects, representing all the metadata of the collection.
*
* @see \Tainacan\Repositories\Metadatas->fetch()
*
* @return array
*/
/**
* Get collection metadata.
*
* Returns an array of \Entity\Metadata objects, representing all the metadata of the collection.
*
* @see \Tainacan\Repositories\Metadatas->fetch()
*
* @return \Tainacan\Repositories\Array
*/
function get_metadata() {
$Tainacan_Metadatas = new \Tainacan\Repositories\Metadatas();
return $Tainacan_Metadatas->fetch_by_collection( $this, [], 'OBJECT' );

View File

@ -197,5 +197,22 @@ class Entity {
$this->set_validated(true);
return true;
}
public function __toJSON(){
global ${$this->repository};
$map = ${$this->repository}->get_map();
$attributes_names = [];
$attributes_values = [];
foreach ($map as $prop => $content){
array_push($attributes_names, $prop);
array_push($attributes_values, $this->get_mapped_property($prop));
}
$entity_as_json = array_combine($attributes_names, $attributes_values);
return json_encode($entity_as_json, JSON_NUMERIC_CHECK, JSON_UNESCAPED_UNICODE);
}
}