Files
SkinbaseNova/app/Http/Requests/Worlds/StoreWorldRequest.php
2026-04-18 17:02:56 +02:00

141 lines
7.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Worlds;
use App\Models\World;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
class StoreWorldRequest extends FormRequest
{
/**
* @return array<int, string>
*/
protected function reservedSlugs(): array
{
return ['create'];
}
public function authorize(): bool
{
return (bool) $this->user();
}
public function rules(): array
{
$sectionKeys = array_keys((array) config('worlds.sections', []));
$relationTypes = array_keys((array) config('worlds.relation_types', []));
return [
'title' => ['required', 'string', 'max:180'],
'slug' => ['nullable', 'string', 'max:180', 'regex:/^[a-z0-9]+(?:-[a-z0-9]+)*$/', Rule::notIn($this->reservedSlugs())],
'tagline' => ['nullable', 'string', 'max:220'],
'summary' => ['nullable', 'string', 'max:320'],
'description' => ['nullable', 'string', 'max:20000'],
'cover_path' => ['nullable', 'string', 'max:2048'],
'theme_key' => ['nullable', 'string', 'max:80'],
'accent_color' => ['nullable', 'string', 'max:16'],
'accent_color_secondary' => ['nullable', 'string', 'max:16'],
'background_motif' => ['nullable', 'string', 'max:80'],
'icon_name' => ['nullable', 'string', 'max:120'],
'status' => ['required', Rule::in([World::STATUS_DRAFT, World::STATUS_PUBLISHED, World::STATUS_ARCHIVED])],
'type' => ['required', Rule::in([World::TYPE_SEASONAL, World::TYPE_EVENT, World::TYPE_CAMPAIGN, World::TYPE_TRIBUTE])],
'starts_at' => ['nullable', 'date'],
'ends_at' => ['nullable', 'date', 'after_or_equal:starts_at'],
'accepts_submissions' => ['nullable', 'boolean'],
'participation_mode' => ['nullable', Rule::in([
World::PARTICIPATION_MODE_MANUAL_APPROVAL,
World::PARTICIPATION_MODE_AUTO_ADD,
World::PARTICIPATION_MODE_CLOSED,
])],
'submission_starts_at' => ['nullable', 'date'],
'submission_ends_at' => ['nullable', 'date', 'after_or_equal:submission_starts_at'],
'submission_note_enabled' => ['nullable', 'boolean'],
'community_section_enabled' => ['nullable', 'boolean'],
'allow_readd_after_removal' => ['nullable', 'boolean'],
'is_featured' => ['nullable', 'boolean'],
'is_recurring' => ['nullable', 'boolean'],
'recurrence_key' => ['nullable', 'string', 'max:120'],
'recurrence_rule' => ['nullable', 'string', 'max:160'],
'edition_year' => ['nullable', 'integer', 'between:2000,2100'],
'cta_label' => ['nullable', 'string', 'max:120'],
'cta_url' => ['nullable', 'url', 'max:2048'],
'badge_label' => ['nullable', 'string', 'max:120'],
'badge_description' => ['nullable', 'string', 'max:2000'],
'submission_guidelines' => ['nullable', 'string', 'max:5000'],
'badge_url' => ['nullable', 'url', 'max:2048'],
'seo_title' => ['nullable', 'string', 'max:255'],
'seo_description' => ['nullable', 'string', 'max:300'],
'og_image_path' => ['nullable', 'string', 'max:2048'],
'related_tags_json' => ['nullable', 'array', 'max:12'],
'related_tags_json.*' => ['string', 'max:40'],
'section_order_json' => ['nullable', 'array'],
'section_order_json.*' => ['string', Rule::in($sectionKeys)],
'section_visibility_json' => ['nullable', 'array'],
'section_visibility_json.*' => ['boolean'],
'parent_world_id' => ['nullable', 'integer', 'exists:worlds,id'],
'published_at' => ['nullable', 'date'],
'relations' => ['nullable', 'array', 'max:60'],
'relations.*.section_key' => ['required_with:relations', 'string', Rule::in($sectionKeys)],
'relations.*.related_type' => ['required_with:relations', 'string', Rule::in($relationTypes)],
'relations.*.related_id' => ['required_with:relations', 'integer', 'min:1'],
'relations.*.context_label' => ['nullable', 'string', 'max:120'],
'relations.*.sort_order' => ['nullable', 'integer', 'min:0'],
'relations.*.is_featured' => ['nullable', 'boolean'],
];
}
public function withValidator($validator): void
{
$validator->after(function ($validator): void {
$sections = (array) config('worlds.sections', []);
if ($this->boolean('is_recurring')) {
if (trim((string) $this->input('recurrence_key', '')) === '') {
$validator->errors()->add('recurrence_key', 'Recurring worlds need a recurrence key such as halloween or retro-month.');
}
if (! is_numeric($this->input('edition_year'))) {
$validator->errors()->add('edition_year', 'Recurring worlds need an edition year.');
}
}
$recurrenceKey = trim((string) $this->input('recurrence_key', ''));
$editionYear = $this->input('edition_year');
if ($recurrenceKey !== '' && ! preg_match('/^[a-z0-9]+(?:-[a-z0-9]+)*$/', $recurrenceKey)) {
$validator->errors()->add('recurrence_key', 'Use lowercase letters, numbers, and dashes only.');
}
if ($this->boolean('is_recurring') && $recurrenceKey !== '' && is_numeric($editionYear)) {
$worldId = $this->route('world')?->id;
$exists = World::query()
->where('recurrence_key', $recurrenceKey)
->where('edition_year', (int) $editionYear)
->when($worldId, fn (Builder $builder) => $builder->where('id', '!=', $worldId))
->exists();
if ($exists) {
$validator->errors()->add('edition_year', 'That recurrence key already has an edition for this year.');
}
}
foreach ((array) $this->input('relations', []) as $index => $relation) {
$sectionKey = (string) ($relation['section_key'] ?? '');
$relatedType = (string) ($relation['related_type'] ?? '');
$allowed = (array) ($sections[$sectionKey]['relation_types'] ?? []);
if ($sectionKey !== '' && $relatedType !== '' && $allowed !== [] && ! in_array($relatedType, $allowed, true)) {
$validator->errors()->add("relations.{$index}.related_type", 'That entity type cannot be attached to the selected section.');
}
}
if ((string) $this->input('participation_mode') === World::PARTICIPATION_MODE_CLOSED && $this->boolean('accepts_submissions')) {
$validator->errors()->add('accepts_submissions', 'Closed worlds cannot accept creator submissions.');
}
});
}
}