/
ConfigurationContentDimensionPresetSource.php
240 lines (209 loc) · 9.36 KB
/
ConfigurationContentDimensionPresetSource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
<?php
namespace Neos\ContentRepository\Domain\Service;
/*
* This file is part of the Neos.ContentRepository package.
*
* (c) Contributors of the Neos Project - www.neos.io
*
* This package is Open Source Software. For the full copyright and license
* information, please view the LICENSE file which was distributed with this
* source code.
*/
use Neos\Flow\Annotations as Flow;
use Neos\Utility\PositionalArraySorter;
use Neos\ContentRepository\Exception;
/**
* A Dimension Preset Source that gets presets from settings
*
* Everything is configured in Settings.yaml in path "Neos.ContentRepository.contentDimensions".
* @Flow\Scope("singleton")
*/
class ConfigurationContentDimensionPresetSource implements ContentDimensionPresetSourceInterface
{
/**
* Dimension presets configuration indexed by dimension name, see ContentDimensionPresetSourceInterface
*
* @var array
*/
protected $configuration = [];
/**
* {@inheritdoc}
*/
public function getAllPresets()
{
$sorter = new PositionalArraySorter($this->configuration);
$sortedConfiguration = $sorter->toArray();
foreach ($sortedConfiguration as &$dimensionConfiguration) {
$sorter = new PositionalArraySorter($dimensionConfiguration['presets']);
$dimensionConfiguration['presets'] = $sorter->toArray();
}
return $sortedConfiguration;
}
/**
* {@inheritdoc}
*/
public function getDefaultPreset($dimensionName)
{
if (isset($this->configuration[$dimensionName]['defaultPreset']) && isset($this->configuration[$dimensionName]['presets'][$this->configuration[$dimensionName]['defaultPreset']])) {
$preset = $this->configuration[$dimensionName]['presets'][$this->configuration[$dimensionName]['defaultPreset']];
$preset['identifier'] = $this->configuration[$dimensionName]['defaultPreset'];
return $preset;
}
return null;
}
/**
* {@inheritdoc}
*/
public function findPresetByDimensionValues($dimensionName, array $dimensionValues)
{
if (isset($this->configuration[$dimensionName])) {
foreach ($this->configuration[$dimensionName]['presets'] as $presetIdentifier => $presetConfiguration) {
if (isset($presetConfiguration['values']) && $presetConfiguration['values'] === $dimensionValues) {
$presetConfiguration['identifier'] = $presetIdentifier;
return $presetConfiguration;
}
}
}
return null;
}
/**
* Returns a list of presets of the specified dimension which are allowed in combination with the given presets
* of other dimensions.
*
* @param string $dimensionName Name of the dimension to return presets for
* @param array $preselectedDimensionPresets An array of dimension name and preset identifier specifying the presets which are already selected
* @return array An array of presets only for the dimension specified in $dimensionName. Structure is: array($dimensionName => array('presets' => array(...))
*/
public function getAllowedDimensionPresetsAccordingToPreselection($dimensionName, array $preselectedDimensionPresets)
{
if (!isset($this->configuration[$dimensionName])) {
return null;
}
$dimensionConfiguration = [$dimensionName => $this->configuration[$dimensionName]];
$sorter = new PositionalArraySorter($dimensionConfiguration[$dimensionName]['presets']);
$dimensionConfiguration[$dimensionName]['presets'] = $sorter->toArray();
foreach (array_keys($dimensionConfiguration[$dimensionName]['presets']) as $presetIdentifier) {
$currentPresetCombination = $preselectedDimensionPresets;
$currentPresetCombination[$dimensionName] = $presetIdentifier;
if (!$this->isPresetCombinationAllowedByConstraints($currentPresetCombination)) {
unset($dimensionConfiguration[$dimensionName]['presets'][$presetIdentifier]);
}
}
return $dimensionConfiguration;
}
/**
* Checks if the given combination of presets is allowed, according to possibly defined constraints in the
* content dimension configuration.
*
* @param array $dimensionsNamesAndPresetIdentifiers Preset pairs, for example array('language' => 'de', 'country' => 'GER', 'persona' => 'clueless')
* @return boolean
*/
public function isPresetCombinationAllowedByConstraints(array $dimensionsNamesAndPresetIdentifiers)
{
foreach ($dimensionsNamesAndPresetIdentifiers as $dimensionName => $presetIdentifier) {
if (!isset($this->configuration[$dimensionName]) || !isset($this->configuration[$dimensionName]['presets'][$presetIdentifier])) {
return false;
}
foreach ($this->configuration as $currentDimensionName => $dimensionConfiguration) {
if (!isset($dimensionsNamesAndPresetIdentifiers[$currentDimensionName])) {
continue;
}
$currentPresetIdentifier = $dimensionsNamesAndPresetIdentifiers[$currentDimensionName];
if (isset($dimensionConfiguration['presets'][$currentPresetIdentifier]['constraints'])) {
$constraintsResult = $this->isPresetAllowedByConstraints($dimensionName, $presetIdentifier, $dimensionConfiguration['presets'][$currentPresetIdentifier]['constraints']);
if ($constraintsResult === false) {
return false;
}
}
}
}
return true;
}
/**
* Checks if the given preset of the specified dimension is allowed according to the given constraints
*
* @param string $dimensionName Name of the dimension the preset belongs to
* @param string $presetIdentifier Identifier of the preset to check
* @param array $constraints Constraints to use for the check
* @return boolean
*/
protected function isPresetAllowedByConstraints($dimensionName, $presetIdentifier, array $constraints)
{
if (!array_key_exists($dimensionName, $constraints)) {
return true;
}
if (array_key_exists($presetIdentifier, $constraints[$dimensionName]) && $constraints[$dimensionName][$presetIdentifier] === true) {
return true;
}
if (array_key_exists($presetIdentifier, $constraints[$dimensionName]) && $constraints[$dimensionName][$presetIdentifier] === false) {
return false;
}
if (array_key_exists('*', $constraints[$dimensionName])) {
return (boolean)$constraints[$dimensionName]['*'];
}
return true;
}
/**
* @param array $configuration
* @return void
* @throws Exception
*/
public function setConfiguration(array $configuration)
{
foreach ($configuration as $dimensionName => $dimensionConfiguration) {
$defaultPreset = $dimensionConfiguration['defaultPreset'];
if (!isset($dimensionConfiguration['presets'][$defaultPreset])) {
throw new Exception(sprintf('The preset "%s" which was configured to be the default preset for the content dimension "%s" does not exist. Please check your content dimension settings.', $defaultPreset, $dimensionName), 1401093863);
}
}
$this->configuration = $configuration;
}
/**
* {@inheritdoc}
*/
public function findPresetsByTargetValues(array $targetValues)
{
$matchingPresets = [];
$allPresets = $this->getAllPresets();
foreach ($targetValues as $dimensionName => $dimensionValues) {
$matchingPresets[$dimensionName] = array_reduce($allPresets[$dimensionName]['presets'], function ($primaryPreset, $presetForDimension) use ($dimensionValues) {
return $this->comparePresetsForTargetValue($presetForDimension, $dimensionValues, $primaryPreset);
}, null);
if ($matchingPresets[$dimensionName] !== null) {
continue;
}
$matchingPresets[$dimensionName] = [
'label' => reset($dimensionValues),
'values' => $dimensionValues
];
}
return $matchingPresets;
}
/**
* Compares the given $possibleBetterPreset to the $targetValues (based on the position of the contained values)
* and returns either $possibleBetterPreset or the $currentBestPreset, depending on the result.
*
* @param array $possibleBetterPreset
* @param array $targetValues
* @param array $currentBestPreset
* @return array
*/
protected function comparePresetsForTargetValue(array $possibleBetterPreset, array $targetValues, array $currentBestPreset = null)
{
if (!isset($possibleBetterPreset['values'][0])) {
return $currentBestPreset;
}
if ($possibleBetterPreset['values'] === $targetValues) {
return $possibleBetterPreset;
}
if ($possibleBetterPreset['values'][0] === reset($targetValues)) {
return $possibleBetterPreset;
}
foreach ($targetValues as $targetValue) {
if ($currentBestPreset === null && in_array($targetValue, $possibleBetterPreset['values'])) {
return $possibleBetterPreset;
}
}
return $currentBestPreset;
}
}