Exponential 5.x Kernel 7.5.40 Exponential New Stack (Symfony-based) Kernel — PHP Class Reference

FileSizeExtension.php
Go to the documentation of this file.
1 <?php
2
7 namespace eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension;
8
9 use Locale;
10 use NumberFormatter;
11 use Twig_Extension;
12 use Twig_SimpleFilter;
13 use Symfony\Component\Translation\TranslatorInterface;
14 use eZ\Publish\Core\MVC\ConfigResolverInterface;
15 use eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface;
16
20 class FileSizeExtension extends Twig_Extension
21 {
25 protected $translator;
26
30 protected $suffixes;
31
35 protected $configResolver;
36
40 protected $localeConverter;
41
48 public function __construct(TranslatorInterface $translator, array $suffixes, ConfigResolverInterface $configResolver, LocaleConverterInterface $localeConverter)
49 {
50 $this->translator = $translator;
51 $this->suffixes = $suffixes;
52 $this->configResolver = $configResolver;
53 $this->localeConverter = $localeConverter;
54 }
55
56 private function getLocale()
57 {
58 foreach ($this->configResolver->getParameter('languages') as $locale) {
59 $convertedLocale = $this->localeConverter->convertToPOSIX($locale);
60 if ($convertedLocale !== null) {
61 return $convertedLocale;
62 }
63 }
64
65 return Locale::getDefault();
66 }
67
73 public function getFilters()
74 {
75 return [
76 new Twig_SimpleFilter('ez_file_size', [$this, 'sizeFilter']),
77 ];
78 }
79
89 public function sizeFilter($number, $precision)
90 {
91 $mod = 1024;
92 $index = count($this->suffixes);
93 if ($number < ($mod ** $index)) {
94 for ($i = 0; $number >= $mod; ++$i) {
95 $number /= $mod;
96 }
97 } else {
98 $number /= $mod ** ($index - 1);
99 $i = ($index - 1);
100 }
101 $formatter = new NumberFormatter($this->getLocale(), NumberFormatter::PATTERN_DECIMAL);
102 $formatter->setPattern($formatter->getPattern() . ' ' . $this->translator->trans($this->suffixes[$i]));
103 $formatter->setAttribute(NumberFormatter::MAX_FRACTION_DIGITS, $precision);
104
105 return $formatter->format($number);
106 }
107
113 public function getName()
114 {
115 return 'ezpublish.fileSize';
116 }
117 }
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension
Class FileSizeExtension.
Definition: FileSizeExtension.php:21
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\$configResolver
$configResolver
Definition: FileSizeExtension.php:35
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\getFilters
getFilters()
Returns a list of filters to add to the existing list.
Definition: FileSizeExtension.php:73
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\$localeConverter
$localeConverter
Definition: FileSizeExtension.php:40
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\getLocale
getLocale()
Definition: FileSizeExtension.php:56
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\$suffixes
$suffixes
Definition: FileSizeExtension.php:30
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\$translator
$translator
Definition: FileSizeExtension.php:25
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\getName
getName()
Returns the name of the extension.
Definition: FileSizeExtension.php:113
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\sizeFilter
sizeFilter($number, $precision)
Returns the binary file size, $precision will determine the decimal number precision,...
Definition: FileSizeExtension.php:89
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension\FileSizeExtension\__construct
__construct(TranslatorInterface $translator, array $suffixes, ConfigResolverInterface $configResolver, LocaleConverterInterface $localeConverter)
Definition: FileSizeExtension.php:48
eZ\Publish\Core\MVC\ConfigResolverInterface
Interface for config resolvers.
Definition: ConfigResolverInterface.php:19
eZ\Publish\Core\MVC\Symfony\Locale\LocaleConverterInterface
Interface for locale converters.
Definition: LocaleConverterInterface.php:17
eZ\Publish\Core\MVC\Symfony\Templating\Twig\Extension
Definition: ContentExtension.php:7
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.