109 lines
4.1 KiB
PHP
109 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Tenant\Requests;
|
|
|
|
use App\Domains\Tenant\Support\TenantDomainNormalizer;
|
|
use Closure;
|
|
use Illuminate\Foundation\Http\FormRequest;
|
|
use Illuminate\Http\UploadedFile;
|
|
use Illuminate\Support\Str;
|
|
use Illuminate\Validation\Rule;
|
|
|
|
class StoreTenantRequest extends FormRequest
|
|
{
|
|
protected bool $hasInvalidDomain = false;
|
|
|
|
public function authorize(): bool
|
|
{
|
|
return true;
|
|
}
|
|
|
|
protected function prepareForValidation(): void
|
|
{
|
|
$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
|
|
{
|
|
$logoRule = [
|
|
'nullable',
|
|
static function (string $attribute, mixed $value, Closure $fail): void {
|
|
if ($value instanceof UploadedFile) {
|
|
$mime = $value->getMimeType();
|
|
if (! str_starts_with($mime, 'image/')) {
|
|
$fail("The {$attribute} must be a valid image or SVG.");
|
|
}
|
|
return;
|
|
}
|
|
|
|
if (is_string($value)) {
|
|
if (Str::isUuid($value)) {
|
|
return;
|
|
}
|
|
|
|
try {
|
|
$payload = trim($value);
|
|
$declaredMimeType = null;
|
|
if (preg_match('/^data:(?<mime>[-\w.+\/]+);base64,(?<data>.+)$/s', $payload, $matches) === 1) {
|
|
$declaredMimeType = strtolower($matches['mime']);
|
|
$payload = $matches['data'];
|
|
}
|
|
$decoded = base64_decode(preg_replace('/\s+/', '', $payload), true);
|
|
if ($decoded === false || $decoded === '') {
|
|
$fail("The {$attribute} must be a valid base64 image or SVG.");
|
|
return;
|
|
}
|
|
$finfo = new \finfo(FILEINFO_MIME_TYPE);
|
|
$mime = $finfo->buffer($decoded);
|
|
if (! $mime && $declaredMimeType) {
|
|
$mime = $declaredMimeType;
|
|
}
|
|
if (! $mime || ! str_starts_with($mime, 'image/')) {
|
|
$fail("The {$attribute} must be a valid image or SVG.");
|
|
}
|
|
} catch (\Throwable) {
|
|
$fail("The {$attribute} must be a valid image or SVG.");
|
|
}
|
|
return;
|
|
}
|
|
|
|
$fail("The {$attribute} must be a valid file or base64 string.");
|
|
},
|
|
];
|
|
|
|
return [
|
|
'codigo' => ['required', 'string', 'max:255', Rule::unique('tenants', 'codigo')],
|
|
'nombre' => ['required', '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'),
|
|
],
|
|
'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})$/'],
|
|
'header_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,
|
|
];
|
|
}
|
|
}
|