1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> |
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 FOS\RestBundle\Controller\Annotations; |
13
|
|
|
|
14
|
|
|
use Symfony\Component\HttpFoundation\Request; |
15
|
|
|
use Symfony\Component\Validator\Constraint; |
16
|
|
|
use Symfony\Component\Validator\Constraints\All; |
17
|
|
|
use Symfony\Component\Validator\Constraints\File; |
18
|
|
|
use Symfony\Component\Validator\Constraints\Image; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Represents a file that must be present. |
22
|
|
|
* |
23
|
|
|
* @Annotation |
24
|
|
|
* @Target("METHOD") |
25
|
|
|
* |
26
|
|
|
* @author Ener-Getick <[email protected]> |
27
|
|
|
*/ |
28
|
|
|
class FileParam extends AbstractParam |
29
|
|
|
{ |
30
|
|
|
/** @var bool */ |
31
|
|
|
public $strict = true; |
32
|
|
|
|
33
|
|
|
/** @var mixed */ |
34
|
|
|
public $requirements = null; |
35
|
|
|
|
36
|
|
|
/** @var bool */ |
37
|
|
|
public $image = false; |
38
|
|
|
|
39
|
|
|
/** @var bool */ |
40
|
|
|
public $map = false; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* {@inheritdoc} |
44
|
|
|
*/ |
45
|
10 |
|
public function getConstraints() |
46
|
|
|
{ |
47
|
10 |
|
$constraints = parent::getConstraints(); |
48
|
10 |
|
if ($this->requirements instanceof Constraint) { |
49
|
1 |
|
$constraints[] = $this->requirements; |
50
|
|
|
} |
51
|
|
|
|
52
|
10 |
|
$options = is_array($this->requirements) ? $this->requirements : array(); |
53
|
10 |
|
if ($this->image) { |
54
|
4 |
|
$constraints[] = new Image($options); |
55
|
|
|
} else { |
56
|
6 |
|
$constraints[] = new File($options); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
// If the user wants to map the value |
60
|
10 |
|
if ($this->map) { |
61
|
|
|
$constraints = array( |
62
|
5 |
|
new All(array('constraints' => $constraints)), |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
10 |
|
return $constraints; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritdoc} |
71
|
|
|
*/ |
72
|
7 |
|
public function getValue(Request $request, $default = null) |
73
|
|
|
{ |
74
|
7 |
|
return $request->files->get($this->getKey(), $default); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|