2019-08-26 15:09:05 +02: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-08-26 15:09:05 +02:00
|
|
|
namespace App\Services;
|
|
|
|
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
2020-01-05 22:49:00 +01:00
|
|
|
use InvalidArgumentException;
|
2019-08-26 15:09:05 +02:00
|
|
|
use Symfony\Component\OptionsResolver\Options;
|
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This service formats an part amout using a Measurement Unit.
|
|
|
|
*/
|
|
|
|
class AmountFormatter
|
|
|
|
{
|
2022-09-18 22:59:31 +02:00
|
|
|
protected SIFormatter $siFormatter;
|
2019-08-26 15:09:05 +02:00
|
|
|
|
|
|
|
public function __construct(SIFormatter $siFormatter)
|
|
|
|
{
|
|
|
|
$this->siFormatter = $siFormatter;
|
|
|
|
}
|
|
|
|
|
2020-01-05 15:46:58 +01:00
|
|
|
/**
|
2020-03-29 23:13:25 +02:00
|
|
|
* Formats the given value using the measurement unit and options.
|
|
|
|
*
|
2020-05-10 21:39:31 +02:00
|
|
|
* @param float|string|int $value
|
|
|
|
* @param MeasurementUnit|null $unit The measurement unit, whose unit symbol should be used for formatting.
|
|
|
|
* If set to null, it is assumed that the part amount is measured in pieces.
|
2020-01-05 15:46:58 +01:00
|
|
|
*
|
|
|
|
* @return string The formatted string
|
|
|
|
*
|
2020-01-05 22:49:00 +01:00
|
|
|
* @throws InvalidArgumentException thrown if $value is not numeric
|
2020-01-05 15:46:58 +01:00
|
|
|
*/
|
2020-08-21 22:43:37 +02:00
|
|
|
public function format($value, ?MeasurementUnit $unit = null, array $options = []): string
|
2020-01-05 15:46:58 +01:00
|
|
|
{
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!is_numeric($value)) {
|
2020-01-05 22:49:00 +01:00
|
|
|
throw new InvalidArgumentException('$value must be an numeric value!');
|
2020-01-05 15:46:58 +01:00
|
|
|
}
|
|
|
|
$value = (float) $value;
|
|
|
|
|
|
|
|
//Find out what options to use
|
|
|
|
$resolver = new OptionsResolver();
|
|
|
|
$resolver->setDefault('measurement_unit', $unit);
|
|
|
|
$this->configureOptions($resolver);
|
|
|
|
|
|
|
|
$options = $resolver->resolve($options);
|
|
|
|
|
|
|
|
if ($options['is_integer']) {
|
|
|
|
$value = round($value);
|
|
|
|
}
|
|
|
|
|
|
|
|
//If the measurement unit uses a SI prefix format it that way.
|
|
|
|
if ($options['show_prefix']) {
|
|
|
|
return $this->siFormatter->format($value, $options['unit'], $options['decimals']);
|
|
|
|
}
|
|
|
|
|
|
|
|
//Otherwise just output it
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($options['unit'])) {
|
2020-01-05 15:46:58 +01:00
|
|
|
$format_string = '%.'.$options['decimals'].'f '.$options['unit'];
|
|
|
|
} else { //Dont add space after number if no unit was specified
|
|
|
|
$format_string = '%.'.$options['decimals'].'f';
|
|
|
|
}
|
|
|
|
|
|
|
|
return sprintf($format_string, $value);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function configureOptions(OptionsResolver $resolver): void
|
2019-08-26 15:09:05 +02:00
|
|
|
{
|
|
|
|
$resolver->setDefaults([
|
2020-08-21 22:43:37 +02:00
|
|
|
'show_prefix' => static function (Options $options) {
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $options['measurement_unit']) {
|
2019-08-26 15:09:05 +02:00
|
|
|
/** @var MeasurementUnit $unit */
|
|
|
|
$unit = $options['measurement_unit'];
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 15:09:05 +02:00
|
|
|
return $unit->isUseSIPrefix();
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-09-08 18:45:39 +02:00
|
|
|
return false;
|
2019-08-26 15:09:05 +02:00
|
|
|
},
|
2020-08-21 22:43:37 +02:00
|
|
|
'is_integer' => static function (Options $options) {
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $options['measurement_unit']) {
|
2019-08-26 15:09:05 +02:00
|
|
|
/** @var MeasurementUnit $unit */
|
|
|
|
$unit = $options['measurement_unit'];
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 15:09:05 +02:00
|
|
|
return $unit->isInteger();
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 15:09:05 +02:00
|
|
|
return true;
|
|
|
|
},
|
2020-08-21 22:43:37 +02:00
|
|
|
'unit' => static function (Options $options) {
|
2019-11-09 00:47:20 +01:00
|
|
|
if (null !== $options['measurement_unit']) {
|
2019-08-26 15:09:05 +02:00
|
|
|
/** @var MeasurementUnit $unit */
|
|
|
|
$unit = $options['measurement_unit'];
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2022-09-17 17:58:01 +02:00
|
|
|
//When no unit is set, return empty string so that this can be formatted properly
|
|
|
|
return $unit->getUnit() ?? '';
|
2019-08-26 15:09:05 +02:00
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 15:09:05 +02:00
|
|
|
return '';
|
|
|
|
},
|
|
|
|
'decimals' => 2,
|
2020-01-05 22:49:00 +01:00
|
|
|
'error_mapping' => [
|
|
|
|
'.' => 'value',
|
|
|
|
],
|
2019-08-26 15:09:05 +02:00
|
|
|
]);
|
|
|
|
|
|
|
|
$resolver->setAllowedTypes('decimals', 'int');
|
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
$resolver->setNormalizer('decimals', static function (Options $options, $value) {
|
2019-08-26 15:09:05 +02:00
|
|
|
// If the unit is integer based, then dont show any decimals
|
|
|
|
if ($options['is_integer']) {
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
|
2019-08-26 15:09:05 +02:00
|
|
|
return $value;
|
|
|
|
});
|
|
|
|
}
|
2019-11-09 00:47:20 +01:00
|
|
|
}
|