1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace App\Controller\Api; |
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
|
|
|
* ArticlesListController. |
13
|
|
|
* |
14
|
|
|
* @Route("/api/articles", name="api_articles_list") |
15
|
|
|
* @Method("GET") |
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
|
|
|
class ArticlesListController |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var ArticleRepository |
26
|
|
|
*/ |
27
|
|
|
private $repository; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @param ArticleRepository $repository |
31
|
|
|
*/ |
32
|
1 |
|
public function __construct(ArticleRepository $repository) |
33
|
|
|
{ |
34
|
1 |
|
$this->repository = $repository; |
35
|
1 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ParamFetcher $paramFetcher |
39
|
|
|
* |
40
|
|
|
* @return array |
41
|
|
|
*/ |
42
|
1 |
|
public function __invoke(ParamFetcher $paramFetcher) |
43
|
|
|
{ |
44
|
1 |
|
$articles = $this->repository->getArticles( |
45
|
1 |
|
(int) $paramFetcher->get('offset'), |
46
|
1 |
|
(int) $paramFetcher->get('limit'), |
47
|
1 |
|
$paramFetcher->get('tag'), |
48
|
1 |
|
$paramFetcher->get('author'), |
49
|
1 |
|
$paramFetcher->get('favorited') |
50
|
|
|
); |
51
|
|
|
|
52
|
1 |
|
$articlesCount = count($articles); |
53
|
|
|
|
54
|
|
|
return [ |
55
|
1 |
|
'articles' => $articles, |
56
|
1 |
|
'articlesCount' => $articlesCount, |
57
|
|
|
]; |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|