|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
|
4
|
|
|
* |
|
5
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics) |
|
6
|
|
|
* |
|
7
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
|
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
|
10
|
|
|
* (at your option) any later version. |
|
11
|
|
|
* |
|
12
|
|
|
* This program is distributed in the hope that it will be useful, |
|
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
15
|
|
|
* GNU Affero General Public License for more details. |
|
16
|
|
|
* |
|
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
|
19
|
|
|
*/ |
|
20
|
|
|
|
|
21
|
|
|
declare(strict_types=1); |
|
22
|
|
|
|
|
23
|
|
|
namespace App\Twig; |
|
24
|
|
|
|
|
25
|
|
|
use App\Entity\Attachments\Attachment; |
|
26
|
|
|
use App\Entity\Base\AbstractDBElement; |
|
27
|
|
|
use App\Entity\ProjectSystem\Project; |
|
28
|
|
|
use App\Entity\LabelSystem\LabelProfile; |
|
29
|
|
|
use App\Entity\Parts\Category; |
|
30
|
|
|
use App\Entity\Parts\Footprint; |
|
31
|
|
|
use App\Entity\Parts\Manufacturer; |
|
32
|
|
|
use App\Entity\Parts\MeasurementUnit; |
|
33
|
|
|
use App\Entity\Parts\Part; |
|
34
|
|
|
use App\Entity\Parts\Storelocation; |
|
35
|
|
|
use App\Entity\Parts\Supplier; |
|
36
|
|
|
use App\Entity\PriceInformations\Currency; |
|
37
|
|
|
use App\Entity\UserSystem\Group; |
|
38
|
|
|
use App\Entity\UserSystem\User; |
|
39
|
|
|
use App\Services\Formatters\AmountFormatter; |
|
40
|
|
|
use App\Services\Attachments\AttachmentURLGenerator; |
|
41
|
|
|
use App\Services\EntityURLGenerator; |
|
42
|
|
|
use App\Services\Misc\FAIconGenerator; |
|
43
|
|
|
use App\Services\Formatters\MarkdownParser; |
|
44
|
|
|
use App\Services\Formatters\MoneyFormatter; |
|
45
|
|
|
use App\Services\Formatters\SIFormatter; |
|
46
|
|
|
use App\Services\Trees\TreeViewGenerator; |
|
47
|
|
|
use Brick\Math\BigDecimal; |
|
48
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer; |
|
49
|
|
|
use Symfony\Component\Serializer\SerializerInterface; |
|
50
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
|
51
|
|
|
use Twig\Extension\AbstractExtension; |
|
52
|
|
|
use Twig\TwigFilter; |
|
53
|
|
|
use Twig\TwigFunction; |
|
54
|
|
|
use Twig\TwigTest; |
|
55
|
|
|
|
|
56
|
|
|
use function get_class; |
|
57
|
|
|
|
|
58
|
|
|
final class FormatExtension extends AbstractExtension |
|
59
|
|
|
{ |
|
60
|
|
|
protected MarkdownParser $markdownParser; |
|
61
|
|
|
protected MoneyFormatter $moneyFormatter; |
|
62
|
|
|
protected SIFormatter $siformatter; |
|
63
|
|
|
protected AmountFormatter $amountFormatter; |
|
64
|
|
|
|
|
65
|
|
|
|
|
66
|
|
|
public function __construct(MarkdownParser $markdownParser, MoneyFormatter $moneyFormatter, |
|
67
|
|
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter) |
|
68
|
|
|
{ |
|
69
|
|
|
$this->markdownParser = $markdownParser; |
|
70
|
|
|
$this->moneyFormatter = $moneyFormatter; |
|
71
|
|
|
$this->siformatter = $SIFormatter; |
|
72
|
|
|
$this->amountFormatter = $amountFormatter; |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
public function getFilters(): array |
|
76
|
|
|
{ |
|
77
|
|
|
return [ |
|
78
|
|
|
/* Mark the given text as markdown, which will be rendered in the browser */ |
|
79
|
|
|
new TwigFilter('format_markdown', [$this->markdownParser, 'markForRendering'], [ |
|
80
|
|
|
'pre_escape' => 'html', |
|
81
|
|
|
'is_safe' => ['html'], |
|
82
|
|
|
]), |
|
83
|
|
|
/* Format the given amount as money, using a given currency */ |
|
84
|
|
|
new TwigFilter('format_money', [$this, 'formatCurrency']), |
|
85
|
|
|
/* Format the given number using SI prefixes and the given unit (string) */ |
|
86
|
|
|
new TwigFilter('format_si', [$this, 'siFormat']), |
|
87
|
|
|
/** Format the given amount using the given MeasurementUnit */ |
|
88
|
|
|
new TwigFilter('format_amount', [$this, 'amountFormat']), |
|
89
|
|
|
/** Format the given number of bytes as human readable number */ |
|
90
|
|
|
new TwigFilter('format_bytes', [$this, 'formatBytes']), |
|
91
|
|
|
]; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5): string |
|
95
|
|
|
{ |
|
96
|
|
|
if ($amount instanceof BigDecimal) { |
|
97
|
|
|
$amount = (string) $amount; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->moneyFormatter->format($amount, $currency, $decimals); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
public function siFormat($value, $unit, $decimals = 2, bool $show_all_digits = false): string |
|
|
|
|
|
|
104
|
|
|
{ |
|
105
|
|
|
return $this->siformatter->format($value, $unit, $decimals); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = []): string |
|
109
|
|
|
{ |
|
110
|
|
|
return $this->amountFormatter->format($value, $unit, $options); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @param $bytes |
|
115
|
|
|
* @param int $precision |
|
116
|
|
|
* @return string |
|
117
|
|
|
*/ |
|
118
|
|
|
public function formatBytes(int $bytes, int $precision = 2): string |
|
119
|
|
|
{ |
|
120
|
|
|
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB']; |
|
121
|
|
|
$factor = floor((strlen((string) $bytes) - 1) / 3); |
|
122
|
|
|
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor]; |
|
123
|
|
|
} |
|
124
|
|
|
} |
|
125
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.