mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-07 18:44:08 +00:00
This change improves clarity by making the method name more descriptive and aligned with its purpose. All relevant interfaces, classes, and tests have been updated accordingly to reflect this renaming.
124 lines
5.6 KiB
PHP
124 lines
5.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
/*
|
|
* Chill is a software for social workers
|
|
*
|
|
* For the full copyright and license information, please view
|
|
* the LICENSE file that was distributed with this source code.
|
|
*/
|
|
|
|
namespace Chill\MainBundle\Export;
|
|
|
|
use Chill\MainBundle\Entity\Center;
|
|
use Chill\MainBundle\Form\Type\Export\AggregatorType;
|
|
use Chill\MainBundle\Form\Type\Export\ExportType;
|
|
use Chill\MainBundle\Form\Type\Export\FilterType;
|
|
use Chill\MainBundle\Repository\CenterRepositoryInterface;
|
|
|
|
/**
|
|
* @phpstan-type NormalizedData array{centers: list<int>, export: array{form: array<string, mixed>, version: int}, filters: array<string, array{enabled: boolean, form: array<string, mixed>, version: int}>, aggregators: array<string, array{enabled: boolean, form: array<string, mixed>, version: int}>, pick_formatter: string, formatter: array{form: array<string, mixed>, version: int}}
|
|
*/
|
|
class ExportConfigNormalizer
|
|
{
|
|
public function __construct(
|
|
private readonly ExportManager $exportManager,
|
|
private readonly CenterRepositoryInterface $centerRepository,
|
|
) {}
|
|
|
|
/**
|
|
* @return NormalizedData
|
|
*/
|
|
public function normalizeConfig(string $exportAlias, array $formData): array
|
|
{
|
|
$exportData = $formData[ExportType::EXPORT_KEY];
|
|
$export = $this->exportManager->getExport($exportAlias);
|
|
|
|
$serialized = [
|
|
'export' => [
|
|
'form' => $export->normalizeFormData($exportData),
|
|
'version' => $export->getNormalizationVersion(),
|
|
],
|
|
];
|
|
|
|
$serialized['centers'] = array_values(
|
|
array_map(static fn (Center $center) => $center->getId(), $formData['centers'])
|
|
);
|
|
|
|
|
|
$filtersSerialized = [];
|
|
foreach ($formData[ExportType::FILTER_KEY] as $alias => $filterData) {
|
|
$filter = $this->exportManager->getFilter($alias);
|
|
$filtersSerialized[$alias][FilterType::ENABLED_FIELD] = (bool) $filterData[FilterType::ENABLED_FIELD];
|
|
if ($filterData[FilterType::ENABLED_FIELD]) {
|
|
$filtersSerialized[$alias]['form'] = $filter->normalizeFormData($filterData['form']);
|
|
$filtersSerialized[$alias]['version'] = $filter->getNormalizationVersion();
|
|
|
|
}
|
|
}
|
|
$serialized['filters'] = $filtersSerialized;
|
|
|
|
$aggregatorsSerialized = [];
|
|
foreach ($formData[ExportType::AGGREGATOR_KEY] as $alias => $aggregatorData) {
|
|
$aggregator = $this->exportManager->getAggregator($alias);
|
|
$aggregatorsSerialized[$alias][FilterType::ENABLED_FIELD] = (bool) $aggregatorData[AggregatorType::ENABLED_FIELD];
|
|
if ($aggregatorData[AggregatorType::ENABLED_FIELD]) {
|
|
$aggregatorsSerialized[$alias]['form'] = $aggregator->normalizeFormData($aggregatorData['form']);
|
|
$aggregatorsSerialized[$alias]['version'] = $aggregator->getNormalizationVersion();
|
|
}
|
|
}
|
|
$serialized['aggregators'] = $aggregatorsSerialized;
|
|
|
|
$serialized['pick_formatter'] = $formData['pick_formatter'];
|
|
$formatter = $this->exportManager->getFormatter($formData['pick_formatter']);
|
|
$serialized['formatter']['form'] = $formatter->normalizeFormData($formData['formatter']['form']);
|
|
$serialized['formatter']['version'] = $formatter->getNormalizationVersion();
|
|
|
|
return $serialized;
|
|
}
|
|
|
|
/**
|
|
* @param NormalizedData $serializedData
|
|
* @param bool $replaceDisabledByDefaultData if true, when a filter is not enabled, the formDefaultData is set
|
|
*/
|
|
public function denormalizeConfig(string $exportAlias, array $serializedData, bool $replaceDisabledByDefaultData = false): array
|
|
{
|
|
$export = $this->exportManager->getExport($exportAlias);
|
|
$formater = $this->exportManager->getFormatter($serializedData['pick_formatter']);
|
|
|
|
$filtersConfig = [];
|
|
foreach ($serializedData['filters'] as $alias => $filterData) {
|
|
$aggregator = $this->exportManager->getFilter($alias);
|
|
$filtersConfig[$alias]['enabled'] = $filterData['enabled'];
|
|
|
|
if ($filterData['enabled']) {
|
|
$filtersConfig[$alias]['form'] = $aggregator->denormalizeFormData($filterData['form'], $filterData['version']);
|
|
} elseif ($replaceDisabledByDefaultData) {
|
|
$filtersConfig[$alias]['form'] = $aggregator->getFormDefaultData();
|
|
}
|
|
}
|
|
|
|
$aggregatorsConfig = [];
|
|
foreach ($serializedData['aggregators'] as $alias => $aggregatorData) {
|
|
$aggregator = $this->exportManager->getAggregator($alias);
|
|
$aggregatorsConfig[$alias]['enabled'] = $aggregatorData['enabled'];
|
|
|
|
if ($aggregatorData['enabled']) {
|
|
$aggregatorsConfig[$alias]['form'] = $aggregator->denormalizeFormData($aggregatorData['form'], $aggregatorData['version']);
|
|
} elseif ($replaceDisabledByDefaultData) {
|
|
$aggregatorsConfig[$alias]['form'] = $aggregator->getFormDefaultData();
|
|
}
|
|
}
|
|
|
|
return [
|
|
'export' => $export->denormalizeFormData($serializedData['export']['form'], $serializedData['export']['version']),
|
|
'filters' => $filtersConfig,
|
|
'aggregators' => $aggregatorsConfig,
|
|
'pick_formatter' => $serializedData['pick_formatter'],
|
|
'formatter' => ['form' => $formater->denormalizeFormData($serializedData['formatter']['form'], $serializedData['formatter']['version'])],
|
|
'centers' => array_filter(array_map(fn (int $id) => $this->centerRepository->find($id), $serializedData['centers']), fn ($item) => null !== $item),
|
|
];
|
|
}
|
|
}
|