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