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

68 lines
2.4 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Http\Resources\PublicApi;
use App\Models\Page;
use App\Support\ImagePayload;
use App\Support\SiteUrls;
use App\Support\TranslatablePayload;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin Page
*/
class PublicPageResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
public function toArray(Request $request): array
{
$locale = app()->getLocale();
return [
'id' => $this->id,
'locale' => $locale,
/*
* An explicit list rather than the model's whole translatable
* set. `featured_image_alt` is in that set, and resolving it here
* put the alt text at the top level as well as inside
* `featured_image`, where the specification says it lives and
* where an <img> needs it. Two names for one string is how a
* frontend ends up reading the one that later turns out to be the
* accidental one.
*/
...TranslatablePayload::resolve(
$this->resource,
['title', 'slug', 'excerpt', 'body', 'meta_title', 'meta_description'],
$locale,
),
'status' => $this->status->value,
'is_featured' => $this->is_featured,
'featured_image' => ImagePayload::make(
$this->featured_image,
$this->resource->relationLoaded('featuredMedia') ? $this->resource->getRelation('featuredMedia') : null,
$this->getTranslation('featured_image_alt', $locale, false) ?: null,
),
'published_at' => $this->published_at?->toIso8601String(),
// So a cache downstream can expire when the content does, rather
// than holding a notice that is no longer true.
'unpublish_at' => $this->unpublish_at?->toIso8601String(),
'updated_at' => $this->updated_at?->toIso8601String(),
'category' => PublicCategoryResource::make($this->whenLoaded('category')),
// Lets the frontend emit correct hreflang tags. Only locales that
// actually have a translation appear.
'alternates' => SiteUrls::alternates($this->resource, 'page'),
'path' => SiteUrls::alternates($this->resource, 'page')[$locale] ?? null,
];
}
}