First version
This commit is contained in:
30
src/Twig/AppExtension.php
Normal file
30
src/Twig/AppExtension.php
Normal file
@ -0,0 +1,30 @@
|
||||
<?php
|
||||
|
||||
namespace App\Twig;
|
||||
|
||||
use Symfony\Component\DependencyInjection\ContainerInterface;
|
||||
use Twig\Extension\AbstractExtension;
|
||||
use Twig\TwigFilter;
|
||||
|
||||
class AppExtension extends AbstractExtension
|
||||
{
|
||||
public function getFilters()
|
||||
{
|
||||
return array(
|
||||
new TwigFilter('formatBytes', array($this, 'formatBytes')),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $bytes
|
||||
* @param int $precision
|
||||
* @return string
|
||||
*/
|
||||
public function formatBytes($bytes, $precision = 2)
|
||||
{
|
||||
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
|
||||
$factor = floor((strlen($bytes) - 1) / 3);
|
||||
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . @$size[$factor];
|
||||
}
|
||||
|
||||
}
|
Reference in New Issue
Block a user