summaryrefslogtreecommitdiffstats
path: root/controller/entityapiserializer.php
diff options
context:
space:
mode:
Diffstat (limited to 'controller/entityapiserializer.php')
-rw-r--r--controller/entityapiserializer.php48
1 files changed, 38 insertions, 10 deletions
diff --git a/controller/entityapiserializer.php b/controller/entityapiserializer.php
index 8bd17ba29..b84006750 100644
--- a/controller/entityapiserializer.php
+++ b/controller/entityapiserializer.php
@@ -12,6 +12,9 @@
namespace OCA\News\Controller;
use \OCP\AppFramework\Http\IResponseSerializer;
+use \OCP\AppFramework\Http\Response;
+
+use \OCA\News\Db\IAPI;
class EntityApiSerializer implements IResponseSerializer {
@@ -23,24 +26,49 @@ class EntityApiSerializer implements IResponseSerializer {
/**
- * Wrap a list of entities in an array with $level as index and serialize
- * them using the toAPI method
+ * Call toAPI() method on all entities. Works on
+ * @param mixed $data:
+ * * Entity
+ * * Entity[]
+ * * array('level' => Entity[])
+ * * Response
*/
public function serialize($data) {
- if(!is_array($data)) {
- $data = array($data);
+
+ if($data === null || $data instanceof Response) {
+ return $data;
}
- $response = array(
- $this->level => array()
- );
+ if($data instanceof IAPI) {
+ return array(
+ $this->level => array($data->toAPI())
+ );
+ }
- foreach($data as $entity) {
- $response[$this->level][] = $entity->toAPI();
+ if(is_array($data) && array_key_exists($this->level, $data)) {
+ $data[$this->level] = $this->convert($data[$this->level]);
+ } elseif(is_array($data)) {
+ $data = array(
+ $this->level => $this->convert($data)
+ );
}
- return $response;
+ return $data;
}
+ private function convert($entities) {
+ $converted = array();
+
+ foreach($entities as $entity) {
+ if($entity instanceof IAPI) {
+ $converted[] = $entity->toAPI();
+ } else {
+ $converted[] = $entity;
+ }
+ }
+
+ return $converted;
+ }
+
} \ No newline at end of file