1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the feedback project. |
5
|
|
|
* |
6
|
|
|
* (c) Florian Moser <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace App\Controller\Base; |
13
|
|
|
|
14
|
|
|
use App\Entity\Answer; |
15
|
|
|
use App\Entity\Event; |
16
|
|
|
use App\Entity\Participant; |
17
|
|
|
use App\Entity\Semester; |
18
|
|
|
use Symfony\Component\HttpFoundation\JsonResponse; |
19
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
20
|
|
|
|
21
|
|
|
class BaseApiController extends BaseDoctrineController |
22
|
|
|
{ |
23
|
|
|
public static function getSubscribedServices() |
24
|
|
|
{ |
25
|
|
|
return parent::getSubscribedServices() + |
26
|
|
|
[ |
27
|
|
|
'serializer' => SerializerInterface::class, |
28
|
|
|
]; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return SerializerInterface |
33
|
|
|
*/ |
34
|
|
|
private function getSerializer() |
35
|
|
|
{ |
36
|
|
|
return $this->get('serializer'); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @param Semester|Semester[] $semester |
41
|
|
|
* |
42
|
|
|
* @return JsonResponse |
43
|
|
|
*/ |
44
|
|
|
protected function returnSemester($semester) |
45
|
|
|
{ |
46
|
|
|
return $this->returnJson($semester, ['name', 'events' => ['id', 'name', 'date']]); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param Event|Event[] $events |
51
|
|
|
* |
52
|
|
|
* @return JsonResponse |
53
|
|
|
*/ |
54
|
|
|
protected function returnEvent($events) |
55
|
|
|
{ |
56
|
|
|
return $this->returnJson($events, ['id', 'name', 'date', 'template', 'categoryWhitelist']); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param Answer[]|Answer $answers |
61
|
|
|
* |
62
|
|
|
* @return JsonResponse |
63
|
|
|
*/ |
64
|
|
|
protected function returnAnswers($answers) |
65
|
|
|
{ |
66
|
|
|
return $this->returnJson($answers, ['questionIndex', 'value']); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param Participant|Participant[] $participants |
71
|
|
|
* |
72
|
|
|
* @return JsonResponse |
73
|
|
|
*/ |
74
|
|
|
protected function returnParticipant($participants) |
75
|
|
|
{ |
76
|
|
|
return $this->returnJson($participants, [ |
77
|
|
|
'timeNeededInMinutes', 'answers' => [ |
78
|
|
|
'questionIndex', 'value', |
79
|
|
|
], |
80
|
|
|
]); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param $content |
85
|
|
|
* @param array $attributes |
86
|
|
|
* |
87
|
|
|
* @return JsonResponse |
88
|
|
|
*/ |
89
|
|
|
private function returnJson($content, $attributes = []) |
90
|
|
|
{ |
91
|
|
|
$addition = \count($attributes) > 0 ? ['attributes' => $attributes] : []; |
92
|
|
|
|
93
|
|
|
return new JsonResponse( |
94
|
|
|
$this->getSerializer()->serialize( |
95
|
|
|
$content, |
96
|
|
|
'json', |
97
|
|
|
['json_encode_options' => JsonResponse::DEFAULT_ENCODING_OPTIONS | JSON_UNESCAPED_UNICODE] + $addition |
98
|
|
|
), |
99
|
|
|
200, |
100
|
|
|
[], |
101
|
|
|
true |
102
|
|
|
); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|