Skip to content

Commit 368af16

Browse files
committed
minor #1481 merge branch 1.8 into 2.0 (Ener-Getick, xabbuh)
This PR was merged into the 2.0 branch. Discussion ---------- merge branch 1.8 into 2.0 Commits ------- bb9d4de Merge branch '1.8' into 2.0 6608121 feature #1460 Backport #1453 (Ener-Getick, xabbuh) e230461 deprecate the violation formatter 55fafa4 Replace the ViolationFormatter by a new exception
2 parents 46baca3 + bb9d4de commit 368af16

File tree

7 files changed

+60
-34
lines changed

7 files changed

+60
-34
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@ CHANGELOG
44
1.8.0
55
-----
66

7+
* added a new `InvalidParameterException` as a specialization of the `BadRequestHttpException`
8+
9+
* deprecated the `FOS\RestBundle\Util\ViolationFormatter` class and the
10+
`FOS\RestBundle\Util\ViolationFormatterInterface`
11+
12+
* deprecated the `ViolationFormatterInterface` argument of the `ParamFetcher` class constructor
13+
714
* deprecated the `RedirectView` and `RouteRedirectView` classes, use `View::createRedirect()` and
815
`View::createRouteRedirect()` instead
916

Exception/InvalidParameterException.php

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,33 +20,53 @@ class InvalidParameterException extends BadRequestHttpException
2020
private $parameter;
2121
private $violations;
2222

23-
public function __construct(ParamInterface $parameter, ConstraintViolationListInterface $violations)
23+
public function getParameter()
2424
{
25-
$this->parameter = $parameter;
26-
$this->violations = $violations;
25+
return $this->parameter;
26+
}
2727

28+
public function getViolations()
29+
{
30+
return $this->violations;
31+
}
32+
33+
public static function withViolations(ParamInterface $parameter, ConstraintViolationListInterface $violations)
34+
{
2835
$message = '';
36+
2937
foreach ($violations as $key => $violation) {
3038
if ($key > 0) {
3139
$message .= "\n";
3240
}
41+
3342
$message .= sprintf(
3443
'Parameter "%s" of value "%s" violated a constraint "%s"',
3544
$parameter->getName(),
3645
$violation->getInvalidValue(),
3746
$violation->getMessage()
3847
);
3948
}
40-
parent::__construct($message);
41-
}
4249

43-
public function getParameter()
44-
{
45-
return $this->parameter;
50+
return self::withViolationsAndMessage($parameter, $violations, $message);
4651
}
4752

48-
public function getViolations()
53+
/**
54+
* Do not use this method. It will be removed in 2.0.
55+
*
56+
* @param ParamInterface $parameter
57+
* @param ConstraintViolationListInterface $violations
58+
* @param string $message
59+
*
60+
* @return self
61+
*
62+
* @internal
63+
*/
64+
public static function withViolationsAndMessage(ParamInterface $parameter, ConstraintViolationListInterface $violations, $message)
4965
{
50-
return $this->violations;
66+
$exception = new self($message);
67+
$exception->parameter = $parameter;
68+
$exception->violations = $violations;
69+
70+
return $exception;
5171
}
5272
}

Request/ParamFetcher.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ protected function cleanParamWithRequirements(ParamInterface $param, $paramValue
159159

160160
if (0 < count($errors)) {
161161
if ($strict) {
162-
throw new InvalidParameterException($param, $errors);
162+
throw InvalidParameterException::withViolations($param, $errors);
163163
}
164164

165165
return null === $default ? '' : $default;

Tests/Fixtures/Constraints/FooConstraint.php

Lines changed: 0 additions & 22 deletions
This file was deleted.

Tests/Request/ParamFetcherTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ public function testValidationException()
252252
}
253253

254254
/**
255-
* @expectedException Symfony\Component\HttpKernel\Exception\BadRequestHttpException
255+
* @expectedException \FOS\RestBundle\Exception\InvalidParameterException
256256
* @expectedMessage expected exception.
257257
*/
258258
public function testValidationErrorsInStrictMode()

UPGRADING-2.0.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
Upgrading From 1.x To 2.0
2+
=========================
3+
4+
* The `RedirectView` and `RouteRedirect` view classes were removed. Use `View::createRedirect()`
5+
and `View::createRouteRedirect()` instead.
6+
7+
**Note**: the default status code for a route redirect has changed from HTTP_CREATED (201) to
8+
HTTP_FOUND (302).
9+
10+
* The `FOS\RestBundle\Util\ViolationFormatter` class and the `FOS\RestBundle\Util\ViolationFormatterInterface`
11+
were removed. Catch specialized exception classes instead of checking specific exception messages.
12+
13+
* The `ViolationFormatterInterface` argument of the constructor of the `ParamFetcher` class was
14+
removed.

UPGRADING.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,13 @@ This document will be updated to list important BC breaks and behavioral changes
4343

4444
### upgrading to 1.8 (unreleased)
4545

46+
* The `FOS\RestBundle\Util\ViolationFormatter` class and the `FOS\RestBundle\Util\ViolationFormatterInterface`
47+
were deprecated. Catch specialized exception classes instead of checking specific exception
48+
messages.
49+
50+
* The `ViolationFormatterInterface` argument of the constructor of the `ParamFetcher` class is
51+
deprecated and will be removed in 2.0.
52+
4653
* The `Param` class was deprecated in favor of the new `AbstractParam` and `AbstractScalarParam`
4754
classes and the `ParamInterface`.
4855

0 commit comments

Comments
 (0)