Created
February 28, 2013 10:21
-
-
Save pumatertion/5055712 to your computer and use it in GitHub Desktop.
Distance Converting Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace TYPO3\Units\UnitType; | |
use TYPO3\Units\Error\UnitConstantException; | |
/** | |
* Class to convert distance unit values into others | |
* For example it converts meter to inch and backwards | |
* or Megameter in mile | |
*/ | |
class Distance { | |
const Mm = 1000000, | |
km = 1000, | |
hm = 100, | |
dam = 10, | |
m = 1, | |
dm = 0.1, | |
cm = 0.01, | |
mm = 0.001, | |
µm = 0.000001, | |
nm = 0.000000001, | |
Å = 0.0000000001, | |
pm = 0.000000000001, | |
fm = 0.000000000000001, | |
inch = 0.0254, | |
mile = 1609.344; | |
/** | |
* @param float $value | |
* @param string $from | |
* @param string $to | |
* @return float | |
* @throws \TYPO3\Units\Error\UnitConstantException | |
*/ | |
public static function convert($value, $from, $to) { | |
try{ | |
$fromFactor = constant('self::'.$from); | |
}catch(\Exception $e){ | |
$classReflection = new \TYPO3\Flow\Reflection\ClassReflection(__CLASS__); | |
throw new UnitConstantException('You want to convert from an unknown unit ('.$from.'). Possible types are: '.implode(', ',array_keys($classReflection->getConstants())),1362041112,$e); | |
} | |
try{ | |
$toFactor = constant('self::'.$to); | |
}catch(\Exception $e){ | |
$classReflection = new \TYPO3\Flow\Reflection\ClassReflection(__CLASS__); | |
throw new UnitConstantException('You want to convert from an unknown unit ('.$from.'). Possible types are: '.implode(', ',array_keys($classReflection->getConstants())),1362041112,$e); | |
} | |
return $value * $fromFactor / $toFactor; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment