You are here

final class LengthUnit in Physical Fields 8

Provides length units.

Hierarchy

Expanded class hierarchy of LengthUnit

5 files declare their use of LengthUnit
Dimensions.php in src/Element/Dimensions.php
DimensionsDefaultFormatter.php in src/Plugin/Field/FieldFormatter/DimensionsDefaultFormatter.php
DimensionsDefaultWidget.php in src/Plugin/Field/FieldWidget/DimensionsDefaultWidget.php
DimensionsTestForm.php in tests/modules/physical_test/src/Form/DimensionsTestForm.php
MeasurementTestForm.php in tests/modules/physical_test/src/Form/MeasurementTestForm.php

File

src/LengthUnit.php, line 8

Namespace

Drupal\physical
View source
final class LengthUnit implements UnitInterface {
  const MILLIMETER = 'mm';
  const CENTIMETER = 'cm';
  const METER = 'm';
  const KILOMETER = 'km';
  const INCH = 'in';
  const FOOT = 'ft';
  const NAUTICAL_MILE = 'M';

  /**
   * {@inheritdoc}
   */
  public static function getLabels() {
    return [
      self::MILLIMETER => t('mm'),
      self::CENTIMETER => t('cm'),
      self::METER => t('m'),
      self::KILOMETER => t('km'),
      self::INCH => t('in'),
      self::FOOT => t('ft'),
      self::NAUTICAL_MILE => t('M'),
    ];
  }

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

  /**
   * {@inheritdoc}
   */
  public static function getBaseFactor($unit) {
    self::assertExists($unit);
    $factors = [
      self::MILLIMETER => '0.001',
      self::CENTIMETER => '0.01',
      self::METER => '1',
      self::KILOMETER => '1000',
      self::INCH => '0.0254',
      self::FOOT => '0.3048',
      self::NAUTICAL_MILE => '1852',
    ];
    return $factors[$unit];
  }

  /**
   * {@inheritdoc}
   */
  public static function assertExists($unit) {
    $allowed_units = [
      self::MILLIMETER,
      self::CENTIMETER,
      self::METER,
      self::KILOMETER,
      self::INCH,
      self::FOOT,
      self::NAUTICAL_MILE,
    ];
    if (!in_array($unit, $allowed_units)) {
      throw new \InvalidArgumentException(sprintf('Invalid length unit "%s" provided.', $unit));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LengthUnit::assertExists public static function Asserts that the given unit exists. Overrides UnitInterface::assertExists
LengthUnit::CENTIMETER constant
LengthUnit::FOOT constant
LengthUnit::getBaseFactor public static function Gets the base factor for the given unit. Overrides UnitInterface::getBaseFactor
LengthUnit::getBaseUnit public static function Gets the base unit. Overrides UnitInterface::getBaseUnit
LengthUnit::getLabels public static function Gets the labels for the defined units. Overrides UnitInterface::getLabels
LengthUnit::INCH constant
LengthUnit::KILOMETER constant
LengthUnit::METER constant
LengthUnit::MILLIMETER constant
LengthUnit::NAUTICAL_MILE constant