1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Knp\RadBundle\Resource\Resolver\OptionsBased; |
4
|
|
|
|
5
|
|
|
use Knp\RadBundle\HttpFoundation\RequestManipulator; |
6
|
|
|
use Symfony\Component\HttpFoundation\Request; |
7
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
8
|
|
|
|
9
|
|
|
class ArgumentResolver |
10
|
|
|
{ |
11
|
|
|
private $requestManipulator; |
12
|
|
|
private $optionsResolver; |
13
|
|
|
|
14
|
|
|
public function __construct(RequestManipulator $requestManipulator = null) |
15
|
|
|
{ |
16
|
|
|
$this->requestManipulator = $requestManipulator ?: new RequestManipulator(); |
17
|
|
|
|
18
|
|
|
$this->optionsResolver = new OptionsResolver(); |
19
|
|
|
$this->optionsResolver->setOptional(array('name', 'value')); |
|
|
|
|
20
|
|
|
$this->optionsResolver->setAllowedTypes(array('name' => 'string', 'value' => 'string')); |
|
|
|
|
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
public function resolveArgument(Request $request, array $options) |
24
|
|
|
{ |
25
|
|
|
$options = $this->optionsResolver->resolve($options); |
26
|
|
|
|
27
|
|
|
$hasName = array_key_exists('name', $options); |
28
|
|
|
$hasValue = array_key_exists('value', $options); |
29
|
|
|
|
30
|
|
|
if ($hasName && $hasValue) { |
31
|
|
|
throw new \InvalidArgumentException( |
32
|
|
|
'You must pass either a `name` or a `value` option, but not both.' |
33
|
|
|
); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
if ($hasName) { |
37
|
|
|
return $this->resolveName($request, $options['name']); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
if ($hasValue) { |
41
|
|
|
return $options['value']; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
throw new \InvalidArgumentException( |
45
|
|
|
'You must path either a `name` or a `value` option.' |
46
|
|
|
); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function resolveName(Request $request, $name) |
50
|
|
|
{ |
51
|
|
|
return $this->requestManipulator->getAttribute($request, $name); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
This method has been deprecated. The supplier of the class has supplied an explanatory message.
The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.