1
0
cms11/app/Classes/Term.php

55 lines
1.4 KiB
PHP

<?php
namespace App\Classes;
use App\Models\Url;
use Illuminate\Support\Str;
class Term
{
protected Bundle $bundle;
protected string $slug;
protected string $path;
public function __construct(
protected string $term,
protected string $subCategory,
protected Bundle $source
) {
$this->slug = Str::slug($term);
$this->path = sprintf('/termes/%s', $this->slug);
$this->bundle = new Bundle($this->path, $source->getDisk());
$this->bundle->load();
$currentRelations = $this->bundle->metadata('relations')->get($subCategory, []);
if (!in_array($source->getPath(), $currentRelations)) {
$currentRelations[] = $source->getPath();
$this->bundle->metadata('relations')->set($subCategory, collect($currentRelations)->unique()->toArray());
}
if (empty($this->bundle->markdown()->get())) {
$this->bundle->markdown()->set('');
}
if (empty($this->bundle->metadata()->get('title'))) {
$this->bundle->metadata()->set('title', $term);
}
$this->bundle->save();
}
public function render()
{
return Url::from($this->path, $this->bundle)->toHtmlElement($this->term);
}
public static function register(string $term, string $subCategory, Bundle $source)
{
return new static($term, $subCategory, $source);
}
}