/** * @return array */ public function calculateDimensionCombinations() { $dimensionPresets = $this->contentDimensionPresetSource->getAllPresets(); $dimensionValueCountByDimension = array(); $possibleCombinationCount = 1; $combinations = array(); foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { if (isset($dimensionPreset['presets']) && !empty($dimensionPreset['presets'])) { $dimensionValueCountByDimension[$dimensionName] = count($dimensionPreset['presets']); $possibleCombinationCount = $possibleCombinationCount * $dimensionValueCountByDimension[$dimensionName]; } } foreach ($dimensionPresets as $dimensionName => $dimensionPreset) { for ($i = 0; $i < $possibleCombinationCount; $i++) { if (!isset($combinations[$i]) || !is_array($combinations[$i])) { $combinations[$i] = array(); } $currentDimensionCurrentPreset = current($dimensionPresets[$dimensionName]['presets']); $combinations[$i][$dimensionName] = $currentDimensionCurrentPreset['values']; if (!next($dimensionPresets[$dimensionName]['presets'])) { reset($dimensionPresets[$dimensionName]['presets']); } } } return $combinations; }
/** * Array of all possible dimension configurations allowed by configured presets. * * @return array */ public function getAllAllowedCombinations() { $configuration = $this->contentDimensionPresetSource->getAllPresets(); $dimensionCombinations = []; $dimensionNames = array_keys($configuration); $dimensionCount = count($dimensionNames); if ($dimensionCount === 0) { // This is correct, we have one allowed combination which is no dimension values (empty array). return [[]]; } // Reset all presets first just to be sure foreach ($configuration as $dimensionName => &$dimensionConfiguration) { reset($dimensionConfiguration['presets']); } unset($dimensionConfiguration); while (true) { $skipCurrentCombination = false; $currentPresetCombination = ['withPresetIdentifiers' => [], 'withDimensionValues' => []]; foreach ($dimensionNames as $dimensionName) { $presetIdentifierForDimension = key($configuration[$dimensionName]['presets']); $presetForDimension = current($configuration[$dimensionName]['presets']); if (!is_array($presetForDimension) || !isset($presetForDimension['values'])) { $skipCurrentCombination = true; } $currentPresetCombination['withPresetIdentifiers'][$dimensionName] = $presetIdentifierForDimension; $currentPresetCombination['withDimensionValues'][$dimensionName] = $presetForDimension['values']; } if ($skipCurrentCombination === false && $this->contentDimensionPresetSource->isPresetCombinationAllowedByConstraints($currentPresetCombination['withPresetIdentifiers'])) { $dimensionCombinations[] = $currentPresetCombination['withDimensionValues']; } $nextDimension = 0; $hasValue = next($configuration[$dimensionNames[$nextDimension]]['presets']); while ($hasValue === false) { reset($configuration[$dimensionNames[$nextDimension]]['presets']); $nextDimension++; if (!isset($dimensionNames[$nextDimension])) { // we have gone through all dimension combinations now. return $dimensionCombinations; } $hasValue = next($configuration[$dimensionNames[$nextDimension]]['presets']); } } }