From 3cfe5df2a7891607bad481fae90a0e7c71989ac4 Mon Sep 17 00:00:00 2001 From: Pierre Date: Sat, 21 Apr 2018 19:04:58 +0200 Subject: [PATCH] Upgraded API to use only Comments object instead of arrays --- ...sController.php => CommentsController.php} | 4 +- RestControllers/postsController.php | 8 ++++ classes/components/comments.php | 40 +------------------ 3 files changed, 12 insertions(+), 40 deletions(-) rename RestControllers/{commentsController.php => CommentsController.php} (98%) diff --git a/RestControllers/commentsController.php b/RestControllers/CommentsController.php similarity index 98% rename from RestControllers/commentsController.php rename to RestControllers/CommentsController.php index 2ac54f4..7f860b8 100644 --- a/RestControllers/commentsController.php +++ b/RestControllers/CommentsController.php @@ -5,7 +5,7 @@ * @author Pierre HUBERT */ -class commentsController { +class CommentsController { /** * Create a comment @@ -181,7 +181,7 @@ class commentsController { * @param Comment $comment The comment to convert * @return array Informations about the comment */ - private function commentToAPI(Comment $comment) : array { + public static function commentToAPI(Comment $comment) : array { $data = array(); $data["ID"] = $comment->get_id(); diff --git a/RestControllers/postsController.php b/RestControllers/postsController.php index 04764a0..008572b 100644 --- a/RestControllers/postsController.php +++ b/RestControllers/postsController.php @@ -528,6 +528,14 @@ class postsController { //Update visibility level $infos['visibility_level'] = $this::VISIBILITY_LEVELS_API[$infos['visibility_level']]; + //Parse comments if required + if(isset($infos['comments'])){ + if($infos['comments'] != null){ + foreach($infos['comments'] as $num=>$src) + $infos['comments'][$num] = CommentsController::commentToAPI($src); + } + } + //Return the post ready to be shown return $infos; } diff --git a/classes/components/comments.php b/classes/components/comments.php index 993acbb..bffcb98 100644 --- a/classes/components/comments.php +++ b/classes/components/comments.php @@ -46,7 +46,7 @@ class Comments { * @param int $postID The ID of the post * @param bool $load_comments Specify whether the comments should be * loaded or not (default to true) - * @return array The list of comments of the post + * @return array The list of comments of the post (as Comment objects) */ public function get(int $postID, bool $load_comments = true) : array { @@ -61,7 +61,7 @@ class Comments { $comments = array(); foreach($result as $entry){ - $comments[] = $this->parse_comment($entry, $load_comments); + $comments[] = $this->dbToComment($entry, $load_comments); } return $comments; @@ -234,42 +234,6 @@ class Comments { ) > 0; } - /** - * Parse a comment informations - * - * @param array $src Informations from the database - * @param bool $load_likes Specify if the likes have to be loaded or not - * @return array Informations about the comments ready to be sent - */ - private function parse_comment(array $src, bool $load_likes = true): array { - $info = array(); - - $info["ID"] = $src["ID"]; - $info["userID"] = $src["ID_personne"]; - $info["postID"] = $src["ID_texte"]; - $info["time_sent"] = strtotime($src["date_envoi"]); - $info["content"] = $src["commentaire"]; - - //Check for image - if($src["image_commentaire"] != ""){ - $info["img_path"] = str_replace("file:", "", $src["image_commentaire"]); - $info["img_url"] = path_user_data($info["img_path"], false); - - } else { - $info["img_path"] = null; - $info["img_url"] = null; - } - - if($load_likes){ - //Get informations about likes - $info["likes"] = CS::get()->components->likes->count($info["ID"], Likes::LIKE_COMMENT); - $info["userlike"] = user_signed_in() ? CS::get()->components->likes->is_liking(userID, $info["ID"], Likes::LIKE_COMMENT) : false; - } - - return $info; - } - - /** * Turn a comment database entry into a Comment object *