More tests and refactoring
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
2023-06-15 14:10:17 +00:00
parent 72058335ca
commit f41571cfd2
9 changed files with 92 additions and 38 deletions

View File

@@ -0,0 +1,42 @@
<?php
namespace App\Template;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;
/**
* Twig extension
*
* Additional formatters for templates
*/
class TwigExtension extends AbstractExtension
{
/**
* Extend the filters
*
* @return TwigFilter[]
*/
public function getFilters(): array
{
return array(
new TwigFilter('formatBytes', array($this, 'formatBytes')),
);
}
/**
* Format a file size to be human readable
*
* @param int $bytes Number of bytes
* @param int $precision Precision
*
* @return string Formatted string
*/
public function formatBytes($bytes, $precision = 2)
{
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
$fact = (int)(floor((strlen((string)$bytes) - 1) / 3));
return sprintf("%.{$precision}f", $bytes / pow(1024, $fact)) . $size[$fact];
}
}