Add MDMeasurementType for listing available measurement types
This commit is contained in:
141
src/enums/MDMeasurementType.php
Normal file
141
src/enums/MDMeasurementType.php
Normal file
@ -0,0 +1,141 @@
|
||||
<?PHP
|
||||
/**
|
||||
* Represents a measurement type.
|
||||
*
|
||||
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||||
*/
|
||||
declare(strict_types = 1);
|
||||
|
||||
/**
|
||||
* Represents a measurement type.
|
||||
*/
|
||||
enum MDMeasurementType implements JsonSerializable {
|
||||
|
||||
case length;
|
||||
case height;
|
||||
case width;
|
||||
case number_of_pieces;
|
||||
case weight;
|
||||
case diameter;
|
||||
case wall;
|
||||
case number_of_pages;
|
||||
case die_axis;
|
||||
|
||||
/**
|
||||
* Returns a value of this type based on a string.
|
||||
*
|
||||
* @param string $input Input to get a value from.
|
||||
*
|
||||
* @return MDCopyrightCollective
|
||||
*/
|
||||
public static function fromString(string $input):MDCopyrightCollective {
|
||||
|
||||
return match($input) {
|
||||
'length' => self::length,
|
||||
'height' => self::height,
|
||||
'width' => self::width,
|
||||
'number_of_pieces' => self::number_of_pieces,
|
||||
'weight' => self::weight,
|
||||
'diameter' => self::diameter,
|
||||
'wall' => self::wall,
|
||||
'number_of_pages' => self::number_of_pages,
|
||||
'die_axis' => self::die_axis,
|
||||
default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a value of this type based on an integer.
|
||||
*
|
||||
* @param integer $input Input to get a value from.
|
||||
*
|
||||
* @return MDCopyrightCollective
|
||||
*/
|
||||
public static function fromInt(int $input):MDCopyrightCollective {
|
||||
|
||||
return match($input) {
|
||||
1 => self::length,
|
||||
2 => self::height,
|
||||
3 => self::width,
|
||||
4 => self::number_of_pieces,
|
||||
5 => self::weight,
|
||||
6 => self::diameter,
|
||||
7 => self::wall,
|
||||
8 => self::number_of_pages,
|
||||
9 => self::die_axis,
|
||||
default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* Lists all available names.
|
||||
*
|
||||
* @return array<string>
|
||||
*/
|
||||
public static function caseNames():array {
|
||||
|
||||
$output = [];
|
||||
|
||||
$cases = self::cases();
|
||||
foreach ($cases as $case) {
|
||||
$output[] = $case->name;
|
||||
}
|
||||
|
||||
return $output;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the human-readable name of the measurement type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function getName():string {
|
||||
|
||||
return match($this) {
|
||||
self::vg_bildkunst => "VG Bildkunst",
|
||||
# default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
||||
default => throw new MDpageParameterNotFromListException("To be implemented"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an integer representation of the collective (for storage in DB).
|
||||
*
|
||||
* @return integer
|
||||
*/
|
||||
public function toInt():int {
|
||||
|
||||
return match($this) {
|
||||
self::length => 1,
|
||||
self::height => 2,
|
||||
self::width => 3,
|
||||
self::number_of_pieces => 4,
|
||||
self::weight => 5,
|
||||
self::diameter => 6,
|
||||
self::wall => 7,
|
||||
self::number_of_pages => 8,
|
||||
self::die_axis => 9,
|
||||
default => throw new MDpageParameterNotFromListException("Unknown measurement type"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides the option to serialize as a string during json_encode().
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function jsonSerialize():string {
|
||||
|
||||
return $this->name;
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user