50 lines
1.3 KiB
PHP
50 lines
1.3 KiB
PHP
|
<?php
|
||
|
|
||
|
namespace App\Classes;
|
||
|
|
||
|
use Illuminate\Support\Str;
|
||
|
|
||
|
class Term
|
||
|
{
|
||
|
protected Bundle $bundle;
|
||
|
|
||
|
protected string $slug;
|
||
|
|
||
|
protected Link $link;
|
||
|
|
||
|
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->link = new Link($this->path, $term, $source);
|
||
|
|
||
|
$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());
|
||
|
}
|
||
|
|
||
|
$this->bundle->markdown()->set('');
|
||
|
$this->bundle->metadata()->set('title', $term);
|
||
|
|
||
|
$this->bundle->save();
|
||
|
}
|
||
|
|
||
|
public function render()
|
||
|
{
|
||
|
return (string) $this->link->toHtmlElement();
|
||
|
}
|
||
|
|
||
|
public static function register(string $term, string $subCategory, Bundle $source)
|
||
|
{
|
||
|
return new static($term, $subCategory, $source);
|
||
|
}
|
||
|
}
|