}> */ public function validateSlug(string $slug, ?int $ignoreId = null): array { if ('' === $slug) { return [[self::ERROR_SLUG_REQUIRED, []]]; } if ($this->smartLinkModel->isSlugTaken($slug, $ignoreId)) { return [[self::ERROR_SLUG_TAKEN, ['%slug%' => $slug]]]; } return []; } /** * A destination is required for every redirect type: an URL for external ones, a landing * page or a standalone form for Mautic ones. Only http(s) URLs are accepted. * * @return list}> */ public function validateDestination(SmartLinkRedirectType $redirectType, ?string $url, ?Page $page = null, ?Form $form = null): array { if ($redirectType->isMautic()) { if (null === $page && null === $form) { return [[self::ERROR_DESTINATION_REQUIRED_MAUTIC, []]]; } return []; } $url = trim((string) $url); if ('' === $url) { return [[self::ERROR_DESTINATION_REQUIRED, []]]; } if (!filter_var($url, FILTER_VALIDATE_URL) || !in_array(parse_url($url, PHP_URL_SCHEME), ['http', 'https'], true)) { return [[self::ERROR_DESTINATION_INVALID, ['%url%' => $url]]]; } return []; } /** * UTM values follow the Smart Links Settings the admin defines. With a taxonomy chosen: * every parameter the taxonomy defines limits the value -- the checkbox subset of the * admin's values for a dropdown, the taxonomy's own list for a free parameter. The * values the link carried before the edit stay acceptable (they were chosen when the * taxonomy — or, before the feature, the global configuration — allowed them), so a plain * re-save never errors. Without a taxonomy (links that predate the feature, before their * next edit), the values follow the rules of Smart Links Settings. * * @param array $utmParameters * @param array $stored the values the link carried before the edit * * @return list}> */ public function validateUtmParameters(array $utmParameters, ?UtmTaxonomy $taxonomy = null, array $stored = []): array { if (null === $taxonomy) { return $this->validateAgainstGlobalConfiguration($utmParameters, $stored); } $errors = []; foreach ($utmParameters as $key => $value) { $parameter = $this->utmConfiguration->getParameter($key); if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) { $errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]]; continue; } // Every parameter of the taxonomy limits the value: a dropdown subset selected // among the admin's values, or the taxonomy's own list for a free parameter. $allowed = $taxonomy->getParameterValues($key); if ([] === $allowed) { $errors[] = [self::ERROR_TAXONOMY_PARAMETER_UNKNOWN, ['%key%' => $key, '%name%' => (string) $taxonomy->getName()]]; continue; } if (in_array($value, array_column($allowed, 'value'), true) || ($stored[$key] ?? null) === $value) { continue; } $errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]]; } return $errors; } /** * The rules of Smart Links Settings: the values belong to enabled parameters, respect the * technical pattern, and dropdown values stay within the configured list — unless the link * already carried them. * * @param array $utmParameters * @param array $stored * * @return list}> */ private function validateAgainstGlobalConfiguration(array $utmParameters, array $stored): array { $valuePattern = UtmSettingsValidator::VALUE_PATTERN; $errors = []; foreach ($utmParameters as $key => $value) { $parameter = $this->utmConfiguration->getParameter($key); if (null === $parameter || (!$parameter['enabled']) || UtmParameterMode::DISABLED === $parameter['mode']) { $errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]]; continue; } if (1 !== preg_match($valuePattern, $value)) { $errors[] = [self::ERROR_UTM_VALUE_INVALID, ['%key%' => $key]]; continue; } $inList = UtmParameterMode::DROPDOWN !== $parameter['mode'] || in_array($value, array_column($this->utmConfiguration->getDropdownValues($key), 'value'), true); if (!$inList && ($stored[$key] ?? null) !== $value) { $errors[] = [self::ERROR_UTM_VALUE_NOT_ALLOWED, ['%key%' => $key, '%value%' => $value]]; } } return $errors; } }