Skip to content

Commit

Permalink
0.10 additional si units (#510)
Browse files Browse the repository at this point in the history
* Added Speed Units

* added force units

* Pressure Units

* Add volumetric flow

* Added quotient

* Added translations

* Removed light year

---------

Signed-off-by: Vinzenz Rosenkranz <[email protected]>
Co-authored-by: Vinzenz Rosenkranz <[email protected]>
  • Loading branch information
Severino and v1r0x authored Jul 30, 2024
1 parent e964df6 commit 34b5296
Show file tree
Hide file tree
Showing 15 changed files with 1,654 additions and 1,405 deletions.
17 changes: 17 additions & 0 deletions app/AttributeTypes/Units/Implementations/ForceUnits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace App\AttributeTypes\Units\Implementations;

use App\AttributeTypes\Units\Constants\Si;
use App\AttributeTypes\Units\Unit;
use App\AttributeTypes\Units\UnitSystem;
use App\AttributeTypes\Units\UnitType;

class ForceUnits extends UnitSystem {
public function __construct() {
parent::__construct('force', [
Unit::createBase('newton' , 'N' ),
Unit::createUnit('kilonewton', 'kN', Si::KILO, 1, UnitType::SI),
]);
}
}
15 changes: 6 additions & 9 deletions app/AttributeTypes/Units/Implementations/LengthUnits.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class LengthUnits extends UnitSystem {

public function __construct() {
parent::__construct('length', [
Unit::createUnit('nanometre' , 'nm', Si::NANO, 1, UnitType::SI),
Unit::createUnit('micrometre', 'µm', Si::MICRO, 1, UnitType::SI),
Unit::createUnit('millimetre', 'mm', Si::MILLI, 1, UnitType::SI),
Unit::createUnit('centimetre', 'cm', Si::CENTI, 1, UnitType::SI),
Unit::createBase( 'metre' , 'm'),
Unit::createUnit('kilometre' , 'km', Si::KILO, 1, UnitType::SI),
Unit::createUnit('nanometre' , 'nm' , Si::NANO , 1, UnitType::SI),
Unit::createUnit('micrometre', 'µm' , Si::MICRO , 1, UnitType::SI),
Unit::createUnit('millimetre', 'mm' , Si::MILLI , 1, UnitType::SI),
Unit::createUnit('centimetre', 'cm' , Si::CENTI , 1, UnitType::SI),
Unit::createBase('metre' , 'm' ),
Unit::createUnit('kilometre' , 'km' , Si::KILO , 1, UnitType::SI),
]);

$this->addMultiple([
Expand All @@ -27,8 +27,5 @@ public function __construct() {
Unit::createUnit('yard' , 'yd', Imperial::YARD_2_M),
Unit::createUnit('mile' , 'mi', Imperial::MILE_2_M),
]);
$this->add(
Unit::createUnit('light_year', 'ly', General::LIGHTYEAR),
);
}
}
35 changes: 35 additions & 0 deletions app/AttributeTypes/Units/Implementations/PressureUnits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace App\AttributeTypes\Units\Implementations;

use App\AttributeTypes\Units\Call;
use App\AttributeTypes\Units\Constants\Si;
use App\AttributeTypes\Units\Unit;
use App\AttributeTypes\Units\UnitSystem;
use App\AttributeTypes\Units\UnitType;

class PressureUnits extends UnitSystem {
public function __construct() {
// Pascal
parent::__construct('pressure', [
Unit::createBase('pascal' , 'Pa' ),
Unit::createUnit('kilopascal' , 'kPa', Si::KILO, 1, UnitType::SI),
Unit::createUnit('hectopascal', 'hPa', Si::HECTO, 1, UnitType::SI),
]);

// Bar
$this->addMultiple([
Unit::createUnit('bar' , 'bar' , 1e5),
Unit::createUnit('decibar' , 'dbar', 1e4), // Used in oceanography
Unit::createUnit('millibar', 'mbar', 1e2),
]);

// Various
$this->addMultiple([
Unit::createUnit('pound_per_square_inch', 'psi' , 6894.757),
Unit::createUnit('torr' , 'Torr', 133.3224),
Unit::createUnit('technical_atmosphere' , 'at' , 98066.5 ),
Unit::createUnit('standard_atmosphere' , 'atm' , 101325 ),
]);
}
}
18 changes: 18 additions & 0 deletions app/AttributeTypes/Units/Implementations/QuotientUnits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

namespace App\AttributeTypes\Units\Implementations;

use App\AttributeTypes\Units\Unit;
use App\AttributeTypes\Units\UnitSystem;

class QuotientUnits extends UnitSystem
{

public function __construct()
{
parent::__construct('quotient', [
Unit::createUnit('percent' , '%' , 1e4),
Unit::createBase('parts per million', 'ppm' ),
]);
}
}
24 changes: 24 additions & 0 deletions app/AttributeTypes/Units/Implementations/SpeedUnits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace App\AttributeTypes\Units\Implementations;

use App\AttributeTypes\Units\Unit;
use App\AttributeTypes\Units\UnitSystem;

class SpeedUnits extends UnitSystem {

public function __construct() {
parent::__construct('speed', [
Unit::createBase('metre_per_second' , 'm/s' ),
Unit::createUnit('kilometre_per_hour', 'km/h' , self::factorFromTimeAndLengthUnits('hour', 'kilometre')),
Unit::createUnit('mile_per_hour' , 'mph' , self::factorFromTimeAndLengthUnits('hour', 'mile') ),
]);
}

public static function factorFromTimeAndLengthUnits(string $time, string $length) : float {
$lengthUnits = new LengthUnits();
$timeUnits = new TimeUnits();

return $lengthUnits->getByLabel($length)->is(1) / $timeUnits->getByLabel($time)->is(1);
}
}
28 changes: 16 additions & 12 deletions app/AttributeTypes/Units/Implementations/UnitManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,26 @@ function __construct() {
public function addAllUnitSystems(): void {
$this->addUnitSystems([
new AreaUnits(),
new ForceUnits(),
new LengthUnits(),
new MassUnits(),
new PressureUnits(),
new SpeedUnits(),
new TemperatureUnits(),
new TimeUnits(),
new VolumeUnits(),
new VolumetricFlowUnits(),
]);
}

public function addUnitSystems(array $unitSystems): void {
foreach ($unitSystems as $unitSystem) {
foreach($unitSystems as $unitSystem) {
$this->addUnitSystem($unitSystem);
}
}

public function addUnitSystem(UnitSystem $system): void {
foreach ($system->getUnits() as $unit) {
foreach($system->getUnits() as $unit) {
$this->verifyUnitDoesNotExist($system, $unit);
}

Expand All @@ -52,23 +56,23 @@ public function addUnitSystem(UnitSystem $system): void {
private function verifyUnitDoesNotExist(unitSystem $unitSystem, Unit $unit): void {

$unityByLabel = $this->findUnitByLabel($unit->getLabel());
if (isset($unityByLabel)) {
if(isset($unityByLabel)) {
throw new Exception('Error on "' . $unitSystem->getName() . '": Unit with Label "' . $unit->getLabel() . '" already exists');
}

$unitBySymbol = $this->findUnitBySymbol($unit->getSymbol());
if (isset($unitBySymbol)) {
if(isset($unitBySymbol)) {
throw new Exception('Error on "' . $unitSystem->getName() . '": Unit with Symbol "' . $unit->getSymbol() . '" already exists');
}
}

function findUnitByAny(string $text): ?Unit {
$unit = $this->findUnitByLabel($text);
if (isset($unit)) {
if(isset($unit)) {
return $unit;
}
$unit = $this->findUnitBySymbol($text);
if (isset($unit)) {
if(isset($unit)) {
return $unit;
}

Expand All @@ -84,18 +88,18 @@ function findUnitBySymbol(string $symbol): ?Unit {
}

private function getByUnitSystemFunc(string $value, string $functionName): ?Unit {
foreach ($this->unitSystems as $unitSystem) {
foreach($this->unitSystems as $unitSystem) {
$unit = $unitSystem->{$functionName}($value);
if (isset($unit)) {
if(isset($unit)) {
return $unit;
}
}
return null;
}

public function getUnitSystem(string $name): ?UnitSystem {
foreach ($this->unitSystems as $unitSystem) {
if ($unitSystem->getName() == $name) {
foreach($this->unitSystems as $unitSystem) {
if($unitSystem->getName() == $name) {
return $unitSystem;
}
}
Expand All @@ -107,8 +111,8 @@ public function getUnitSystems(): array {
}

public function hasQuantity(string $quantity): bool {
foreach ($this->unitSystems as $unitSystem) {
if ($unitSystem->getName() == $quantity) {
foreach($this->unitSystems as $unitSystem) {
if($unitSystem->getName() == $quantity) {
return true;
}
}
Expand Down
16 changes: 16 additions & 0 deletions app/AttributeTypes/Units/Implementations/VolumetricFlowUnits.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace App\AttributeTypes\Units\Implementations;

use App\AttributeTypes\Units\Constants\Si;
use App\AttributeTypes\Units\Unit;
use App\AttributeTypes\Units\UnitSystem;

class VolumetricFlowUnits extends UnitSystem {
public function __construct() {
parent::__construct('volumetric_flow', [
Unit::createBase('cubic_metre_per_second', 'm³/s' ),
Unit::createUnit('litre_per_second' , 'l/s' , 10 ** (Si::DECI * 3)),
]);
}
}
11 changes: 7 additions & 4 deletions app/AttributeTypes/Units/UnitSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function __construct(string $name, array $units) {
}

public function addMultiple(array $units) {
foreach ($units as $unit) {
foreach($units as $unit) {
if($unit->getBase()) {
throw new Exception('Base already defined for this Unit System');
}
Expand All @@ -43,7 +43,7 @@ public function addMultiple(array $units) {
}

public function add(Unit $unit) {
if (isset($this->labelMap[$unit->getLabel()]) || isset($this->symbolMap[$unit->getSymbol()])) {
if(isset($this->labelMap[$unit->getLabel()]) || isset($this->symbolMap[$unit->getSymbol()])) {
throw new Exception("Unit already exists: {$unit->getLabel()} ({$unit->getSymbol()})");
}

Expand All @@ -57,19 +57,22 @@ public function add(Unit $unit) {
return count($this->units);
}

/**
* Get's a unit by it's label
*/
public function get(string $label): ?Unit {
return $this->getByLabel($label);
}

public function getByLabel(string $label): ?Unit {

if (!isset($this->labelMap[$label])) return null;
if(!isset($this->labelMap[$label])) return null;

return $this->labelMap[$label];
}

public function getBySymbol(string $symbol): ?Unit {
if (!isset($this->symbolMap[$symbol])) return null;
if(!isset($this->symbolMap[$symbol])) return null;

return $this->symbolMap[$symbol];
}
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@
},
"require-dev": {
"beyondcode/laravel-dump-server": "^1.0",
"dms/phpunit-arraysubset-asserts": "^0.2.1",
"dms/phpunit-arraysubset-asserts": "^0.5.0",
"filp/whoops": "~2.0",
"fzaninotto/faker": "~1.4",
"mockery/mockery": "~1.0",
"nunomaduro/collision": "^7.0",
"phpunit/phpunit": "~9.0"
"phpunit/phpunit": "^10.0"
},
"autoload": {
"files": [
Expand Down
Loading

0 comments on commit 34b5296

Please sign in to comment.