mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Introduced a custom ExponentialNumberType for parameter values
This type shows small values in exponential notation instead of rounding it to zero.
This commit is contained in:
parent
1aee0a91c0
commit
21c3c45150
3 changed files with 152 additions and 3 deletions
|
@ -53,6 +53,7 @@ use App\Entity\Parameters\PartParameter;
|
|||
use App\Entity\Parameters\StorageLocationParameter;
|
||||
use App\Entity\Parameters\SupplierParameter;
|
||||
use App\Entity\Parts\MeasurementUnit;
|
||||
use App\Form\Type\ExponentialNumberType;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\TextType;
|
||||
|
@ -93,7 +94,7 @@ class ParameterType extends AbstractType
|
|||
],
|
||||
]);
|
||||
|
||||
$builder->add('value_max', NumberType::class, [
|
||||
$builder->add('value_max', ExponentialNumberType::class, [
|
||||
'label' => false,
|
||||
'required' => false,
|
||||
'html5' => true,
|
||||
|
@ -104,7 +105,7 @@ class ParameterType extends AbstractType
|
|||
'style' => 'max-width: 12ch;',
|
||||
],
|
||||
]);
|
||||
$builder->add('value_min', NumberType::class, [
|
||||
$builder->add('value_min', ExponentialNumberType::class, [
|
||||
'label' => false,
|
||||
'required' => false,
|
||||
'html5' => true,
|
||||
|
@ -115,7 +116,7 @@ class ParameterType extends AbstractType
|
|||
'style' => 'max-width: 12ch;',
|
||||
],
|
||||
]);
|
||||
$builder->add('value_typical', NumberType::class, [
|
||||
$builder->add('value_typical', ExponentialNumberType::class, [
|
||||
'label' => false,
|
||||
'required' => false,
|
||||
'html5' => true,
|
||||
|
|
52
src/Form/Type/ExponentialNumberType.php
Normal file
52
src/Form/Type/ExponentialNumberType.php
Normal file
|
@ -0,0 +1,52 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Form\Type;
|
||||
|
||||
use App\Form\Type\Helper\ExponentialNumberTransformer;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\NumberType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
|
||||
/**
|
||||
* Similar to the NumberType, but formats small values in scienfitic notation instead of rounding it to 0, like NumberType
|
||||
*/
|
||||
class ExponentialNumberType extends AbstractType
|
||||
{
|
||||
public function getParent(): string
|
||||
{
|
||||
return NumberType::class;
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options)
|
||||
{
|
||||
$builder->resetViewTransformers();
|
||||
|
||||
$builder->addViewTransformer(new ExponentialNumberTransformer(
|
||||
$options['scale'],
|
||||
$options['grouping'],
|
||||
$options['rounding_mode'],
|
||||
$options['html5'] ? 'en' : null
|
||||
));
|
||||
}
|
||||
}
|
96
src/Form/Type/Helper/ExponentialNumberTransformer.php
Normal file
96
src/Form/Type/Helper/ExponentialNumberTransformer.php
Normal file
|
@ -0,0 +1,96 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2024 Jan Böhmer (https://github.com/jbtronics)
|
||||
*
|
||||
* 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/>.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
|
||||
namespace App\Form\Type\Helper;
|
||||
|
||||
use App\Form\Fixes\FixedNumberToLocalizedStringTransformer;
|
||||
use Symfony\Component\Form\Exception\TransformationFailedException;
|
||||
|
||||
/**
|
||||
* This transformer formats small values in scienfitic notation instead of rounding it to 0, like the default
|
||||
* NumberFormatter.
|
||||
*/
|
||||
class ExponentialNumberTransformer extends FixedNumberToLocalizedStringTransformer
|
||||
{
|
||||
public function __construct(
|
||||
protected ?int $scale = null,
|
||||
?bool $grouping = false,
|
||||
?int $roundingMode = \NumberFormatter::ROUND_HALFUP,
|
||||
protected ?string $locale = null
|
||||
) {
|
||||
parent::__construct($scale, $grouping, $roundingMode, $locale);
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a number type into localized number.
|
||||
*
|
||||
* @param int|float|null $value Number value
|
||||
*
|
||||
* @throws TransformationFailedException if the given value is not numeric
|
||||
* or if the value cannot be transformed
|
||||
*/
|
||||
public function transform(mixed $value): string
|
||||
{
|
||||
if (null === $value) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if (!is_numeric($value)) {
|
||||
throw new TransformationFailedException('Expected a numeric.');
|
||||
}
|
||||
|
||||
//If the value is too small, the number formatter would return 0, therfore use exponential notation for small numbers
|
||||
if (abs($value) < 1e-3) {
|
||||
$formatter = $this->getScientificNumberFormatter();
|
||||
} else {
|
||||
$formatter = $this->getNumberFormatter();
|
||||
}
|
||||
|
||||
|
||||
|
||||
$value = $formatter->format($value);
|
||||
|
||||
if (intl_is_failure($formatter->getErrorCode())) {
|
||||
throw new TransformationFailedException($formatter->getErrorMessage());
|
||||
}
|
||||
|
||||
// Convert non-breaking and narrow non-breaking spaces to normal ones
|
||||
$value = str_replace(["\xc2\xa0", "\xe2\x80\xaf"], ' ', $value);
|
||||
|
||||
return $value;
|
||||
}
|
||||
|
||||
protected function getScientificNumberFormatter(): \NumberFormatter
|
||||
{
|
||||
$formatter = new \NumberFormatter($this->locale ?? \Locale::getDefault(), \NumberFormatter::SCIENTIFIC);
|
||||
|
||||
if (null !== $this->scale) {
|
||||
$formatter->setAttribute(\NumberFormatter::FRACTION_DIGITS, $this->scale);
|
||||
$formatter->setAttribute(\NumberFormatter::ROUNDING_MODE, $this->roundingMode);
|
||||
}
|
||||
|
||||
$formatter->setAttribute(\NumberFormatter::GROUPING_USED, (int) $this->grouping);
|
||||
|
||||
return $formatter;
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue