diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index 6eed1b13..e3921fd0 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -271,6 +271,7 @@ class Part extends AttachmentContainingDBElement /** * @var float|null How much a single part unit weighs in gramms. * @ORM\Column(type="float", nullable=true) + * @Assert\Positive() */ protected $mass; diff --git a/src/Services/SIFormatter.php b/src/Services/SIFormatter.php new file mode 100644 index 00000000..965fd2ee --- /dev/null +++ b/src/Services/SIFormatter.php @@ -0,0 +1,93 @@ += 0) { + $symbol = $prefixes_pos[$nearest]; + } else { + $symbol = $prefixes_neg[$nearest]; + } + + if ($magnitude < 0) { + $nearest *= -1; + } + + return [10 ** (3 * $nearest), $symbol]; + } + + /** + * @param float $value + * @param string $unit + * @param int $decimals + * @return string + */ + public function format(float $value, string $unit = '', int $decimals = 2) + { + [$divisor, $symbol] = $this->getPrefixByMagnitude($this->getMagnitude($value)); + $value /= $divisor; + //Build the format string, e.g.: %.2d km + $format_string = '%.' . $decimals . 'f ' . $symbol . $unit; + + return sprintf($format_string, $value); + } + +} \ No newline at end of file diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 549823eb..f348b342 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -33,6 +33,7 @@ use App\Entity\Attachments\Attachment; use App\Entity\Base\DBElement; use App\Services\EntityURLGenerator; use App\Services\MoneyFormatter; +use App\Services\SIFormatter; use App\Services\TreeBuilder; use Symfony\Component\Cache\Adapter\AdapterInterface; use Symfony\Component\Serializer\SerializerInterface; @@ -49,16 +50,19 @@ class AppExtension extends AbstractExtension protected $serializer; protected $treeBuilder; protected $moneyFormatter; + protected $siformatter; public function __construct(EntityURLGenerator $entityURLGenerator, AdapterInterface $cache, SerializerInterface $serializer, TreeBuilder $treeBuilder, - MoneyFormatter $moneyFormatter) + MoneyFormatter $moneyFormatter, + SIFormatter $SIFormatter) { $this->entityURLGenerator = $entityURLGenerator; $this->cache = $cache; $this->serializer = $serializer; $this->treeBuilder = $treeBuilder; $this->moneyFormatter = $moneyFormatter; + $this->siformatter = $SIFormatter; } public function getFilters() @@ -66,7 +70,8 @@ class AppExtension extends AbstractExtension return [ new TwigFilter('entityURL', [$this, 'generateEntityURL']), new TwigFilter('bbCode', [$this, 'parseBBCode'], ['pre_escape' => 'html', 'is_safe' => ['html']]), - new TwigFilter('moneyFormat', [$this, 'formatCurrency']) + new TwigFilter('moneyFormat', [$this, 'formatCurrency']), + new TwigFilter('siFormat', [$this, 'siFormat']), ]; } @@ -117,4 +122,9 @@ class AppExtension extends AbstractExtension { return $this->moneyFormatter->format($amount, $currency); } + + public function siFormat($value, $unit, $decimals = 2) + { + return $this->siformatter->format($value, $unit, $decimals); + } } diff --git a/templates/Parts/info/_sidebar.html.twig b/templates/Parts/info/_sidebar.html.twig index 3b1bfd48..b299ff73 100644 --- a/templates/Parts/info/_sidebar.html.twig +++ b/templates/Parts/info/_sidebar.html.twig @@ -10,6 +10,15 @@ +{# Part mass #} +{% if part.mass %} +