You are here

public static function WeightUnit::assertExists in Physical Fields 8

Asserts that the given unit exists.

Parameters

string $unit: The unit.

Throws

\InvalidArgumentException

Overrides UnitInterface::assertExists

1 call to WeightUnit::assertExists()
WeightUnit::getBaseFactor in src/WeightUnit.php
Gets the base factor for the given unit.

File

src/WeightUnit.php, line 55

Class

WeightUnit
Provides weight units.

Namespace

Drupal\physical

Code

public static function assertExists($unit) {
  $allowed_units = [
    self::MILLIGRAM,
    self::GRAM,
    self::KILOGRAM,
    self::OUNCE,
    self::POUND,
  ];
  if (!in_array($unit, $allowed_units)) {
    throw new \InvalidArgumentException(sprintf('Invalid weight unit "%s" provided.', $unit));
  }
}