2019-03-13 18:41:32 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-01-05 15:46:58 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2019-03-13 18:41:32 +01:00
|
|
|
namespace App\Twig;
|
|
|
|
|
2022-09-04 23:02:31 +02:00
|
|
|
use App\Entity\Attachments\Attachment;
|
2020-02-01 19:48:07 +01:00
|
|
|
use App\Entity\Base\AbstractDBElement;
|
2022-12-18 20:34:25 +01:00
|
|
|
use App\Entity\ProjectSystem\Project;
|
2022-09-04 23:02:31 +02:00
|
|
|
use App\Entity\LabelSystem\LabelProfile;
|
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
2019-08-26 15:09:05 +02:00
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
2022-09-04 23:02:31 +02:00
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
use App\Entity\Parts\Supplier;
|
2019-09-01 13:56:14 +02:00
|
|
|
use App\Entity\PriceInformations\Currency;
|
2022-09-04 23:02:31 +02:00
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
use App\Entity\UserSystem\User;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\Formatters\AmountFormatter;
|
2019-10-06 15:44:19 +02:00
|
|
|
use App\Services\Attachments\AttachmentURLGenerator;
|
2019-03-13 18:41:32 +01:00
|
|
|
use App\Services\EntityURLGenerator;
|
2022-12-18 17:28:42 +01:00
|
|
|
use App\Services\Misc\FAIconGenerator;
|
|
|
|
use App\Services\Formatters\MarkdownParser;
|
|
|
|
use App\Services\Formatters\MoneyFormatter;
|
|
|
|
use App\Services\Formatters\SIFormatter;
|
2020-01-02 22:55:28 +01:00
|
|
|
use App\Services\Trees\TreeViewGenerator;
|
2020-05-20 22:02:07 +02:00
|
|
|
use Brick\Math\BigDecimal;
|
2022-09-10 00:08:59 +02:00
|
|
|
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
|
2019-04-05 17:49:02 +02:00
|
|
|
use Symfony\Component\Serializer\SerializerInterface;
|
2019-12-29 20:04:52 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
2019-03-13 18:41:32 +01:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
use Twig\TwigFilter;
|
2019-04-05 17:49:02 +02:00
|
|
|
use Twig\TwigFunction;
|
2019-04-28 14:18:11 +02:00
|
|
|
use Twig\TwigTest;
|
2019-03-13 20:14:19 +01:00
|
|
|
|
2022-08-14 19:32:53 +02:00
|
|
|
use function get_class;
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
final class FormatExtension extends AbstractExtension
|
2019-03-13 18:41:32 +01:00
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected MarkdownParser $markdownParser;
|
|
|
|
protected MoneyFormatter $moneyFormatter;
|
|
|
|
protected SIFormatter $siformatter;
|
|
|
|
protected AmountFormatter $amountFormatter;
|
2019-03-13 18:41:32 +01:00
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
|
|
|
|
public function __construct(MarkdownParser $markdownParser, MoneyFormatter $moneyFormatter,
|
|
|
|
SIFormatter $SIFormatter, AmountFormatter $amountFormatter)
|
2019-03-13 18:41:32 +01:00
|
|
|
{
|
2019-10-11 23:53:12 +02:00
|
|
|
$this->markdownParser = $markdownParser;
|
2019-08-02 12:17:56 +02:00
|
|
|
$this->moneyFormatter = $moneyFormatter;
|
2019-08-16 16:43:31 +02:00
|
|
|
$this->siformatter = $SIFormatter;
|
2019-08-26 15:09:05 +02:00
|
|
|
$this->amountFormatter = $amountFormatter;
|
2019-03-13 18:41:32 +01:00
|
|
|
}
|
|
|
|
|
2022-08-14 19:09:07 +02:00
|
|
|
public function getFilters(): array
|
2019-03-13 18:41:32 +01:00
|
|
|
{
|
2019-03-20 23:16:07 +01:00
|
|
|
return [
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Mark the given text as markdown, which will be rendered in the browser */
|
|
|
|
new TwigFilter('format_markdown', [$this->markdownParser, 'markForRendering'], [
|
2020-01-05 22:49:00 +01:00
|
|
|
'pre_escape' => 'html',
|
|
|
|
'is_safe' => ['html'],
|
|
|
|
]),
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Format the given amount as money, using a given currency */
|
|
|
|
new TwigFilter('format_money', [$this, 'formatCurrency']),
|
|
|
|
/* Format the given number using SI prefixes and the given unit (string) */
|
|
|
|
new TwigFilter('format_si', [$this, 'siFormat']),
|
|
|
|
/** Format the given amount using the given MeasurementUnit */
|
|
|
|
new TwigFilter('format_amount', [$this, 'amountFormat']),
|
2023-01-16 00:06:14 +01:00
|
|
|
/** Format the given number of bytes as human readable number */
|
|
|
|
new TwigFilter('format_bytes', [$this, 'formatBytes']),
|
2019-04-05 17:49:02 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5): string
|
2019-08-02 12:17:56 +02:00
|
|
|
{
|
2020-05-20 22:02:07 +02:00
|
|
|
if ($amount instanceof BigDecimal) {
|
|
|
|
$amount = (string) $amount;
|
|
|
|
}
|
|
|
|
|
2019-09-01 13:56:14 +02:00
|
|
|
return $this->moneyFormatter->format($amount, $currency, $decimals);
|
2019-08-02 12:17:56 +02:00
|
|
|
}
|
2019-08-16 16:43:31 +02:00
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
public function siFormat($value, $unit, $decimals = 2, bool $show_all_digits = false): string
|
2019-08-16 16:43:31 +02:00
|
|
|
{
|
2020-08-21 22:43:37 +02:00
|
|
|
return $this->siformatter->format($value, $unit, $decimals);
|
2019-08-16 16:43:31 +02:00
|
|
|
}
|
2019-08-26 15:09:05 +02:00
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
public function amountFormat($value, ?MeasurementUnit $unit, array $options = []): string
|
2019-08-26 15:09:05 +02:00
|
|
|
{
|
|
|
|
return $this->amountFormatter->format($value, $unit, $options);
|
|
|
|
}
|
2023-01-16 00:06:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $bytes
|
|
|
|
* @param int $precision
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function formatBytes(int $bytes, int $precision = 2): string
|
|
|
|
{
|
|
|
|
$size = ['B','kB','MB','GB','TB','PB','EB','ZB','YB'];
|
|
|
|
$factor = floor((strlen((string) $bytes) - 1) / 3);
|
|
|
|
return sprintf("%.{$precision}f", $bytes / pow(1024, $factor)) . ' ' . @$size[$factor];
|
|
|
|
}
|
2019-03-20 23:16:07 +01:00
|
|
|
}
|