1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BenTools\OpenCubes\Component\Sort; |
4
|
|
|
|
5
|
|
|
use BenTools\OpenCubes\Component\ComponentFactoryInterface; |
6
|
|
|
use BenTools\OpenCubes\Component\ComponentInterface; |
7
|
|
|
use BenTools\OpenCubes\Component\Sort\Model\Sort; |
8
|
|
|
use BenTools\OpenCubes\OptionsTrait; |
9
|
|
|
use Psr\Http\Message\UriInterface; |
10
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
11
|
|
|
|
12
|
|
|
final class SortComponentFactory implements ComponentFactoryInterface |
13
|
|
|
{ |
14
|
|
|
use OptionsTrait; |
15
|
|
|
|
16
|
|
|
public const OPT_AVAILABLE_SORTS = 'available_sorts'; |
17
|
|
|
public const OPT_DEFAULT_SORTS = 'default_sorts'; |
18
|
|
|
public const OPT_APPLIED_SORTS = 'applied_sorts'; |
19
|
|
|
public const OPT_ENABLE_MULTISORT = 'enable_multisort'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var SortUriManagerInterface |
23
|
|
|
*/ |
24
|
|
|
private $uriManager; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* SortComponentFactory constructor. |
28
|
|
|
* @param array $options |
29
|
|
|
* @param SortUriManagerInterface|null $uriManager |
30
|
|
|
*/ |
31
|
|
|
public function __construct(array $options = [], SortUriManagerInterface $uriManager = null) |
32
|
|
|
{ |
33
|
|
|
$this->options = $this->resolveOptions($options); |
34
|
|
|
$this->uriManager = $uriManager ?? new SortUriManager(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $options |
39
|
|
|
* @return array |
40
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\AccessException |
41
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\InvalidOptionsException |
42
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\MissingOptionsException |
43
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\NoSuchOptionException |
44
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\OptionDefinitionException |
45
|
|
|
* @throws \Symfony\Component\OptionsResolver\Exception\UndefinedOptionsException |
46
|
|
|
*/ |
47
|
|
|
protected function resolveOptions(array $options): array |
48
|
|
|
{ |
49
|
|
|
$resolver = new OptionsResolver(); |
50
|
|
|
$resolver->setDefaults([ |
51
|
|
|
self::OPT_AVAILABLE_SORTS => [], |
52
|
|
|
self::OPT_DEFAULT_SORTS => [], |
53
|
|
|
self::OPT_APPLIED_SORTS => [], |
54
|
|
|
self::OPT_ENABLE_MULTISORT => false, |
55
|
|
|
]); |
56
|
|
|
|
57
|
|
|
$resolver->setAllowedTypes(self::OPT_AVAILABLE_SORTS, 'array'); |
58
|
|
|
$resolver->setAllowedTypes(self::OPT_DEFAULT_SORTS, 'array'); |
59
|
|
|
$resolver->setAllowedTypes(self::OPT_APPLIED_SORTS, 'array'); |
60
|
|
|
$resolver->setAllowedTypes(self::OPT_ENABLE_MULTISORT, 'bool'); |
61
|
|
|
|
62
|
|
|
return $resolver->resolve($options); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @inheritDoc |
67
|
|
|
*/ |
68
|
|
|
public function supports(string $name): bool |
69
|
|
|
{ |
70
|
|
|
return SortComponent::getName() === $name; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @inheritDoc |
75
|
|
|
* @return SortComponent |
76
|
|
|
*/ |
77
|
|
|
public function createComponent(UriInterface $uri, array $options = []): ComponentInterface |
78
|
|
|
{ |
79
|
|
|
$options = $this->resolveOptions( |
80
|
|
|
$this->getMergedOptions($options, [ |
81
|
|
|
self::OPT_APPLIED_SORTS => $this->uriManager->getAppliedSorts($uri), |
82
|
|
|
]) |
83
|
|
|
); |
84
|
|
|
|
85
|
|
|
$appliedSorts = [] !== $this->getOption(self::OPT_APPLIED_SORTS, $options) ? $this->getOption(self::OPT_APPLIED_SORTS, $options) : $this->getOption(self::OPT_DEFAULT_SORTS, $options); |
86
|
|
|
$availableSorts = $this->getOption(self::OPT_AVAILABLE_SORTS, $options); |
87
|
|
|
|
88
|
|
|
$sorts = []; |
89
|
|
|
|
90
|
|
|
foreach ($availableSorts as $field => $directions) { |
91
|
|
|
foreach ($directions as $direction) { |
92
|
|
|
if (isset($appliedSorts[$field]) && $direction === $appliedSorts[$field]) { |
93
|
|
|
$stack = true === $this->getOption(self::OPT_ENABLE_MULTISORT, $options) ? array_replace($appliedSorts, [$field => $direction]) : [$field => $direction]; |
94
|
|
|
unset($stack[$field]); |
95
|
|
|
$unsetUri = $this->uriManager->buildSortUri($uri, $stack); |
96
|
|
|
$sorts[] = new Sort($field, $direction, true, $unsetUri); |
97
|
|
|
continue; |
98
|
|
|
} |
99
|
|
|
$stack = true === $this->getOption(self::OPT_ENABLE_MULTISORT) ? array_replace($appliedSorts, [$field => $direction]) : [$field => $direction]; |
100
|
|
|
$sorts[] = new Sort($field, $direction, false, $this->uriManager->buildSortUri($uri, $stack)); |
101
|
|
|
} |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
foreach ($appliedSorts as $field => $direction) { |
105
|
|
|
if (!array_key_exists($field, $availableSorts) || !in_array($direction, $availableSorts[$field])) { |
106
|
|
|
$stack = true === $this->getOption(self::OPT_ENABLE_MULTISORT, $options) ? array_replace($appliedSorts, [$field => $direction]) : [$field => $direction]; |
107
|
|
|
unset($stack[$field]); |
108
|
|
|
$unsetUri = $this->uriManager->buildSortUri($uri, $stack); |
109
|
|
|
$sorts[] = new Sort($field, $direction, true, $unsetUri); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return new SortComponent($sorts); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|