Files
landing-api/app/Http/Requests/Api/V1/PageRequest.php
Josep Piguillem 9f970808ca
Some checks failed
Tests / lint (push) Has been cancelled
Tests / test (push) Has been cancelled
Tests / test-postgres (push) Has been cancelled
Tests / mcp (push) Has been cancelled
Tag blog posts and sign them with a member
Tags are named in every language and made from the page form; the byline is
the member who signs a post on the site. The public API sends both, filters
posts by tag and lists the tags in use.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-01 11:01:18 +02:00

78 lines
3.0 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\Api\V1;
use App\Enums\PageStatus;
use App\Models\Page;
use App\Rules\Translatable;
use App\Rules\UniqueTranslatedSlug;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
/**
* Shared validation for creating and updating pages. On update every field is
* optional (PATCH semantics) but keeps the same shape rules.
*/
class PageRequest extends FormRequest
{
/**
* Authorisation is handled by the controller's policy calls, which produce
* clearer 403s than a boolean here would.
*/
public function authorize(): bool
{
return true;
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
$page = $this->route('page');
$creating = ! $page instanceof Page;
$sometimes = $creating ? 'required' : 'sometimes';
return [
'title' => [$sometimes, new Translatable(requireDefaultLocale: $creating, max: 255)],
'slug' => [
$sometimes,
new Translatable(requireDefaultLocale: $creating, max: 255),
new UniqueTranslatedSlug(Page::class, $page?->id),
],
'excerpt' => ['sometimes', 'nullable', new Translatable(max: 500)],
'body' => ['sometimes', 'nullable', new Translatable],
'meta_title' => ['sometimes', 'nullable', new Translatable(max: 60)],
'meta_description' => ['sometimes', 'nullable', new Translatable(max: 160)],
'category_id' => ['sometimes', 'nullable', 'integer', Rule::exists('categories', 'id')->whereNull('deleted_at')],
// Who signs it on the site; replaced whole when sent, like the tags.
'byline_member_id' => ['sometimes', 'nullable', 'integer', Rule::exists('members', 'id')->whereNull('deleted_at')],
'tag_ids' => ['sometimes', 'array'],
'tag_ids.*' => ['integer', 'distinct', Rule::exists('tags', 'id')],
'status' => ['sometimes', Rule::enum(PageStatus::class)],
'featured_image' => ['sometimes', 'nullable', 'string', 'max:2048'],
'featured_image_alt' => ['sometimes', 'nullable', new Translatable(max: 255)],
'is_featured' => ['sometimes', 'boolean'],
'published_at' => ['sometimes', 'nullable', 'date'],
'unpublish_at' => ['sometimes', 'nullable', 'date', 'after:published_at'],
];
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
// Publishing is a distinct permission from editing, so a caller
// who may write content still cannot push it live on their own.
$status = $this->input('status');
if ($status === PageStatus::Published->value && $this->user()?->cannot('pages.publish')) {
$validator->errors()->add('status', __('rules.pages.cannot_publish'));
}
});
}
}