Example #1
0
 public function testGetGroup()
 {
     $group = \ILess\Util\UnitConversion::getGroup('length');
     $this->assertInternalType('array', $group);
     $group = \ILess\Util\UnitConversion::getGroup('duration');
     $this->assertInternalType('array', $group);
     $group = \ILess\Util\UnitConversion::getGroup('angle');
     $this->assertInternalType('array', $group);
     $group = \ILess\Util\UnitConversion::getGroup('invalid');
     $this->assertInternalType('null', $group);
 }
Example #2
0
 public function usedUnits()
 {
     $result = [];
     foreach (UnitConversion::getGroups() as $groupName) {
         $group = UnitConversion::getGroup($groupName);
         for ($i = 0; $i < count($this->numerator); ++$i) {
             $atomicUnit = $this->numerator[$i];
             if (isset($group[$atomicUnit]) && !isset($result[$groupName])) {
                 $result[$groupName] = $atomicUnit;
             }
         }
         for ($i = 0; $i < count($this->denominator); ++$i) {
             $atomicUnit = $this->denominator[$i];
             if (isset($group[$atomicUnit]) && !isset($result[$groupName])) {
                 $result[$groupName] = $atomicUnit;
             }
         }
     }
     return $result;
 }
Example #3
0
 /**
  * Converts to another unit
  *
  * @param array|string $conversions
  * @return DimensionNode
  */
 public function convertTo($conversions)
 {
     $value = $this->value;
     $unit = clone $this->unit;
     if (is_string($conversions)) {
         $derivedConversions = [];
         foreach (UnitConversion::getGroups() as $i) {
             $group = UnitConversion::getGroup($i);
             if (isset($group[$conversions])) {
                 $derivedConversions = [$i => $conversions];
             }
         }
         $conversions = $derivedConversions;
     }
     foreach ($conversions as $groupName => $targetUnit) {
         $group = UnitConversion::getGroup($groupName);
         // numerator
         for ($i = 0, $count = count($unit->numerator); $i < $count; $i++) {
             $atomicUnit = $unit->numerator[$i];
             if (is_object($atomicUnit)) {
                 continue;
             }
             if (!isset($group[$atomicUnit])) {
                 continue;
             }
             $value = $value * $group[$atomicUnit] / $group[$targetUnit];
             $unit->numerator[$i] = $targetUnit;
         }
         // denominator
         for ($i = 0, $count = count($unit->denominator); $i < $count; $i++) {
             $atomicUnit = $unit->denominator[$i];
             if (!isset($group[$atomicUnit])) {
                 continue;
             }
             $value = $value / ($group[$atomicUnit] / $group[$targetUnit]);
             $unit->denominator[$i] = $targetUnit;
         }
     }
     $unit->cancel();
     return new DimensionNode($value, $unit);
 }