|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace App\Controller\Article; |
|
6
|
|
|
|
|
7
|
|
|
use App\Repository\ArticleRepository; |
|
8
|
|
|
use FOS\RestBundle\Controller\Annotations\QueryParam; |
|
9
|
|
|
use FOS\RestBundle\Request\ParamFetcher; |
|
10
|
|
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController; |
|
11
|
|
|
use Symfony\Component\Routing\Annotation\Route; |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @Route("/api/articles", methods={"GET"}, name="api_articles_list") |
|
15
|
|
|
* |
|
16
|
|
|
* @QueryParam(name="tag", requirements="[A-Za-z]+", nullable=true) |
|
17
|
|
|
* @QueryParam(name="author", requirements="[A-Za-z0-9]+", nullable=true) |
|
18
|
|
|
* @QueryParam(name="favorited", requirements="[A-Za-z0-9]+", nullable=true) |
|
19
|
|
|
* @QueryParam(name="limit", requirements="\d+", default="20") |
|
20
|
|
|
* @QueryParam(name="offset", requirements="\d+", default="0") |
|
21
|
|
|
*/ |
|
22
|
|
|
final class GetArticlesListController extends AbstractController |
|
23
|
|
|
{ |
|
24
|
|
|
private ArticleRepository $articleRepository; |
|
25
|
|
|
|
|
26
|
6 |
|
public function __construct(ArticleRepository $repository) |
|
27
|
|
|
{ |
|
28
|
6 |
|
$this->articleRepository = $repository; |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
6 |
|
public function __invoke(ParamFetcher $paramFetcher): array |
|
32
|
|
|
{ |
|
33
|
6 |
|
$articlesCount = $this->articleRepository->getArticlesListCount( |
|
34
|
6 |
|
$paramFetcher->get('tag'), |
|
35
|
6 |
|
$paramFetcher->get('author'), |
|
36
|
6 |
|
$paramFetcher->get('favorited') |
|
37
|
|
|
); |
|
38
|
|
|
|
|
39
|
6 |
|
$articles = $this->articleRepository->getArticlesList( |
|
40
|
6 |
|
(int) $paramFetcher->get('offset'), |
|
41
|
6 |
|
(int) $paramFetcher->get('limit'), |
|
42
|
6 |
|
$paramFetcher->get('tag'), |
|
43
|
6 |
|
$paramFetcher->get('author'), |
|
44
|
6 |
|
$paramFetcher->get('favorited') |
|
45
|
|
|
); |
|
46
|
|
|
|
|
47
|
|
|
return [ |
|
48
|
6 |
|
'articlesCount' => $articlesCount, |
|
49
|
|
|
'articles' => $articles, |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|