From dd2c6efbc9ea85a68515c7a9b059a2987f0ec6ea Mon Sep 17 00:00:00 2001 From: Eldad Fux Date: Wed, 24 Jun 2020 09:05:43 +0300 Subject: [PATCH] Added team dynamic response --- app/controllers/api/teams.php | 5 +++- src/Appwrite/Database/Document.php | 16 +++++++++-- src/Appwrite/Utopia/Response.php | 28 +++++++++++++++---- .../Utopia/Response/Model/BaseList.php | 4 +-- .../Utopia/Response/Model/TeamList.php | 1 + 5 files changed, 43 insertions(+), 11 deletions(-) diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index b8d6d4138..8e91f42db 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -108,7 +108,10 @@ $utopia->get('/v1/teams') ], ]); - $response->json(['sum' => $projectDB->getSum(), 'teams' => $results]); + $response->dynamic(new Document([ + 'sum' => $projectDB->getSum(), + 'teams' => $results + ]), Response::MODEL_TEAM_LIST); } ); diff --git a/src/Appwrite/Database/Document.php b/src/Appwrite/Database/Document.php index a0445702e..9b7b28145 100644 --- a/src/Appwrite/Database/Document.php +++ b/src/Appwrite/Database/Document.php @@ -25,11 +25,11 @@ class Document extends ArrayObject { foreach ($input as $key => &$value) { if (\is_array($value)) { - if (isset($value['$id']) || isset($value['$collection'])) { + if ((isset($value['$id']) || isset($value['$collection'])) && (!$value instanceof self)) { $input[$key] = new self($value); } else { foreach ($value as $childKey => $child) { - if (isset($child['$id']) || isset($child['$collection'])) { + if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) { $value[$childKey] = new self($child); } } @@ -193,6 +193,18 @@ class Document extends ArrayObject return empty($this->getId()); } + /** + * Checks if a document key is set. + * + * @param $key + * + * @return bool + */ + public function isSet($key) + { + return isset($this[$key]); + } + /** * Get Array Copy. * diff --git a/src/Appwrite/Utopia/Response.php b/src/Appwrite/Utopia/Response.php index ded28945d..8650c3533 100644 --- a/src/Appwrite/Utopia/Response.php +++ b/src/Appwrite/Utopia/Response.php @@ -101,28 +101,44 @@ class Response extends UtopiaResponse */ public function dynamic(Document $document, string $model) { - $data = $document->getArrayCopy(); + return $this->json($this->output($document, $model)); + } + + /** + * Generate valid response object from document data + */ + protected function output(Document $document, string $model): array + { + $data = $document; $model = $this->getModel($model); $output = []; foreach($model->getRules() as $key => $rule) { - if(!isset($data[$key])) { + if(!$document->isSet($key)) { if(!is_null($rule['default'])) { - $data[$key] = $rule['default']; + $document->setAttribute($key, $rule['default']); } else { throw new Exception('Missing response key: '.$key); } } - if($rule['array'] && !is_array($data[$key])) { - throw new Exception($key.' must be an array of '.$rule['type'].' types'); + if($rule['array']) { + if(!is_array($data[$key])) { + throw new Exception($key.' must be an array of '.$rule['type'].' types'); + } + + foreach ($data[$key] as &$item) { + if(array_key_exists($rule['type'], $this->models) && $item instanceof Document) { + $item = $this->output($item, $rule['type']); + } + } } $output[$key] = $data[$key]; } - return $this->json($output); + return $output; } /** diff --git a/src/Appwrite/Utopia/Response/Model/BaseList.php b/src/Appwrite/Utopia/Response/Model/BaseList.php index 1e7c8050b..24f0a7648 100644 --- a/src/Appwrite/Utopia/Response/Model/BaseList.php +++ b/src/Appwrite/Utopia/Response/Model/BaseList.php @@ -11,9 +11,9 @@ abstract class BaseList extends Model { $this ->addRule('sum', [ - 'type' => 'intgere', + 'type' => 'integer', 'description' => 'Total sum of items in the list.', - 'example' => '5e5ea5c16897e', + 'example' => '5', ]) ; } diff --git a/src/Appwrite/Utopia/Response/Model/TeamList.php b/src/Appwrite/Utopia/Response/Model/TeamList.php index 2c1951cfa..c96e4cd0d 100644 --- a/src/Appwrite/Utopia/Response/Model/TeamList.php +++ b/src/Appwrite/Utopia/Response/Model/TeamList.php @@ -8,6 +8,7 @@ class TeamList extends BaseList { public function __construct() { + parent::__construct(); $this ->addRule('teams', [ 'type' => Response::MODEL_TEAM,