-
Notifications
You must be signed in to change notification settings - Fork 692
Expand file tree
/
Copy pathParamFetcherController.php
More file actions
110 lines (95 loc) · 3.9 KB
/
ParamFetcherController.php
File metadata and controls
110 lines (95 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/*
* This file is part of the FOSRestBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\RestBundle\Tests\Functional\Bundle\TestBundle\Controller;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use FOS\RestBundle\Controller\Annotations\FileParam;
use FOS\RestBundle\Controller\Annotations\QueryParam;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use FOS\RestBundle\Request\ParamFetcherInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Validator\Constraints\IdenticalTo;
class ParamFetcherController extends AbstractFOSRestController
{
/**
* @RequestParam(name="raw", requirements=@IdenticalTo({"foo"="raw", "bar"="foo"}), default="invalid", strict=false)
* @RequestParam(name="map", map=true, requirements=@IdenticalTo({"foo"="map", "foobar"="foo"}), default="%invalid2% %%", strict=false)
* @RequestParam(name="bar", nullable=true, requirements="%bar%\ foo")
* @QueryParam(name="foz", requirements="[a-z]+")
* @QueryParam(name="baz", requirements="[a-z]+", incompatibles={"foz"})
*/
public function paramsAction(ParamFetcherInterface $fetcher): \Symfony\Component\HttpFoundation\JsonResponse
{
return new JsonResponse($fetcher->all());
}
/**
* @QueryParam(name="foo", default="invalid")
* @RequestParam(name="bar", default="%foo%")
*/
public function testAction(Request $request, ParamFetcherInterface $fetcher): \Symfony\Component\HttpFoundation\JsonResponse
{
$paramsBefore = $fetcher->all();
$newRequest = $request->duplicate($request->query->all(), $request->request->all(), [
'_controller' => sprintf('%s::paramsAction', __CLASS__),
]);
$response = $this->container->get('http_kernel')->handle($newRequest, HttpKernelInterface::SUB_REQUEST, false);
$paramsAfter = $fetcher->all(false);
return new JsonResponse([
'before' => $paramsBefore,
'during' => json_decode($response->getContent(), true),
'after' => $paramsAfter,
]);
}
/**
* @FileParam(name="single_file", strict=false, default="noFile")
*/
public function singleFileAction(ParamFetcherInterface $fetcher): \Symfony\Component\HttpFoundation\JsonResponse
{
/** @var UploadedFile $file */
$file = $fetcher->get('single_file');
return new JsonResponse([
'single_file' => $this->printFile($file),
]);
}
/**
* @FileParam(name="array_files", map=true)
*/
public function fileCollectionAction(ParamFetcherInterface $fetcher): \Symfony\Component\HttpFoundation\JsonResponse
{
$files = $fetcher->get('array_files');
return new JsonResponse([
'array_files' => [
$this->printFile($files[0]),
$this->printFile($files[1]),
],
]);
}
/**
* @FileParam(name="array_images", image=true, strict=false, map=true, default="NotAnImage")
*/
public function imageCollectionAction(ParamFetcherInterface $fetcher): \Symfony\Component\HttpFoundation\JsonResponse
{
$files = $fetcher->get('array_images');
return new JsonResponse([
'array_images' => (is_string($files)) // Default message on validation error
? $files
: [
$this->printFile($files[0]),
$this->printFile($files[1]),
],
]);
}
private function printFile($file)
{
return ($file instanceof UploadedFile) ? $file->getClientOriginalName() : $file;
}
}