1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Alchemy\RestBundle\Rest\Request; |
4
|
|
|
|
5
|
|
|
use Alchemy\Rest\Request\SortOptions; |
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
7
|
|
|
|
8
|
|
|
class SortOptionsFactory |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $sortName; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $directionName; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $multisortName; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param string $sortName |
27
|
|
|
* @param string $directionName |
28
|
|
|
* @param string $multisortName |
29
|
|
|
*/ |
30
|
12 |
|
public function __construct($sortName, $directionName, $multisortName) |
31
|
|
|
{ |
32
|
12 |
|
$this->sortName = $sortName; |
33
|
12 |
|
$this->directionName = $directionName; |
34
|
12 |
|
$this->multisortName = $multisortName; |
35
|
12 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $request |
39
|
|
|
* @param array $config |
40
|
|
|
* @return SortRequest |
41
|
|
|
*/ |
42
|
10 |
|
public function create(array $request, array $config) |
43
|
|
|
{ |
44
|
10 |
|
$sortName = isset($config['sort_parameter']) ? $config['sort_parameter'] : $this->sortName; |
45
|
10 |
|
$directionName = isset($config['direction_parameter']) ? $config['direction_parameter'] : $this->directionName; |
46
|
10 |
|
$multisortName = isset($config['multi_sort_parameter']) ? |
47
|
10 |
|
$config['multi_sort_parameter'] : $this->multisortName; |
48
|
|
|
|
49
|
10 |
|
$resolver = $this->configureResolver($request, $sortName, $directionName, $multisortName); |
50
|
10 |
|
$values = $resolver->resolve($request); |
51
|
|
|
|
52
|
10 |
|
return new SortRequest($values[$multisortName], $values[$sortName], $values[$directionName]); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $options |
57
|
|
|
* @param string $sortName |
58
|
|
|
* @param string $directionName |
59
|
|
|
* @param string $multisortName |
60
|
|
|
* @return OptionsResolver |
61
|
|
|
*/ |
62
|
10 |
|
private function configureResolver(array $options, $sortName, $directionName, $multisortName) |
63
|
|
|
{ |
64
|
10 |
|
$optionsResolver = new OptionsResolver(); |
65
|
|
|
|
66
|
|
|
$keys = array( |
67
|
10 |
|
$sortName => null, |
68
|
10 |
|
$directionName => null, |
69
|
|
|
$multisortName => null |
70
|
10 |
|
); |
71
|
|
|
|
72
|
10 |
|
if (method_exists($optionsResolver, 'setDefined')) { |
73
|
10 |
|
$optionsResolver->setDefined(array_merge(array_keys($keys), array_keys($options))); |
74
|
10 |
|
$optionsResolver->setAllowedValues($directionName, array( |
75
|
10 |
|
strtoupper(SortOptions::SORT_ASC), |
76
|
10 |
|
strtolower(SortOptions::SORT_ASC), |
77
|
10 |
|
strtoupper(SortOptions::SORT_DESC), |
78
|
10 |
|
strtolower(SortOptions::SORT_DESC), |
79
|
|
|
null |
80
|
10 |
|
)); |
81
|
10 |
|
} else { |
82
|
|
|
// BC with symfony < 2.6 |
83
|
|
|
$optionsResolver->setOptional(array_merge(array_keys($keys), array_keys($options))); |
|
|
|
|
84
|
|
|
$optionsResolver->setAllowedValues(array( |
|
|
|
|
85
|
|
|
$directionName => array( |
86
|
|
|
strtoupper(SortOptions::SORT_ASC), |
87
|
|
|
strtolower(SortOptions::SORT_ASC), |
88
|
|
|
strtoupper(SortOptions::SORT_DESC), |
89
|
|
|
strtolower(SortOptions::SORT_DESC), |
90
|
|
|
null |
91
|
|
|
) |
92
|
|
|
)); |
93
|
|
|
} |
94
|
10 |
|
$optionsResolver->setDefaults($keys); |
95
|
|
|
|
96
|
10 |
|
return $optionsResolver; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
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.