generated from boboko/starter
71 lines
2.1 KiB
PHP
71 lines
2.1 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use Spatie\YamlFrontMatter\YamlFrontMatter;
|
|
|
|
/**
|
|
* Site-wide, marketing-editable settings from Stoic's `settings` singleton
|
|
* (resources/stoic/content/settings.md). Read directly rather than through a
|
|
* Paper model: Paper models map to a folder of entries, and this is one file
|
|
* at the content root. Legal and transactional details (address, ΑΦΜ, phone)
|
|
* live in boboko-core's StoreDetails instead.
|
|
*
|
|
* Bound as scoped in AppServiceProvider, so the file is parsed once per
|
|
* request (or queued job) and edits in Stoic show up on the next one.
|
|
*/
|
|
class StoicSettings
|
|
{
|
|
private const SOCIALS = [
|
|
'facebook' => 'Facebook',
|
|
'instagram' => 'Instagram',
|
|
'tiktok' => 'TikTok',
|
|
];
|
|
|
|
private array $data;
|
|
|
|
public function __construct()
|
|
{
|
|
$path = resource_path('stoic/content/settings.md');
|
|
|
|
$this->data = is_file($path)
|
|
? YamlFrontMatter::parse(file_get_contents($path))->matter()
|
|
: [];
|
|
}
|
|
|
|
public function get(string $key, mixed $default = null): mixed
|
|
{
|
|
return filled($this->data[$key] ?? null) ? $this->data[$key] : $default;
|
|
}
|
|
|
|
/**
|
|
* Absolute og:image URL for a Stoic image field value (the page's own, else
|
|
* this file's default), or null. Image fields store a comma-separated list;
|
|
* only the first is used.
|
|
*/
|
|
public function ogImageUrl(?string $pageImage = null): ?string
|
|
{
|
|
$filename = trim(explode(',', $pageImage ?: (string) $this->get('og_image', ''))[0]);
|
|
|
|
return $filename === '' ? null : url(Stoic::image($filename, 'large'));
|
|
}
|
|
|
|
/**
|
|
* Only the networks that have a URL set, in display order.
|
|
*
|
|
* @return list<array{icon: string, label: string, url: string}>
|
|
*/
|
|
public function socialLinks(): array
|
|
{
|
|
$links = [];
|
|
|
|
foreach (self::SOCIALS as $icon => $label) {
|
|
if ($url = $this->get("{$icon}_url")) {
|
|
$links[] = ['icon' => $icon, 'label' => $label, 'url' => $url];
|
|
}
|
|
}
|
|
|
|
return $links;
|
|
}
|
|
}
|