Files
landing-api/app/Http/Resources/PageResource.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

62 lines
2.3 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Resources;
use App\Models\Page;
use App\Support\ImagePayload;
use App\Support\TranslatablePayload;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin Page
*/
class PageResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$locale = $request->query(config('locales.query_parameter')) ?? app()->getLocale();
$resolved = TranslatablePayload::resolve($this->resource, $this->translatable, $locale);
return [
'id' => $this->id,
'locale' => $locale,
...$resolved,
'status' => $this->status->value,
'status_label' => $this->status->getLabel(),
'is_featured' => $this->is_featured,
// The stored path, because this is the editing API and it is
// what a client PATCHes back. What was *derived* from it travels
// alongside rather than replacing it.
'featured_image' => $this->featured_image,
'featured_image_media' => ImagePayload::make(
$this->featured_image,
$this->resource->relationLoaded('featuredMedia') ? $this->resource->getRelation('featuredMedia') : null,
$resolved['featured_image_alt'] ?? null,
),
'published_at' => $this->published_at?->toIso8601String(),
'unpublish_at' => $this->unpublish_at?->toIso8601String(),
'created_at' => $this->created_at?->toIso8601String(),
'updated_at' => $this->updated_at?->toIso8601String(),
'deleted_at' => $this->deleted_at?->toIso8601String(),
'category' => CategoryResource::make($this->whenLoaded('category')),
'author' => UserResource::make($this->whenLoaded('author')),
// Opt-in: `?with=translations` returns every language at once,
// which is what a translation UI needs and a page renderer does not.
'translations' => $this->when(
in_array('translations', explode(',', (string) $request->query('with')), true),
fn () => TranslatablePayload::all($this->resource, $this->translatable),
),
];
}
}