feat: implement AI styling foundation and billing scope rework
This commit is contained in:
90
app/Services/AiEditing/EventAiEditingPolicyService.php
Normal file
90
app/Services/AiEditing/EventAiEditingPolicyService.php
Normal file
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\AiEditing;
|
||||
|
||||
use App\Models\AiStyle;
|
||||
use App\Models\Event;
|
||||
use Illuminate\Support\Arr;
|
||||
use Illuminate\Support\Collection;
|
||||
|
||||
class EventAiEditingPolicyService
|
||||
{
|
||||
/**
|
||||
* @return array{
|
||||
* enabled: bool,
|
||||
* allow_custom_prompt: bool,
|
||||
* allowed_style_keys: array<int, string>,
|
||||
* 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<int, string> $allowedStyleKeys */
|
||||
$allowedStyleKeys = $policy['allowed_style_keys'];
|
||||
if ($allowedStyleKeys === []) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return in_array($style->key, $allowedStyleKeys, true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Collection<int, AiStyle> $styles
|
||||
* @return Collection<int, AiStyle>
|
||||
*/
|
||||
public function filterStyles(Event $event, Collection $styles): Collection
|
||||
{
|
||||
$policy = $this->resolve($event);
|
||||
|
||||
/** @var array<int, string> $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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user