
| Current Path : /var/www/html/store/web/modules/contrib/physical/src/ |
Linux ift1.ift-informatik.de 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64 |
| Current File : /var/www/html/store/web/modules/contrib/physical/src/MeasurementType.php |
<?php
namespace Drupal\physical;
/**
* Provides measurement types.
*/
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));
}
}
}