78 lines
2.7 KiB
PHP
78 lines
2.7 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Tenant\Requests;
|
|
|
|
use App\Domains\Shared\Rules\ImageOrBase64Rule;
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use App\Domains\Tenant\Support\TenantDomainNormalizer;
|
|
use Closure;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class UpdateTenantRequest extends FormRequest
|
|
{
|
|
protected bool $hasInvalidDomain = false;
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
if ($this->has('dominio')) {
|
|
$rawDomain = $this->input('dominio');
|
|
$normalizedDomain = TenantDomainNormalizer::normalize($rawDomain);
|
|
|
|
$this->hasInvalidDomain = TenantDomainNormalizer::hasValue($rawDomain)
|
|
&& $normalizedDomain === null;
|
|
|
|
$this->merge([
|
|
'dominio' => $normalizedDomain,
|
|
]);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return array<string, mixed>
|
|
*/
|
|
public function rules(): array
|
|
{
|
|
/** @var Tenant|null $tenant */
|
|
$tenant = $this->route('tenant');
|
|
|
|
$logoRule = ['nullable', new ImageOrBase64Rule()];
|
|
|
|
|
|
return [
|
|
'codigo' => [
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
Rule::unique('tenants', 'codigo')->ignore($tenant?->id),
|
|
],
|
|
'nombre' => ['nullable', 'string', 'max:255'],
|
|
'dominio' => [
|
|
'bail',
|
|
function (string $attribute, mixed $value, Closure $fail): void {
|
|
if ($this->hasInvalidDomain) {
|
|
$fail("The {$attribute} field must contain a valid domain or URL.");
|
|
}
|
|
},
|
|
'nullable',
|
|
'string',
|
|
'max:255',
|
|
Rule::unique('tenants', 'dominio')->ignore($tenant?->id),
|
|
],
|
|
'primary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'secondary_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'danger_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'success_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'header_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'footer_bg_color' => ['nullable', 'string', 'regex:/^#([a-fA-F0-9]{3,4}|[a-fA-F0-9]{6}|[a-fA-F0-9]{8})$/'],
|
|
'header_logo' => $logoRule,
|
|
'footer_logo' => $logoRule,
|
|
];
|
|
}
|
|
}
|