Skip to content

Commit 0c87f23

Browse files
committed
Merge pull request #1463 from xabbuh/deprecate-param
deprecate the Param class
2 parents ea7a0bb + f57c5a2 commit 0c87f23

File tree

7 files changed

+249
-1
lines changed

7 files changed

+249
-1
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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\Validator\Constraints;
15+
16+
/**
17+
* {@inheritdoc}
18+
*
19+
* @author Jordi Boggiano <[email protected]>
20+
* @author Boris Guéry <[email protected]>
21+
* @author Ener-Getick <[email protected]>
22+
*/
23+
abstract class AbstractParam implements ParamInterface
24+
{
25+
/** @var string */
26+
public $name;
27+
/** @var string */
28+
public $key;
29+
/** @var mixed */
30+
public $default;
31+
/** @var string */
32+
public $description;
33+
/** @var bool */
34+
public $strict = false;
35+
/** @var bool */
36+
public $nullable = false;
37+
/** @var array */
38+
public $incompatibles = array();
39+
40+
/** {@inheritdoc} */
41+
public function getName()
42+
{
43+
return $this->name;
44+
}
45+
46+
/** {@inheritdoc} */
47+
public function getDefault()
48+
{
49+
return $this->default;
50+
}
51+
52+
/** {@inheritdoc} */
53+
public function getDescription()
54+
{
55+
return $this->description;
56+
}
57+
58+
/** {@inheritdoc} */
59+
public function getIncompatibilities()
60+
{
61+
return $this->incompatibles;
62+
}
63+
64+
/** {@inheritdoc} */
65+
public function getConstraints()
66+
{
67+
$constraints = array();
68+
if (!$this->nullable) {
69+
$constraints[] = new Constraints\NotNull();
70+
}
71+
72+
return $constraints;
73+
}
74+
75+
/** {@inheritdoc} */
76+
public function isStrict()
77+
{
78+
return $this->strict;
79+
}
80+
81+
/**
82+
* @return string
83+
*/
84+
protected function getKey()
85+
{
86+
return $this->key ?: $this->name;
87+
}
88+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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\Validator\Constraint;
15+
use Symfony\Component\Validator\Constraints\NotBlank;
16+
use Symfony\Component\Validator\Constraints\All;
17+
use Symfony\Component\Validator\Constraints\Regex;
18+
19+
/**
20+
* {@inheritdoc}
21+
*
22+
* @author Ener-Getick <[email protected]>
23+
*/
24+
abstract class AbstractScalarParam extends AbstractParam
25+
{
26+
/** @var mixed */
27+
public $requirements = null;
28+
/** @var bool */
29+
public $map = false;
30+
/** @var bool */
31+
public $allowBlank = true;
32+
33+
/** {@inheritdoc} */
34+
public function getConstraints()
35+
{
36+
$constraints = parent::getConstraints();
37+
38+
if ($this->requirements instanceof Constraint) {
39+
$constraints[] = $this->requirements;
40+
} elseif (is_scalar($this->requirements)) {
41+
$constraints[] = new Regex(array(
42+
'pattern' => '#^(?:'.$this->requirements.')$#xsu',
43+
'message' => sprintf(
44+
'Parameter \'%s\' value, does not match requirements \'%s\'',
45+
$this->getName(),
46+
$this->requirements
47+
),
48+
));
49+
} elseif (is_array($this->requirements) && isset($this->requirements['rule']) && $this->requirements['error_message']) {
50+
$constraints[] = new Regex(array(
51+
'pattern' => '#^(?:'.$this->requirements['rule'].')$#xsu',
52+
'message' => $this->requirements['error_message'],
53+
));
54+
}
55+
56+
if (false === $this->allowBlank) {
57+
$constraints[] = new NotBlank();
58+
}
59+
60+
// If the user wants to map the value
61+
if ($this->map) {
62+
$constraints = array(
63+
new All($constraints),
64+
);
65+
}
66+
67+
return $constraints;
68+
}
69+
}

Controller/Annotations/Param.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,10 @@
1616
*
1717
* @author Jordi Boggiano <[email protected]>
1818
* @author Boris Guéry <[email protected]>
19+
*
20+
* @deprecated since version 1.8, to be removed in 2.0. Use the AbstractScalarParam class instead.
1921
*/
20-
abstract class Param
22+
abstract class Param extends AbstractScalarParam
2123
{
2224
/** @var string */
2325
public $name;
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
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+
17+
/**
18+
* Represents a parameter that can be present in the request attributes.
19+
*
20+
* @author Ener-Getick <[email protected]>
21+
*/
22+
interface ParamInterface
23+
{
24+
/**
25+
* Get param name.
26+
*
27+
* @return string
28+
*/
29+
public function getName();
30+
31+
/**
32+
* @return mixed
33+
*/
34+
public function getDefault();
35+
36+
/**
37+
* @return string
38+
*/
39+
public function getDescription();
40+
41+
/**
42+
* Get incompatibles parameters.
43+
*
44+
* @return array
45+
*/
46+
public function getIncompatibilities();
47+
48+
/**
49+
* @return Constraint[]
50+
*/
51+
public function getConstraints();
52+
53+
/**
54+
* @return bool
55+
*/
56+
public function isStrict();
57+
58+
/**
59+
* Get param value in function of the current request.
60+
*
61+
* @param Request $request
62+
* @param mixed $default value
63+
*
64+
* @return mixed
65+
*/
66+
public function getValue(Request $request, $default);
67+
}

Controller/Annotations/QueryParam.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\RestBundle\Controller\Annotations;
1313

14+
use Symfony\Component\HttpFoundation\Request;
15+
1416
/**
1517
* Represents a parameter that must be present in GET data.
1618
*
@@ -21,4 +23,11 @@
2123
*/
2224
class QueryParam extends Param
2325
{
26+
/**
27+
* {@inheritdoc}
28+
*/
29+
public function getValue(Request $request, $default = null)
30+
{
31+
return $request->query->get($this->getKey(), $default);
32+
}
2433
}

Controller/Annotations/RequestParam.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace FOS\RestBundle\Controller\Annotations;
1313

14+
use Symfony\Component\HttpFoundation\Request;
15+
1416
/**
1517
* Represents a parameter that must be present in POST data.
1618
*
@@ -24,4 +26,12 @@ class RequestParam extends Param
2426
{
2527
/** @var bool */
2628
public $strict = true;
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getValue(Request $request, $default = null)
34+
{
35+
return $request->request->get($this->getKey(), $default);
36+
}
2737
}

UPGRADING.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ This document will be updated to list important BC breaks and behavioral changes
1111

1212
### upgrading to 1.8 (unreleased)
1313

14+
* The `Param` class was deprecated in favor of the new `AbstractParam` and `AbstractScalarParam`
15+
classes and the `ParamInterface`.
16+
1417
* The `RedirectView` and `RouteRedirect` view classes are deprecated. Use `View::createRedirect()`
1518
and `View::createRouteRedirect()` instead.
1619

0 commit comments

Comments
 (0)