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

64 lines
2.4 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')),
'byline_member_id' => $this->byline_member_id,
'tag_ids' => $this->whenLoaded('tags', fn () => $this->tags->pluck('id')->values()),
// 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),
),
];
}
}