You are here

final class MeasurementType in Physical Fields 8

Provides measurement types.

Hierarchy

Expanded class hierarchy of MeasurementType

4 files declare their use of MeasurementType
Measurement.php in src/Element/Measurement.php
MeasurementDefaultFormatter.php in src/Plugin/Field/FieldFormatter/MeasurementDefaultFormatter.php
MeasurementDefaultWidget.php in src/Plugin/Field/FieldWidget/MeasurementDefaultWidget.php
MeasurementItem.php in src/Plugin/Field/FieldType/MeasurementItem.php

File

src/MeasurementType.php, line 8

Namespace

Drupal\physical
View source
final class MeasurementType {
  const AREA = 'area';
  const LENGTH = 'length';
  const TEMPERATURE = 'temperature';
  const VOLUME = 'volume';
  const WEIGHT = 'weight';

  /**
   * Gets the labels for the defined measurement types.
   *
   * @return array
   *   An array of labels keyed by measurement type.
   */
  public static function getLabels() {
    return [
      self::AREA => t('Area'),
      self::LENGTH => t('Length'),
      self::TEMPERATURE => t('Temperature'),
      self::VOLUME => t('Volume'),
      self::WEIGHT => t('Weight'),
    ];
  }

  /**
   * Gets the class for the given measurement type.
   *
   * @param string $type
   *   The measurement type.
   *
   * @return string
   *   The fully qualified class name.
   */
  public static function getClass($type) {
    self::assertExists($type);
    $classes = [
      self::AREA => Area::class,
      self::LENGTH => Length::class,
      self::TEMPERATURE => Temperature::class,
      self::VOLUME => Volume::class,
      self::WEIGHT => Weight::class,
    ];
    return $classes[$type];
  }

  /**
   * Gets the unit class for the given measurement type.
   *
   * @param string $type
   *   The measurement type.
   *
   * @return string
   *   The fully qualified class name.
   */
  public static function getUnitClass($type) {
    self::assertExists($type);
    $classes = [
      self::AREA => AreaUnit::class,
      self::LENGTH => LengthUnit::class,
      self::TEMPERATURE => TemperatureUnit::class,
      self::VOLUME => VolumeUnit::class,
      self::WEIGHT => WeightUnit::class,
    ];
    return $classes[$type];
  }

  /**
   * Asserts that the given measurement type exists.
   *
   * @param string $type
   *   The measurement type.
   *
   * @throws \InvalidArgumentException
   */
  public static function assertExists($type) {
    $allowed_types = [
      self::AREA,
      self::LENGTH,
      self::TEMPERATURE,
      self::VOLUME,
      self::WEIGHT,
    ];
    if (!in_array($type, $allowed_types)) {
      throw new \InvalidArgumentException(sprintf('Invalid measurement type "%s" provided.', $type));
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MeasurementType::AREA constant
MeasurementType::assertExists public static function Asserts that the given measurement type exists.
MeasurementType::getClass public static function Gets the class for the given measurement type.
MeasurementType::getLabels public static function Gets the labels for the defined measurement types.
MeasurementType::getUnitClass public static function Gets the unit class for the given measurement type.
MeasurementType::LENGTH constant
MeasurementType::TEMPERATURE constant
MeasurementType::VOLUME constant
MeasurementType::WEIGHT constant