, * policy_message: ?string * } */ public function resolve(Event $event): array { $settings = is_array($event->settings) ? $event->settings : []; $aiSettings = Arr::get($settings, 'ai_editing', []); $aiSettings = is_array($aiSettings) ? $aiSettings : []; $enabled = array_key_exists('enabled', $aiSettings) ? (bool) $aiSettings['enabled'] : true; $allowCustomPrompt = array_key_exists('allow_custom_prompt', $aiSettings) ? (bool) $aiSettings['allow_custom_prompt'] : true; $allowedStyleKeys = collect($aiSettings['allowed_style_keys'] ?? []) ->filter(fn (mixed $value): bool => is_string($value) && trim($value) !== '') ->map(fn (string $value): string => trim($value)) ->unique() ->values() ->all(); $policyMessage = trim((string) ($aiSettings['policy_message'] ?? '')); return [ 'enabled' => $enabled, 'allow_custom_prompt' => $allowCustomPrompt, 'allowed_style_keys' => $allowedStyleKeys, 'policy_message' => $policyMessage !== '' ? $policyMessage : null, ]; } public function isEnabled(Event $event): bool { return (bool) $this->resolve($event)['enabled']; } public function isStyleAllowed(Event $event, ?AiStyle $style): bool { $policy = $this->resolve($event); if ($style === null) { return (bool) $policy['allow_custom_prompt']; } /** @var array $allowedStyleKeys */ $allowedStyleKeys = $policy['allowed_style_keys']; if ($allowedStyleKeys === []) { return true; } return in_array($style->key, $allowedStyleKeys, true); } /** * @param Collection $styles * @return Collection */ public function filterStyles(Event $event, Collection $styles): Collection { $policy = $this->resolve($event); /** @var array $allowedStyleKeys */ $allowedStyleKeys = $policy['allowed_style_keys']; if ($allowedStyleKeys === []) { return $styles->values(); } return $styles ->filter(fn (AiStyle $style): bool => in_array($style->key, $allowedStyleKeys, true)) ->values(); } }