You are here

final class AreaUnit in Physical Fields 8

Provides area units.

Hierarchy

Expanded class hierarchy of AreaUnit

File

src/AreaUnit.php, line 8

Namespace

Drupal\physical
View source
final class AreaUnit implements UnitInterface {
  const SQUARE_MILLIMETER = 'mm2';
  const SQUARE_CENTIMETER = 'cm2';
  const SQUARE_METER = 'm2';
  const SQUARE_INCH = 'in2';
  const SQUARE_FOOT = 'ft2';
  const HECTARE = 'ha';

  /**
   * {@inheritdoc}
   */
  public static function getLabels() {
    return [
      self::SQUARE_MILLIMETER => t('mm²'),
      self::SQUARE_CENTIMETER => t('cm²'),
      self::SQUARE_METER => t('m²'),
      self::SQUARE_INCH => t('in²'),
      self::SQUARE_FOOT => t('ft²'),
      self::HECTARE => t('ha'),
    ];
  }

  /**
   * {@inheritdoc}
   */
  public static function getBaseUnit() {
    return self::SQUARE_METER;
  }

  /**
   * {@inheritdoc}
   */
  public static function getBaseFactor($unit) {
    self::assertExists($unit);
    $factors = [
      self::SQUARE_MILLIMETER => '0.000001',
      self::SQUARE_CENTIMETER => '0.0001',
      self::SQUARE_METER => '1',
      self::SQUARE_INCH => '0.0006451600',
      self::SQUARE_FOOT => '0.09290304',
      self::HECTARE => '10000',
    ];
    return $factors[$unit];
  }

  /**
   * {@inheritdoc}
   */
  public static function assertExists($unit) {
    $allowed_units = [
      self::SQUARE_MILLIMETER,
      self::SQUARE_CENTIMETER,
      self::SQUARE_METER,
      self::SQUARE_INCH,
      self::SQUARE_FOOT,
      self::HECTARE,
    ];
    if (!in_array($unit, $allowed_units)) {
      throw new \InvalidArgumentException(sprintf('Invalid area unit "%s" provided.', $unit));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AreaUnit::assertExists public static function Asserts that the given unit exists. Overrides UnitInterface::assertExists
AreaUnit::getBaseFactor public static function Gets the base factor for the given unit. Overrides UnitInterface::getBaseFactor
AreaUnit::getBaseUnit public static function Gets the base unit. Overrides UnitInterface::getBaseUnit
AreaUnit::getLabels public static function Gets the labels for the defined units. Overrides UnitInterface::getLabels
AreaUnit::HECTARE constant
AreaUnit::SQUARE_CENTIMETER constant
AreaUnit::SQUARE_FOOT constant
AreaUnit::SQUARE_INCH constant
AreaUnit::SQUARE_METER constant
AreaUnit::SQUARE_MILLIMETER constant