*/ class UtmTaxonomyModel extends FormModel { public const MODEL_NAME = 'mt3smarketingprogram.utm_taxonomy'; public function __construct( EntityManagerInterface $em, CorePermissions $security, EventDispatcherInterface $dispatcher, UrlGeneratorInterface $router, TranslatorInterface $translator, UserHelper $userHelper, LoggerInterface $logger, CoreParametersHelper $coreParametersHelper, ) { parent::__construct($em, $security, $dispatcher, $router, $translator, $userHelper, $logger, $coreParametersHelper); } public function getRepository(): UtmTaxonomyRepository { $repository = $this->em->getRepository(UtmTaxonomy::class); \assert($repository instanceof UtmTaxonomyRepository); return $repository; } public function getPermissionBase(): string { return 'mt3smarketingprogram:taxonomies'; } public function getEntity($id = null): ?UtmTaxonomy { if (null === $id) { return new UtmTaxonomy(); } $entity = parent::getEntity($id); return $entity instanceof UtmTaxonomy ? $entity : null; } /** * @param array $options */ public function createForm($entity, FormFactoryInterface $formFactory, $action = null, $options = []): FormInterface { if (!$entity instanceof UtmTaxonomy) { throw new MethodNotAllowedHttpException(['UtmTaxonomy']); } if (!empty($action)) { $options['action'] = $action; } return $formFactory->create(UtmTaxonomyType::class, $entity, $options); } /** * Applies the canonical shape to the value lists (trimmed, de-duplicated, sorted), then saves. */ public function saveEntity($entity, $unlock = true): void { if ($entity instanceof UtmTaxonomy) { $entity->setParameters($this->normalizeParameters($entity->getParameters())); } parent::saveEntity($entity, $unlock); } /** * The canonical shape of the `parameters` array: parameter key => list of {value, label} rows. * Empty keys and values are dropped, duplicates keep their first label, labels fall back to * the value, keys and rows keep a stable order. The UtmTaxonomyValidator guarantees the * technical pattern of the values; this is the safety net behind it. * * @param array $parameters * * @return array> */ public function normalizeParameters(array $parameters): array { $clean = []; foreach ($parameters as $key => $rows) { $key = trim((string) $key); if ('' === $key || !is_array($rows)) { continue; } $values = []; foreach ($rows as $row) { if (!is_array($row)) { continue; } $value = trim((string) ($row['value'] ?? '')); if ('' === $value || isset($values[$value])) { continue; } $label = trim((string) ($row['label'] ?? '')); $values[$value] = ['value' => $value, 'label' => '' !== $label ? $label : $value]; } if ([] !== $values) { $clean[$key] = array_values($values); } } ksort($clean); return $clean; } /** * True when another saved taxonomy already uses this name. $ignoreId keeps a taxonomy being * edited from colliding with itself. */ public function isNameTaken(string $name, ?int $ignoreId = null): bool { return $this->getRepository()->nameExists($name, $ignoreId); } }