Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 14 additions & 11 deletions Controller/Annotations/AbstractScalarParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ public function getConstraints()
if ($this->requirements instanceof Constraint) {
$constraints[] = $this->requirements;
} elseif (is_scalar($this->requirements)) {
$constraints[] = new Regex([
'pattern' => '#^(?:'.$this->requirements.')$#xsu',
'message' => sprintf(
$constraints[] = new Regex(
'#^(?:'.$this->requirements.')$#xsu',
sprintf(
'Parameter \'%s\' value, does not match requirements \'%s\'',
$this->getName(),
$this->requirements
),
]);
);
} elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
$constraints[] = new Regex([
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
'message' => $this->requirements['error_message'],
]);
$constraints[] = new Regex(
'#^(?:'.$this->requirements['rule'].')$#xsu',
$this->requirements['error_message'],
);
} elseif (is_array($this->requirements)) {
foreach ($this->requirements as $index => $requirement) {
if ($requirement instanceof Constraint) {
Expand All @@ -75,9 +75,12 @@ public function getConstraints()
// If the user wants to map the value, apply all constraints to every
// value of the map
if ($this->map) {
$constraints = [
new All(['constraints' => $constraints]),
];
if ([] !== $constraints) {
$constraints = [
new All($constraints),
];
}

if (false === $this->nullable) {
$constraints[] = new NotNull();
}
Expand Down
10 changes: 7 additions & 3 deletions Controller/Annotations/FileParam.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,19 @@ public function getConstraints()

$options = is_array($this->requirements) ? $this->requirements : [];
if ($this->image) {
$constraints[] = new Image($options);
$constraint = new Image();
} else {
$constraints[] = new File($options);
$constraint = new File();
}
foreach ($options as $name => $value) {
$constraint->$name = $value;
}
$constraints[] = $constraint;

// If the user wants to map the value
if ($this->map) {
$constraints = [
new All(['constraints' => $constraints]),
new All($constraints),
];
}

Expand Down
4 changes: 1 addition & 3 deletions Tests/Controller/Annotations/AbstractScalarParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,6 @@ public function testArrayWithNoConstraintsDoesNotCreateInvalidConstraint()
{
$this->param->nullable = true;
$this->param->map = true;
$this->assertEquals([new All([
'constraints' => [],
])], $this->param->getConstraints());
$this->assertEquals([], $this->param->getConstraints());
}
}
16 changes: 8 additions & 8 deletions Tests/Controller/Annotations/FileParamTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,40 +75,40 @@ public function testComplexRequirements()
public function testFileRequirements()
{
$this->param->nullable = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'application/json'];
$this->param->requirements = ['mimeTypes' => 'application/json'];
$this->assertEquals([
new File($requirements),
new File(null, null, null, 'application/json'),
], $this->param->getConstraints());
}

public function testImageRequirements()
{
$this->param->image = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
$this->param->requirements = ['mimeTypes' => ['image/*']];
$this->assertEquals([
new NotNull(),
new Image($requirements),
new Image(null, null, null, ['image/*']),
], $this->param->getConstraints());
}

public function testImageConstraintsTransformWhenParamIsAnArray()
{
$this->param->image = true;
$this->param->map = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'image/gif'];
$this->param->requirements = ['mimeTypes' => ['image/*']];
$this->assertEquals([new All([
new NotNull(),
new Image($requirements),
new Image(null, null, null, ['image/*']),
])], $this->param->getConstraints());
}

public function testFileConstraintsWhenParamIsAnArray()
{
$this->param->map = true;
$this->param->requirements = $requirements = ['mimeTypes' => 'application/pdf'];
$this->param->requirements = ['mimeTypes' => 'application/pdf'];
$this->assertEquals([new All([
new NotNull(),
new File($requirements),
new File(null, null, null, 'application/pdf'),
])], $this->param->getConstraints());
}
}
Loading