|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace XHGui\Options; |
|
4
|
|
|
|
|
5
|
|
|
use Symfony\Component\OptionsResolver\Options; |
|
6
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
7
|
|
|
use XHGui\Searcher\SearcherInterface; |
|
8
|
|
|
|
|
9
|
|
|
class SearchOptions extends OptionsConfigurator |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* Options for SearchInterface::getAll |
|
13
|
|
|
* |
|
14
|
|
|
* - sort: an array of search criteria (TODO meta.SERVER.REQUEST_TIME => -1 ????) |
|
15
|
|
|
* - direction: an string, either 'desc' or 'asc' |
|
16
|
|
|
* - page: an integer, the page to display (e.g. 3) |
|
17
|
|
|
* - perPage: an integer, how many profiles to display per page (e.g. 25) |
|
18
|
|
|
* - conditions: an array of criteria to match |
|
19
|
|
|
* - projection: an array or bool |
|
20
|
|
|
*/ |
|
21
|
|
|
protected function configureOptions(OptionsResolver $resolver) |
|
22
|
|
|
{ |
|
23
|
|
|
// NOTE: the null values is trickery to set default values via null value |
|
24
|
|
|
$defaults = [ |
|
25
|
|
|
'sort' => null, |
|
26
|
|
|
'direction' => SearcherInterface::DEFAULT_DIRECTION, |
|
27
|
|
|
'page' => SearcherInterface::DEFAULT_PAGE, |
|
28
|
|
|
'perPage' => SearcherInterface::DEFAULT_PER_PAGE, |
|
29
|
|
|
'conditions' => [], |
|
30
|
|
|
'projection' => false, |
|
31
|
|
|
]; |
|
32
|
|
|
$resolver->setDefaults($defaults); |
|
33
|
|
|
$resolver->setRequired(['sort', 'direction', 'page', 'perPage']); |
|
34
|
|
|
|
|
35
|
|
|
$resolver->setAllowedTypes('sort', ['null', 'string']); |
|
36
|
|
|
$resolver->setAllowedTypes('direction', ['null', 'string']); |
|
37
|
|
|
$resolver->setAllowedTypes('page', 'int'); |
|
38
|
|
|
$resolver->setAllowedTypes('perPage', ['null', 'int']); |
|
39
|
|
|
$resolver->setAllowedTypes('conditions', 'array'); |
|
40
|
|
|
$resolver->setAllowedTypes('projection', ['bool', 'array']); |
|
41
|
|
|
|
|
42
|
|
|
$resolver->setAllowedValues('direction', [null, 'asc', 'desc']); |
|
43
|
|
|
$resolver->setAllowedValues('sort', [null, 'time', 'wt', 'cpu', 'mu', 'pmu']); |
|
44
|
|
|
|
|
45
|
|
|
$resolver->setNormalizer('direction', function (Options $options, $value) use ($defaults) { |
|
46
|
|
|
if (!$value) { |
|
47
|
|
|
return $defaults['direction']; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
return $value; |
|
51
|
|
|
}); |
|
52
|
|
|
$resolver->setNormalizer('perPage', function (Options $options, $value) use ($defaults) { |
|
53
|
|
|
if (!$value) { |
|
54
|
|
|
return $defaults['perPage']; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
return (int)$value; |
|
58
|
|
|
}); |
|
59
|
|
|
$resolver->setNormalizer('page', function (Options $options, $value) use ($defaults) { |
|
60
|
|
|
if (!$value || $value < 1) { |
|
61
|
|
|
return $defaults['page']; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return (int)$value; |
|
65
|
|
|
}); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|