Browse Source

add formatBytes filter

main
ghost 1 year ago
parent
commit
c8ba485edc
  1. 29
      src/Twig/AppExtension.php

29
src/Twig/AppExtension.php

@ -31,6 +31,13 @@ class AppExtension extends AbstractExtension @@ -31,6 +31,13 @@ class AppExtension extends AbstractExtension
'formatAgo'
]
),
new TwigFilter(
'format_bytes',
[
$this,
'formatBytes'
]
),
new TwigFilter(
'message_to_markdown',
[
@ -140,6 +147,28 @@ class AppExtension extends AbstractExtension @@ -140,6 +147,28 @@ class AppExtension extends AbstractExtension
}
}
public function formatBytes(
int $bytes,
int $precision = 2
): string
{
$size = [
$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 messageToMarkdown(
string $text
): string

Loading…
Cancel
Save