diff options
author | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-14 04:16:31 +0400 |
---|---|---|
committer | Bernhard Posselt <dev@bernhard-posselt.com> | 2014-05-14 04:16:31 +0400 |
commit | acc2df1251a1c1b9ec5ede13bdf46d516dc64b0d (patch) | |
tree | ce8503baa37cc05379dfd43a65e9acf0b41e0d97 /controller/entityapiserializer.php | |
parent | db4c29e89d77955c4930731ade08816f5567fe84 (diff) |
use more flexible serializer that ignores nulls, non entity values and responses
Diffstat (limited to 'controller/entityapiserializer.php')
-rw-r--r-- | controller/entityapiserializer.php | 48 |
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 |