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 FOS\RestBundle\Validator\Constraints\Regex; |
15
|
|
|
use Symfony\Component\Validator\Constraint; |
16
|
|
|
use Symfony\Component\Validator\Constraints\NotBlank; |
17
|
|
|
use Symfony\Component\Validator\Constraints\All; |
18
|
|
|
use Symfony\Component\Validator\Constraints\NotNull; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* {@inheritdoc} |
22
|
|
|
* |
23
|
|
|
* @author Ener-Getick <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
abstract class AbstractScalarParam extends AbstractParam |
26
|
|
|
{ |
27
|
|
|
/** @var mixed */ |
28
|
|
|
public $requirements = null; |
29
|
|
|
|
30
|
|
|
/** @var bool */ |
31
|
|
|
public $map = false; |
32
|
|
|
|
33
|
|
|
/** @var bool */ |
34
|
|
|
public $allowBlank = true; |
35
|
|
|
|
36
|
|
|
/** {@inheritdoc} */ |
37
|
29 |
|
public function getConstraints() |
38
|
|
|
{ |
39
|
29 |
|
$constraints = parent::getConstraints(); |
40
|
|
|
|
41
|
29 |
|
if ($this->requirements instanceof Constraint) { |
42
|
8 |
|
$constraints[] = $this->requirements; |
43
|
24 |
|
} elseif (is_scalar($this->requirements)) { |
44
|
7 |
|
$constraints[] = new Regex(array( |
45
|
7 |
|
'pattern' => '#^(?:'.$this->requirements.')$#xsu', |
46
|
7 |
|
'message' => sprintf( |
47
|
7 |
|
'Parameter \'%s\' value, does not match requirements \'%s\'', |
48
|
7 |
|
$this->getName(), |
49
|
7 |
|
$this->requirements |
50
|
|
|
), |
51
|
|
|
)); |
52
|
18 |
|
} elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) { |
53
|
1 |
|
$constraints[] = new Regex(array( |
54
|
1 |
|
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu', |
55
|
1 |
|
'message' => $this->requirements['error_message'], |
56
|
|
|
)); |
57
|
17 |
|
} elseif (is_array($this->requirements)) { |
58
|
1 |
|
foreach ($this->requirements as $index => $requirement) { |
59
|
1 |
|
if ($requirement instanceof Constraint) { |
60
|
1 |
|
$constraints[] = $requirement; |
61
|
|
|
} else { |
62
|
|
|
throw new \TypeError(sprintf('Expected the requirements to be an array of %s instances but got %s at position %d.', Constraint::class, is_object($requirement) ? get_class($requirement) : gettype($requirement), $index)); |
|
|
|
|
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
29 |
|
if (false === $this->allowBlank) { |
68
|
2 |
|
$notBlank = new NotBlank(); |
69
|
2 |
|
if (property_exists(NotBlank::class, 'allowNull')) { |
70
|
2 |
|
$notBlank->allowNull = $this->nullable; |
71
|
|
|
} |
72
|
2 |
|
$constraints[] = $notBlank; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
// If the user wants to map the value, apply all constraints to every |
76
|
|
|
// value of the map |
77
|
29 |
|
if ($this->map) { |
78
|
|
|
$constraints = array( |
79
|
5 |
|
new All(array('constraints' => $constraints)), |
80
|
|
|
); |
81
|
5 |
|
if (false === $this->nullable) { |
82
|
4 |
|
$constraints[] = new NotNull(); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
29 |
|
return $constraints; |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.