MeasurementType.php in Physical Fields 8
File
src/MeasurementType.php
View source
<?php
namespace Drupal\physical;
final class MeasurementType {
const AREA = 'area';
const LENGTH = 'length';
const TEMPERATURE = 'temperature';
const VOLUME = 'volume';
const WEIGHT = 'weight';
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'),
];
}
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];
}
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];
}
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));
}
}
}