Browse Source

add formatBytes filter

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

29
src/Twig/AppExtension.php

@ -31,6 +31,13 @@ class AppExtension extends AbstractExtension
'formatAgo' 'formatAgo'
] ]
), ),
new TwigFilter(
'format_bytes',
[
$this,
'formatBytes'
]
),
new TwigFilter( new TwigFilter(
'message_to_markdown', 'message_to_markdown',
[ [
@ -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( public function messageToMarkdown(
string $text string $text
): string ): string

Loading…
Cancel
Save