You are here

function physical_volume_convert in Physical Fields 7

Converts a volume field value array to the specified unit.

Parameters

$volume: A volume field value array including the 'volume' and 'unit'.

$unit: The volume unit type to convert to.

Return value

A volume field value array including the converted 'volume' amount in the target 'unit' type.

File

./physical.module, line 768
Defines fields (e.g. weight and dimensions) to support describing the physical attributes of entities.

Code

function physical_volume_convert($volume, $unit) {
  if ($volume['unit'] == $unit) {
    return $volume;
  }

  // Convert the volume amount based on the target and source unit type.
  switch ($unit) {
    case 'l':
      if ($volume['unit'] == 'm3') {
        $multiplier = 0.001;
      }
      elseif ($volume['unit'] == 'ml') {
        $multiplier = 1000;
      }
      elseif ($volume['unit'] == 'fl oz') {
        $multiplier = 33.8140227018;
      }
      elseif ($volume['unit'] == 'pt') {
        $multiplier = 2.1133764189;
      }
      elseif ($volume['unit'] == 'gal') {
        $multiplier = 0.2641720524;
      }
      break;
    case 'm3':
      if ($volume['unit'] == 'l') {
        $multiplier = 1000;
      }
      elseif ($volume['unit'] == 'ml') {
        $multiplier = 1000000;
      }
      elseif ($volume['unit'] == 'fl oz') {
        $multiplier = 33814.022701843;
      }
      elseif ($volume['unit'] == 'pt') {
        $multiplier = 2113.3764188652;
      }
      elseif ($volume['unit'] == 'gal') {
        $multiplier = 264.1720523581;
      }
      break;
    case 'ml':
      if ($volume['unit'] == 'l') {
        $multiplier = 0.001;
      }
      elseif ($volume['unit'] == 'm3') {
        $multiplier = 1.0E-6;
      }
      elseif ($volume['unit'] == 'fl oz') {
        $multiplier = 0.0338140227;
      }
      elseif ($volume['unit'] == 'pt') {
        $multiplier = 0.0021133764;
      }
      elseif ($volume['unit'] == 'gal') {
        $multiplier = 0.0002641721;
      }
      break;
    case 'fl oz':
      if ($volume['unit'] == 'l') {
        $multiplier = 0.0295735296;
      }
      elseif ($volume['unit'] == 'm3') {
        $multiplier = 2.95735E-5;
      }
      elseif ($volume['unit'] == 'ml') {
        $multiplier = 29.5735295625;
      }
      elseif ($volume['unit'] == 'pt') {
        $multiplier = 0.0625;
      }
      elseif ($volume['unit'] == 'gal') {
        $multiplier = 0.0078125;
      }
      break;
    case 'pt':
      if ($volume['unit'] == 'l') {
        $multiplier = 0.473176473;
      }
      elseif ($volume['unit'] == 'm3') {
        $multiplier = 0.0004731765;
      }
      elseif ($volume['unit'] == 'ml') {
        $multiplier = 473.176473;
      }
      elseif ($volume['unit'] == 'fl oz') {
        $multiplier = 16;
      }
      elseif ($volume['unit'] == 'gal') {
        $multiplier = 0.125;
      }
      break;
    case 'gal':
      if ($volume['unit'] == 'l') {
        $multiplier = 3.785411784;
      }
      elseif ($volume['unit'] == 'm3') {
        $multiplier = 0.0037854118;
      }
      elseif ($volume['unit'] == 'ml') {
        $multiplier = 3785.411784;
      }
      elseif ($volume['unit'] == 'fl oz') {
        $multiplier = 128;
      }
      elseif ($volume['unit'] == 'pt') {
        $multiplier = 8;
      }
      break;
  }

  // Update the weight amount using the multiplier.
  $volume['volume'] *= $multiplier;

  // Update the unit type to the target type.
  $volume['unit'] = $unit;
  return $volume;
}