add format_ago filter, remove time service

This commit is contained in:
ghost 2023-10-07 22:14:57 +03:00
parent e772955eb2
commit df253ba1b0
6 changed files with 78 additions and 91 deletions

View file

@ -3,18 +3,22 @@
namespace App\Twig;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
class AppExtension extends AbstractExtension
{
protected $container;
protected ContainerInterface $container;
protected TranslatorInterface $translator;
public function __construct(
ContainerInterface $container
ContainerInterface $container,
TranslatorInterface $translator
)
{
$this->container = $container;
$this->translator = $translator;
}
public function getFilters()
@ -28,6 +32,13 @@ class AppExtension extends AbstractExtension
'formatBytes'
]
),
new TwigFilter(
'format_ago',
[
$this,
'formatAgo'
]
),
];
}
@ -37,19 +48,67 @@ class AppExtension extends AbstractExtension
) : string
{
$size = [
'B',
'Kb',
'Mb',
'Gb',
'Tb',
'Pb',
'Eb',
'Zb',
'Yb'
$this->translator->trans('B'),
$this->translator->trans('Kb'),
$this->translator->trans('Mb'),
$this->translator->trans('Gb'),
$this->translator->trans('Tb'),
$this->translator->trans('Pb'),
$this->translator->trans('Eb'),
$this->translator->trans('Zb'),
$this->translator->trans('Yb')
];
$factor = floor((strlen($bytes) - 1) / 3);
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
}
public function formatAgo(
int $time,
) : string
{
$diff = time() - $time;
if ($diff < 1)
{
return $this->translator->trans('now');
}
$values =
[
365 * 24 * 60 * 60 => $this->translator->trans('year'),
30 * 24 * 60 * 60 => $this->translator->trans('month'),
24 * 60 * 60 => $this->translator->trans('day'),
60 * 60 => $this->translator->trans('hour'),
60 => $this->translator->trans('minute'),
1 => $this->translator->trans('second')
];
$plural = [
$this->translator->trans('year') => $this->translator->trans('years'),
$this->translator->trans('month') => $this->translator->trans('months'),
$this->translator->trans('day') => $this->translator->trans('days'),
$this->translator->trans('hour') => $this->translator->trans('hours'),
$this->translator->trans('minute') => $this->translator->trans('minutes'),
$this->translator->trans('second') => $this->translator->trans('seconds')
];
foreach ($values as $key => $value)
{
$result = $diff / $key;
if ($result >= 1)
{
$round = round($result);
return sprintf(
'%s %s %s',
$round,
$round > 1 ? $plural[$value] : $value,
$this->translator->trans('ago')
);
}
}
}
}