<?php

namespace App\Twig\Extension;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

class TruncateExtension extends AbstractExtension
{
    public function getFilters(): array
    {
        return [
            new TwigFilter('truncate', [$this, 'truncate']),
        ];
    }

    public function truncate(?string $value, int $limit): string
    {
        if ($value === null) {
            return '';
        }
        return strlen($value) > $limit ? substr($value, 0, $limit) . '...' : $value;
    }
}
