HEX
Server: LiteSpeed
System: Linux s1049.use1.mysecurecloudhost.com 4.18.0-477.27.2.lve.el8.x86_64 #1 SMP Wed Oct 11 12:32:56 UTC 2023 x86_64
User: xedaptot (3356)
PHP: 8.3.31
Disabled: NONE
Upload Files
File: /home/xedaptot/ai.naniguide.com/app/Http/Requests/Refactor/UpdateBrandVoiceRequest.php
<?php

namespace App\Http\Requests\Refactor;

use App\Http\Requests\Request;

/**
 * Validates the brand voice settings form at
 * `POST /rui/account/brand-voice`. The two lists are submitted as comma-
 * separated strings from the chip-style inputs; the controller turns them
 * into normalized arrays before persisting.
 */
class UpdateBrandVoiceRequest extends Request
{
    public function authorize(): bool
    {
        return $this->user() !== null;
    }

    public function rules(): array
    {
        return [
            'adjectives' => ['nullable', 'string', 'max:600'],
            'forbidden' => ['nullable', 'string', 'max:600'],
            'sample' => ['nullable', 'string', 'max:2000'],
        ];
    }

    /**
     * @return list<string>
     */
    public function adjectivesList(): array
    {
        return $this->parseList((string) $this->input('adjectives', ''));
    }

    /**
     * @return list<string>
     */
    public function forbiddenList(): array
    {
        return $this->parseList((string) $this->input('forbidden', ''));
    }

    public function sampleText(): ?string
    {
        $v = trim((string) $this->input('sample', ''));

        return $v === '' ? null : $v;
    }

    /**
     * @return list<string>
     */
    private function parseList(string $raw): array
    {
        if ($raw === '') {
            return [];
        }

        $items = array_map('trim', explode(',', $raw));
        $items = array_filter($items, fn (string $v) => $v !== '' && mb_strlen($v) <= 40);
        $items = array_values(array_unique($items));

        return array_slice($items, 0, 10);
    }
}