getParams(); $params = $this->checkQueryParams($params); $params['published'] = ArticleModel::PUBLISH_APPROVED; $params['private'] = 0; $params['deleted'] = 0; $sort = $pagerQuery->getSort(); $page = $pagerQuery->getPage(); $limit = $pagerQuery->getLimit(); $articleRepo = new ArticleRepo(); $pager = $articleRepo->paginate($params, $sort, $page, $limit); return $this->handleArticles($pager); } public function handleArticles($pager) { if ($pager->total_items == 0) { return $pager; } $builder = new ArticleListBuilder(); $categories = $builder->getCategories(); $articles = $pager->items->toArray(); $users = $builder->getUsers($articles); $items = []; foreach ($articles as $article) { $article['tags'] = json_decode($article['tags'], true); $category = $categories[$article['category_id']] ?? new \stdClass(); $owner = $users[$article['owner_id']] ?? new \stdClass(); $items[] = [ 'id' => $article['id'], 'title' => $article['title'], 'cover' => $article['cover'], 'summary' => $article['summary'], 'source_type' => $article['source_type'], 'source_url' => $article['source_url'], 'tags' => $article['tags'], 'private' => $article['private'], 'published' => $article['published'], 'closed' => $article['closed'], 'view_count' => $article['view_count'], 'like_count' => $article['like_count'], 'comment_count' => $article['comment_count'], 'favorite_count' => $article['favorite_count'], 'create_time' => $article['create_time'], 'update_time' => $article['update_time'], 'category' => $category, 'owner' => $owner, ]; } $pager->items = $items; return $pager; } protected function checkQueryParams($params) { $validator = new ArticleQueryValidator(); $query = []; if (isset($params['category_id'])) { $query['category_id'] = $validator->checkCategory($params['category_id']); } if (isset($params['tag_id'])) { $query['tag_id'] = $validator->checkTag($params['tag_id']); } if (isset($params['sort'])) { $query['sort'] = $validator->checkSort($params['sort']); } return $query; } }