Files
preregister/app/Http/Requests/Admin/PageBlockContentValidator.php

163 lines
7.6 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Admin;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Contracts\Validation\Validator as ValidatorContract;
use Illuminate\Support\Facades\Validator;
final class PageBlockContentValidator
{
/**
* @param array<int, array<string, mixed>> $blocks
*/
public static function validateBlocksArray(ValidatorContract $validator, array $blocks): void
{
$formCount = 0;
foreach ($blocks as $i => $block) {
if (! is_array($block)) {
$validator->errors()->add("blocks.$i", __('Ongeldig blok.'));
continue;
}
$type = $block['type'] ?? '';
if ($type === 'form') {
$formCount++;
}
$content = $block['content'] ?? [];
if (! is_array($content)) {
$validator->errors()->add("blocks.$i.content", __('Ongeldige blokinhoud.'));
continue;
}
$rules = self::rulesForType((string) $type);
if ($rules === []) {
$validator->errors()->add("blocks.$i.type", __('Onbekend bloktype.'));
continue;
}
$inner = Validator::make($content, $rules);
if ($inner->fails()) {
foreach ($inner->errors()->messages() as $field => $messages) {
foreach ($messages as $message) {
$validator->errors()->add("blocks.$i.content.$field", $message);
}
}
}
if ($type === 'form') {
foreach (['first_name', 'last_name', 'email'] as $coreField) {
if (! data_get($content, "fields.$coreField.enabled", true)) {
$validator->errors()->add(
"blocks.$i.content.fields.$coreField.enabled",
__('Voornaam, achternaam en e-mail moeten ingeschakeld blijven.')
);
}
}
}
}
if ($formCount > 1) {
$validator->errors()->add('blocks', __('Er mag maximaal één registratieformulier-blok zijn.'));
}
}
/**
* @return array<string, array<int, string|ValidationRule>>
*/
private static function rulesForType(string $type): array
{
$icons = 'ticket,clock,mail,users,star,heart,gift,music,shield,check';
return match ($type) {
'hero' => [
'headline' => ['required', 'string', 'max:255'],
'subheadline' => ['nullable', 'string', 'max:5000'],
'eyebrow_text' => ['nullable', 'string', 'max:255'],
'eyebrow_style' => ['nullable', 'string', 'in:badge,text,none'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'image' => [
'image' => ['nullable', 'string', 'max:500'],
'link_url' => ['nullable', 'string', 'max:500'],
'alt' => ['nullable', 'string', 'max:255'],
'max_width_px' => ['nullable', 'integer', 'min:48', 'max:800'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'benefits' => [
'title' => ['nullable', 'string', 'max:255'],
'items' => ['required', 'array', 'min:1'],
'items.*.icon' => ['required', 'string', "in:$icons"],
'items.*.text' => ['required', 'string', 'max:500'],
'layout' => ['nullable', 'string', 'in:list,grid'],
'max_columns' => ['nullable', 'integer', 'in:1,2,3'],
],
'social_proof' => [
'template' => ['required', 'string', 'max:255'],
'min_count' => ['required', 'integer', 'min:0'],
'show_animation' => ['sometimes', 'boolean'],
'style' => ['nullable', 'string', 'in:pill,badge,plain'],
],
'form' => [
'title' => ['nullable', 'string', 'max:255'],
'description' => ['nullable', 'string', 'max:2000'],
'button_label' => ['required', 'string', 'max:120'],
'button_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'button_text_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'show_field_icons' => ['sometimes', 'boolean'],
'privacy_text' => ['nullable', 'string', 'max:2000'],
'privacy_url' => ['nullable', 'string', 'max:500'],
'fields' => ['required', 'array'],
'fields.first_name.enabled' => ['sometimes', 'boolean'],
'fields.first_name.required' => ['sometimes', 'boolean'],
'fields.first_name.label' => ['nullable', 'string', 'max:120'],
'fields.first_name.placeholder' => ['nullable', 'string', 'max:255'],
'fields.last_name.enabled' => ['sometimes', 'boolean'],
'fields.last_name.required' => ['sometimes', 'boolean'],
'fields.last_name.label' => ['nullable', 'string', 'max:120'],
'fields.last_name.placeholder' => ['nullable', 'string', 'max:255'],
'fields.email.enabled' => ['sometimes', 'boolean'],
'fields.email.required' => ['sometimes', 'boolean'],
'fields.email.label' => ['nullable', 'string', 'max:120'],
'fields.email.placeholder' => ['nullable', 'string', 'max:255'],
'fields.phone.enabled' => ['sometimes', 'boolean'],
'fields.phone.required' => ['sometimes', 'boolean'],
'fields.phone.label' => ['nullable', 'string', 'max:120'],
'fields.phone.placeholder' => ['nullable', 'string', 'max:255'],
],
'countdown' => [
'target_datetime' => ['required', 'date'],
'title' => ['nullable', 'string', 'max:255'],
'expired_action' => ['required', 'string', 'in:hide,show_message,reload'],
'expired_message' => ['nullable', 'string', 'max:1000'],
'style' => ['nullable', 'string', 'in:large,compact'],
'show_labels' => ['sometimes', 'boolean'],
'labels' => ['nullable', 'array'],
'labels.days' => ['nullable', 'string', 'max:32'],
'labels.hours' => ['nullable', 'string', 'max:32'],
'labels.minutes' => ['nullable', 'string', 'max:32'],
'labels.seconds' => ['nullable', 'string', 'max:32'],
],
'text' => [
'title' => ['nullable', 'string', 'max:255'],
'body' => ['required', 'string', 'max:20000'],
'text_size' => ['nullable', 'string', 'in:sm,base,lg'],
'text_alignment' => ['nullable', 'string', 'in:center,left,right'],
],
'cta_banner' => [
'text' => ['required', 'string', 'max:500'],
'button_label' => ['required', 'string', 'max:120'],
'button_url' => ['required', 'string', 'url:http,https', 'max:500'],
'button_color' => ['nullable', 'string', 'regex:/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3})$/'],
'style' => ['nullable', 'string', 'in:inline,stacked'],
],
'divider' => [
'style' => ['required', 'string', 'in:line,dots,space_only'],
'spacing' => ['required', 'string', 'in:small,medium,large'],
],
default => [],
};
}
}