Files
Alexandre Gain b138056e43 style: fix indentation, alignment, and docblock annotations
- Correct getChoices() docblock to reflect translated label => enum
  backing value order instead of value => label
- Re-indent Smart Links menu config to match actual array nesting
- Align assignment operators in RefreshStatsCommand
- Add missing newline at end of RefreshStatsCommand.php
2026-11-21 11:23:33 +01:00

137 lines
4.8 KiB
PHP

<?php
declare(strict_types=1);
namespace MauticPlugin\MT3SMarketingProgramBundle\Service;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterCatalog;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterDefinition;
use MauticPlugin\MT3SMarketingProgramBundle\Catalog\UtmParameterMode;
/**
* Turns the stored `mt3s_smart_links_utm_config` array into a canonical, typed structure.
*
* - Every catalog parameter is present (catalog defaults fill the gaps), followed by valid custom parameters.
* - Mautic writes nested config scalars as strings ('1' / ''), so booleans are cast back.
* - Only `dropdown` parameters carry `values` and `default`; `free` and `disabled` parameters keep
* `enabled`, `label` and `mode` only.
*
* @phpstan-type UtmValue array{value: string, label: string}
* @phpstan-type UtmParameter array{enabled: bool, label: string, mode: string, values?: list<UtmValue>, default?: ?string}
* @phpstan-type UtmConfig array{enforce_lowercase: bool, separator_replacement: string, parameters: array<string, UtmParameter>}
*/
final class UtmConfigurationNormalizer
{
public const SEPARATORS = ['-', '_', '.'];
public const DEFAULT_SEPARATOR = '-';
public function __construct(
private readonly UtmParameterCatalog $catalog,
) {
}
/**
* @return UtmConfig
*/
public function normalize(mixed $raw): array
{
$raw = is_array($raw) ? $raw : [];
$separator = $raw['separator_replacement'] ?? self::DEFAULT_SEPARATOR;
$stored = is_array($raw['parameters'] ?? null) ? $raw['parameters'] : [];
$parameters = [];
foreach ($this->catalog->all() as $key => $definition) {
$parameters[$key] = $this->normalizeParameter($stored[$key] ?? null, $definition);
}
foreach ($stored as $key => $parameter) {
$key = (string) $key;
if (isset($parameters[$key]) || !$this->catalog->isValidCustomKey($key)) {
continue;
}
$parameters[$key] = $this->normalizeParameter($parameter, null);
}
return [
'enforce_lowercase' => array_key_exists('enforce_lowercase', $raw) ? self::toBool($raw['enforce_lowercase']) : true,
'separator_replacement' => in_array($separator, self::SEPARATORS, true) ? $separator : self::DEFAULT_SEPARATOR,
'parameters' => $parameters,
];
}
/**
* @return UtmParameter
*/
private function normalizeParameter(mixed $stored, ?UtmParameterDefinition $definition): array
{
$defaultMode = $definition->defaultMode ?? UtmParameterMode::DISABLED;
$allowedModes = $definition?->getAllowedModes() ?? UtmParameterMode::ALL;
if (!is_array($stored)) {
// Free parameters start enabled. Dropdown parameters start switched off: an enabled dropdown
// needs values, and an invalid default would block saving the whole Configuration page.
$stored = ['enabled' => UtmParameterMode::FREE === $defaultMode, 'mode' => $defaultMode];
}
$mode = (string) ($stored['mode'] ?? $defaultMode);
if (!in_array($mode, $allowedModes, true)) {
$mode = $defaultMode;
}
$parameter = [
'enabled' => self::toBool($stored['enabled'] ?? false),
'label' => trim((string) ($stored['label'] ?? '')),
'mode' => $mode,
];
if (UtmParameterMode::DROPDOWN !== $mode) {
return $parameter;
}
$values = $this->normalizeValues($stored['values'] ?? []);
$default = (string) ($stored['default'] ?? '');
$parameter['values'] = $values;
$parameter['default'] = in_array($default, array_column($values, 'value'), true) ? $default : null;
return $parameter;
}
/**
* @return list<array{value: string, label: string}>
*/
private function normalizeValues(mixed $values): array
{
if (!is_array($values)) {
return [];
}
$normalized = [];
foreach ($values as $row) {
if (!is_array($row)) {
continue;
}
$value = trim((string) ($row['value'] ?? ''));
if ('' === $value || isset($normalized[$value])) {
continue;
}
$label = trim((string) ($row['label'] ?? ''));
$normalized[$value] = ['value' => $value, 'label' => '' !== $label ? $label : $value];
}
return array_values($normalized);
}
public static function toBool(mixed $value): bool
{
if (is_string($value)) {
return in_array(strtolower(trim($value)), ['1', 'true', 'yes', 'on'], true);
}
return (bool) $value;
}
}