input('dominio'); $normalizedDomain = TenantDomainNormalizer::normalize($rawDomain); $this->hasInvalidDomain = TenantDomainNormalizer::hasValue($rawDomain) && $normalizedDomain === null; $this->merge([ 'dominio' => $normalizedDomain, ]); } /** * @return array */ 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:(?[-\w.+\/]+);base64,(?.+)$/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, ]; } }