Files
landing-api/app/Http/Requests/PublicApi/ReportViewsRequest.php
Josep Piguillem 3897113313 Initial commit: Pireon Admin — headless admin panel and public API
Laravel 13 + Filament 5 admin panel and REST API for a non-profit developer
collective, backed by the Pireon OAuth identity server. The public site is a
separate, server-rendered application consuming /api/public/v1.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-08-19 17:55:27 +02:00

75 lines
2.1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Requests\PublicApi;
use App\Support\ContentTypes;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
/**
* What the frontend rendered.
*
* Addressed the way the frontend already thinks: it fetched
* `/public/v1/projects/mi-web` to draw the page, so it knows the type and the
* slug without having to parse its own URL back apart. That also means a
* report can only ever name something this API serves.
*/
class ReportViewsRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/**
* Accept one report the same as a batch of one.
*/
protected function prepareForValidation(): void
{
if ($this->has('type') && ! $this->has('views')) {
$this->merge(['views' => [$this->only(['type', 'slug', 'locale', 'count'])]]);
}
}
/**
* @return array<string, mixed>
*/
public function rules(): array
{
return [
'views' => ['required', 'array', 'min:1', 'max:'.$this->maxBatch()],
'views.*.type' => ['required', 'string', Rule::in(ContentTypes::keys())],
'views.*.slug' => ['required', 'string', 'max:200'],
'views.*.locale' => [
'sometimes', 'nullable', 'string',
Rule::in(array_keys((array) config('locales.supported'))),
],
/*
* Optional, because the obvious client sends one report per page
* view. Capped because the frontend is trusted to count its own
* renders and not to invent an audience: a number nobody can
* check is a number worth bounding.
*/
'views.*.count' => ['sometimes', 'integer', 'min:1', 'max:1000'],
];
}
/**
* @return array<int, array<string, mixed>>
*/
public function views(): array
{
return array_values((array) $this->validated('views'));
}
public function maxBatch(): int
{
return max(1, (int) config('site.telemetry.max_batch', 50));
}
}