|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\BreakDown; |
|
4
|
|
|
|
|
5
|
|
|
use BenTools\OpenCubes\Component\BreakDown\Model\Group; |
|
6
|
|
|
use BenTools\OpenCubes\Component\Pager\PagerUriManager; |
|
7
|
|
|
use BenTools\OpenCubes\Component\Pager\PagerUriManagerInterface; |
|
8
|
|
|
use BenTools\OpenCubes\Component\Sort\SortUriManager; |
|
9
|
|
|
use BenTools\OpenCubes\Component\Sort\SortUriManagerInterface; |
|
10
|
|
|
use BenTools\OpenCubes\OptionsTrait; |
|
11
|
|
|
use Psr\Http\Message\UriInterface; |
|
12
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
13
|
|
|
use function BenTools\QueryString\query_string; |
|
14
|
|
|
|
|
15
|
|
|
final class BreakDownUriManager implements BreakDownUriManagerInterface |
|
16
|
|
|
{ |
|
17
|
|
|
use OptionsTrait; |
|
18
|
|
|
|
|
19
|
|
|
public const OPT_BREAKDOWN_QUERY_PARAM = 'query_param'; |
|
20
|
|
|
public const OPT_REMOVE_SORT = 'remove_sort'; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var PagerUriManagerInterface |
|
24
|
|
|
*/ |
|
25
|
|
|
private $pagerUriManager; |
|
26
|
|
|
/** |
|
27
|
|
|
* @var SortUriManagerInterface |
|
28
|
|
|
*/ |
|
29
|
|
|
private $sortUriManager; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* BreakDownUriManager constructor. |
|
33
|
|
|
* @param array $options |
|
34
|
|
|
* @param PagerUriManagerInterface|null $pagerUriManager |
|
35
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
|
36
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
|
37
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException |
|
38
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException |
|
39
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException |
|
40
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct(array $options = [], PagerUriManagerInterface $pagerUriManager = null, SortUriManagerInterface $sortUriManager = null) |
|
43
|
|
|
{ |
|
44
|
|
|
$optionsResolver = new OptionsResolver(); |
|
45
|
|
|
$optionsResolver->setDefaults([ |
|
46
|
|
|
self::OPT_BREAKDOWN_QUERY_PARAM => 'breakdown', |
|
47
|
|
|
self::OPT_REMOVE_SORT => true, |
|
48
|
|
|
]); |
|
49
|
|
|
|
|
50
|
|
|
$this->options = $optionsResolver->resolve($options); |
|
51
|
|
|
$this->pagerUriManager = $pagerUriManager ?? new PagerUriManager(); |
|
52
|
|
|
$this->sortUriManager = $sortUriManager ?? new SortUriManager(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @inheritDoc |
|
57
|
|
|
*/ |
|
58
|
|
|
public function getAppliedGroups(UriInterface $uri): array |
|
59
|
|
|
{ |
|
60
|
|
|
$qs = query_string($uri); |
|
61
|
|
|
|
|
62
|
|
|
$appliedGroups = $qs->getParam($this->getOption(self::OPT_BREAKDOWN_QUERY_PARAM)); |
|
63
|
|
|
|
|
64
|
|
|
if (!is_array($appliedGroups)) { |
|
65
|
|
|
return []; |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
return $appliedGroups; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @inheritDoc |
|
73
|
|
|
*/ |
|
74
|
|
|
public function buildGroupUri(UriInterface $uri, array $groups): UriInterface |
|
75
|
|
|
{ |
|
76
|
|
|
|
|
77
|
|
|
$normalizedGroups = array_map(function ($group) { |
|
78
|
|
|
return $group instanceof Group ? $group->getField() : $group; |
|
79
|
|
|
}, $groups); |
|
80
|
|
|
|
|
81
|
|
|
// Reset to 1st page after breaking down |
|
82
|
|
|
$uri = $this->pagerUriManager->buildPageUri($uri, 1); |
|
83
|
|
|
|
|
84
|
|
|
// Remove sort if required |
|
85
|
|
|
if (true === $this->getOption(self::OPT_REMOVE_SORT)) { |
|
86
|
|
|
$uri = $this->sortUriManager->buildSortUri($uri, []); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
$qs = query_string($uri); |
|
90
|
|
|
$qs = [] === $groups ? $qs->withoutParam($this->getOption(self::OPT_BREAKDOWN_QUERY_PARAM)) : $qs->withParam($this->getOption(self::OPT_BREAKDOWN_QUERY_PARAM), $normalizedGroups); |
|
91
|
|
|
|
|
92
|
|
|
return $uri->withQuery((string) $qs); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|