62 lines
1.3 KiB
PHP
62 lines
1.3 KiB
PHP
<?PHP
|
||
/**
|
||
* Constains lists for categorizing misspelled weight units.
|
||
*
|
||
* @author Joshua Ramon Enslin <joshua@museum-digital.de>
|
||
*/
|
||
declare(strict_types = 1);
|
||
|
||
/**
|
||
* Provides lists for categorizing misspelled sizes.
|
||
*/
|
||
final class MDConcWeights implements MDImporterConcordanceListInterface {
|
||
|
||
/**
|
||
* Matches.
|
||
*
|
||
* @param string $input Input string.
|
||
*
|
||
* @return MDWeightUnit|false
|
||
*/
|
||
private static function _match(string $input):MDWeightUnit|false {
|
||
|
||
return match($input) {
|
||
|
||
"mm",
|
||
"cm" => false,
|
||
|
||
"Gramm",
|
||
"g",
|
||
"G",
|
||
"г" => MDWeightUnit::g,
|
||
|
||
default => throw new MDInvalidWeightUnit("Invalid weight unit " . $input),
|
||
|
||
};
|
||
|
||
}
|
||
|
||
/**
|
||
* Require a function for getting the concordance target.
|
||
*
|
||
* @param string $input Input string.
|
||
*
|
||
* @return MDWeightUnit|false
|
||
*/
|
||
public static function getConcordanceTarget(string $input):MDWeightUnit|false {
|
||
|
||
try {
|
||
return self::_match($input);
|
||
}
|
||
catch (MDInvalidWeightUnit $e) {
|
||
|
||
if (is_numeric($input) || is_numeric(strtr($input, ['.' => '', ',' => '']))) {
|
||
return false;
|
||
}
|
||
|
||
return self::_match(trim($input));
|
||
}
|
||
|
||
}
|
||
}
|