mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Improved parsing of parameters. Values with unit and ranges get now properly parsed into the Part-DB structures
This commit is contained in:
parent
0bbfaf9893
commit
d600cb4b9a
3 changed files with 56 additions and 5 deletions
|
@ -87,14 +87,32 @@ class ParameterDTO
|
||||||
{
|
{
|
||||||
//Try to extract unit from value
|
//Try to extract unit from value
|
||||||
$unit = null;
|
$unit = null;
|
||||||
if (is_string($value) && preg_match('/^(?<value>[0-9.]+)\s*(?<unit>[°a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) {
|
if (is_string($value)) {
|
||||||
$value = $matches['value'];
|
[$number, $unit] = self::splitIntoValueAndUnit($value) ?? [$value, null];
|
||||||
$unit = $matches['unit'];
|
|
||||||
|
|
||||||
return self::parseValueField(name: $name, value: $value, unit: $unit, symbol: $symbol, group: $group);
|
return self::parseValueField(name: $name, value: $number, unit: $unit, symbol: $symbol, group: $group);
|
||||||
}
|
}
|
||||||
|
|
||||||
//Otherwise we assume that no unit is given
|
//Otherwise we assume that no unit is given
|
||||||
return self::parseValueField(name: $name, value: $value, unit: null, symbol: $symbol, group: $group);
|
return self::parseValueField(name: $name, value: $value, unit: null, symbol: $symbol, group: $group);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Splits the given value into a value and a unit part if possible.
|
||||||
|
* If the value is not in the expected format, null is returned.
|
||||||
|
* @param string $value The value to split
|
||||||
|
* @return array|null An array with the value and the unit part or null if the value is not in the expected format
|
||||||
|
* @phpstan-return array{0: string, 1: string}|null
|
||||||
|
*/
|
||||||
|
public static function splitIntoValueAndUnit(string $value): ?array
|
||||||
|
{
|
||||||
|
if (preg_match('/^(?<value>[0-9.]+)\s*(?<unit>[°℃a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) {
|
||||||
|
$value = $matches['value'];
|
||||||
|
$unit = $matches['unit'];
|
||||||
|
|
||||||
|
return [$value, $unit];
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -249,7 +249,24 @@ class LCSCProvider implements InfoProviderInterface
|
||||||
$result = [];
|
$result = [];
|
||||||
|
|
||||||
foreach ($attributes as $attribute) {
|
foreach ($attributes as $attribute) {
|
||||||
$result[] = ParameterDTO::parseValueField(name: $attribute['paramNameEn'], value: $attribute['paramValueEn'], unit: null, group: null);
|
|
||||||
|
//If the attribute contains a tilde we assume it is a range
|
||||||
|
if (str_contains($attribute['paramValueEn'], '~')) {
|
||||||
|
$parts = explode('~', $attribute['paramValueEn']);
|
||||||
|
if (count($parts) === 2) {
|
||||||
|
//Try to extract number and unit from value (allow leading +)
|
||||||
|
[$number, $unit] = ParameterDTO::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
|
||||||
|
[$number2, $unit2] = ParameterDTO::splitIntoValueAndUnit(ltrim($parts[1], " +")) ?? [$parts[1], null];
|
||||||
|
|
||||||
|
//If both parts have the same unit and both values are numerical, we assume it is a range
|
||||||
|
if ($unit === $unit2 && is_numeric($number) && is_numeric($number2)) {
|
||||||
|
$result[] = new ParameterDTO(name: $attribute['paramNameEn'], value_min: (float) $number, value_max: (float) $number2, unit: $unit, group: null);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$result[] = ParameterDTO::parseValueIncludingUnit(name: $attribute['paramNameEn'], value: $attribute['paramValueEn'], group: null);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
|
|
|
@ -161,4 +161,20 @@ class ParameterDTOTest extends TestCase
|
||||||
{
|
{
|
||||||
$this->assertEquals($expected, ParameterDTO::parseValueIncludingUnit($name, $value, $symbol, $group));
|
$this->assertEquals($expected, ParameterDTO::parseValueIncludingUnit($name, $value, $symbol, $group));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testSplitIntoValueAndUnit(): void
|
||||||
|
{
|
||||||
|
$this->assertEquals(['1.0', 'kg'], ParameterDTO::splitIntoValueAndUnit('1.0 kg'));
|
||||||
|
$this->assertEquals(['1.0', 'kg'], ParameterDTO::splitIntoValueAndUnit('1.0kg'));
|
||||||
|
$this->assertEquals(['1', 'kg'], ParameterDTO::splitIntoValueAndUnit('1 kg'));
|
||||||
|
|
||||||
|
$this->assertEquals(['1.0', '°C'], ParameterDTO::splitIntoValueAndUnit('1.0°C'));
|
||||||
|
$this->assertEquals(['1.0', '°C'], ParameterDTO::splitIntoValueAndUnit('1.0 °C'));
|
||||||
|
|
||||||
|
$this->assertEquals(['1.0', 'C_m'], ParameterDTO::splitIntoValueAndUnit('1.0C_m'));
|
||||||
|
$this->assertEquals(["70", "℃"], ParameterDTO::splitIntoValueAndUnit("70℃"));
|
||||||
|
|
||||||
|
$this->assertNull(ParameterDTO::splitIntoValueAndUnit('kg'));
|
||||||
|
$this->assertNull(ParameterDTO::splitIntoValueAndUnit('Test'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue