Files
lucent-laravel/src/ViewModel/ViewModel.php
T

40 lines
1.0 KiB
PHP
Raw Normal View History

2024-08-14 22:04:34 +03:00
<?php
namespace Lucent\ViewModel;
use Lucent\Channel\ChannelService;
use Lucent\Record\QueryRecord;
use Lucent\Schema\CollectionSchema;
use Lucent\Schema\FieldInterface;
use Lucent\Schema\FilesSchema;
use Mustache_Engine;
class ViewModel
{
public function __construct(
public ChannelService $channelService
)
{
}
public function getRecordName(QueryRecord $record): string
{
$schema = $this->channelService->getSchema($record->schema)->get();
2024-12-18 13:02:09 +02:00
if (empty($schema->cardTitle)) {
2024-08-14 22:04:34 +03:00
$title = match (get_class($schema)) {
CollectionSchema::class => $record->data[$schema->fields->filter(fn(FieldInterface $f) => $f->info->name === "text")->first()->name],
FilesSchema::class => $record->_file->path,
};
if (empty(trim($title))) {
return "~Untitled~";
}
return $title;
}
$m = new Mustache_Engine(array('entity_flags' => ENT_QUOTES));
2024-08-19 17:48:10 +03:00
return $m->render($schema->cardTitle, $record->data);
2024-08-14 22:04:34 +03:00
}
}