mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Show part mass in part info page.
This commit is contained in:
parent
aafbda3376
commit
c2b43f2cfa
7 changed files with 210 additions and 6 deletions
|
@ -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;
|
||||
|
||||
|
|
93
src/Services/SIFormatter.php
Normal file
93
src/Services/SIFormatter.php
Normal file
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 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 General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
/**
|
||||
* A service that helps you to format values using the SI prefixes.
|
||||
* @package App\Services
|
||||
*/
|
||||
class SIFormatter
|
||||
{
|
||||
/**
|
||||
* Returns the magnitude of a value (the count of decimal place of the highest decimal place).
|
||||
* For example, for 100 (=10^2) this function returns 2. For -2500 (=-2.5*10^3) this function returns 3.
|
||||
* @param float $value The value of which the magnitude should be determined.
|
||||
* @return int The magnitude of the value
|
||||
*/
|
||||
public function getMagnitude(float $value) : int
|
||||
{
|
||||
return (int) floor(log10(abs($value)));
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the best SI prefix (and its corresponding divisor) for the given magnitude.
|
||||
* @param int $magnitude The magnitude for which the prefix should be determined.
|
||||
* @return array A array, containing the divisor in first element, and the prefix symbol in second. For example, [1000, "k"].
|
||||
*/
|
||||
public function getPrefixByMagnitude(int $magnitude) : array
|
||||
{
|
||||
$prefixes_pos = ['' ,'k', 'M', 'G', 'T', 'P', 'E', 'Z', 'Y'];
|
||||
$prefixes_neg = ['' ,'m', 'μ', 'n', 'p', 'f', 'a', 'z', 'y'];
|
||||
|
||||
//Determine nearest prefix index.
|
||||
$nearest = (int) round(abs($magnitude) / 3);
|
||||
|
||||
if ($magnitude >= 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);
|
||||
}
|
||||
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,15 @@
|
|||
</span>
|
||||
</div>
|
||||
|
||||
{# Part mass #}
|
||||
{% if part.mass %}
|
||||
<div>
|
||||
<h6>
|
||||
<span class="badge badge-secondary" title="{% trans %}part.mass.tooltip{% endtrans %}"><i class="fas fa-weight-hanging fa-fw"></i> {{ part.mass | siFormat("g") }}</span>
|
||||
</h6>
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
{# Needs Review tag #}
|
||||
{% if part.needsReview %}
|
||||
<div class="mt-1">
|
||||
|
|
|
@ -31,7 +31,7 @@
|
|||
|
||||
namespace App\Tests\Entity;
|
||||
|
||||
use App\Entity\PermissionsEmbed;
|
||||
use App\Entity\UserSystem\PermissionsEmbed;
|
||||
use Doctrine\ORM\Mapping\Embedded;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
|
|
|
@ -32,9 +32,9 @@
|
|||
namespace App\Tests\Services;
|
||||
|
||||
|
||||
use App\Entity\Group;
|
||||
use App\Entity\PermissionsEmbed;
|
||||
use App\Entity\User;
|
||||
use App\Entity\UserSystem\Group;
|
||||
use App\Entity\UserSystem\PermissionsEmbed;
|
||||
use App\Entity\UserSystem\User;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use App\Services\PermissionResolver;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
|
91
tests/Services/SIFormatterTest.php
Normal file
91
tests/Services/SIFormatterTest.php
Normal file
|
@ -0,0 +1,91 @@
|
|||
<?php
|
||||
/**
|
||||
*
|
||||
* part-db version 0.1
|
||||
* Copyright (C) 2005 Christoph Lechner
|
||||
* http://www.cl-projects.de/
|
||||
*
|
||||
* part-db version 0.2+
|
||||
* Copyright (C) 2009 K. Jacobs and others (see authors.php)
|
||||
* http://code.google.com/p/part-db/
|
||||
*
|
||||
* Part-DB Version 0.4+
|
||||
* Copyright (C) 2016 - 2019 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 General Public License
|
||||
* as published by the Free Software Foundation; either version 2
|
||||
* 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 General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
||||
*
|
||||
*/
|
||||
|
||||
namespace App\Tests\Services;
|
||||
|
||||
use App\Services\SIFormatter;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
|
||||
|
||||
class SIFormatterTest extends WebTestCase
|
||||
{
|
||||
/**
|
||||
* @var SIFormatter
|
||||
*/
|
||||
protected $service;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
//Get an service instance.
|
||||
self::bootKernel();
|
||||
//$this->service = self::$container->get(SIFormatter::class);
|
||||
|
||||
$this->service = new SIFormatter();
|
||||
}
|
||||
|
||||
public function testGetMagnitude()
|
||||
{
|
||||
//Get an service instance.
|
||||
$this->assertSame(0, $this->service->getMagnitude(7.0));
|
||||
$this->assertSame(0, $this->service->getMagnitude(9.0));
|
||||
$this->assertSame(0, $this->service->getMagnitude(0.0));
|
||||
$this->assertSame(0, $this->service->getMagnitude(-1.0));
|
||||
$this->assertSame(0, $this->service->getMagnitude(-9.9));
|
||||
|
||||
|
||||
$this->assertSame(3, $this->service->getMagnitude(9999.99));
|
||||
$this->assertSame(3, $this->service->getMagnitude(1000.0));
|
||||
$this->assertSame(3, $this->service->getMagnitude(-9999.99));
|
||||
$this->assertSame(3, $this->service->getMagnitude(-1000.0));
|
||||
|
||||
$this->assertSame(-1, $this->service->getMagnitude(0.1));
|
||||
$this->assertSame(-1, $this->service->getMagnitude(-0.9999));
|
||||
|
||||
$this->assertSame(-25, $this->service->getMagnitude(- 1.246e-25));
|
||||
$this->assertSame(12, $this->service->getMagnitude(9.99e12));
|
||||
}
|
||||
|
||||
public function testgetPrefixByMagnitude()
|
||||
{
|
||||
$this->assertSame([1000, 'k'], $this->service->getPrefixByMagnitude(3));
|
||||
$this->assertSame([1000, 'k'], $this->service->getPrefixByMagnitude(2));
|
||||
$this->assertSame([1000, 'k'], $this->service->getPrefixByMagnitude(4));
|
||||
|
||||
$this->assertSame([0.001, 'm'], $this->service->getPrefixByMagnitude(-3));
|
||||
$this->assertSame([0.001, 'm'], $this->service->getPrefixByMagnitude(-2));
|
||||
$this->assertSame([0.001, 'm'], $this->service->getPrefixByMagnitude(-4));
|
||||
}
|
||||
|
||||
public function testFormat()
|
||||
{
|
||||
$this->assertSame("2.32 km", $this->service->format(2321, 'm'));
|
||||
$this->assertSame("-98.20 mg", $this->service->format(-0.0982, 'g'));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue