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>
92 lines
3.6 KiB
PHP
92 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Http\Resources\PublicApi;
|
|
|
|
use App\Models\Page;
|
|
use App\Models\Tag;
|
|
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')),
|
|
|
|
// Only the tags named in this language: one without a slug here
|
|
// has no listing to link to.
|
|
'tags' => $this->whenLoaded('tags', fn () => $this->tags
|
|
->filter(fn (Tag $tag) => filled($tag->getTranslation('slug', $locale, false)))
|
|
->map(fn (Tag $tag) => [
|
|
'name' => $tag->getTranslation('name', $locale),
|
|
'slug' => $tag->getTranslation('slug', $locale, false),
|
|
])
|
|
->values()),
|
|
|
|
// Who signs it — only someone with a public profile to link to.
|
|
'byline' => $this->whenLoaded('byline', fn () => $this->byline?->is_public ? [
|
|
'name' => $this->byline->name,
|
|
'slug' => $this->byline->slug,
|
|
'headline' => $this->byline->getTranslation('headline', $locale),
|
|
'avatar' => ImagePayload::make(
|
|
$this->byline->avatar,
|
|
$this->byline->relationLoaded('avatarMedia') ? $this->byline->getRelation('avatarMedia') : null,
|
|
$this->byline->name,
|
|
),
|
|
'path' => SiteUrls::path('member', $locale, $this->byline->slug),
|
|
] : null),
|
|
|
|
// 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,
|
|
];
|
|
}
|
|
}
|