diff --git a/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php b/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php index f2a0d978..b5597eaf 100644 --- a/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php +++ b/src/Services/InfoProviderSystem/DTOs/ParameterDTO.php @@ -87,14 +87,32 @@ class ParameterDTO { //Try to extract unit from value $unit = null; - if (is_string($value) && preg_match('/^(?[0-9.]+)\s*(?[°a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) { - $value = $matches['value']; - $unit = $matches['unit']; + if (is_string($value)) { + [$number, $unit] = self::splitIntoValueAndUnit($value) ?? [$value, null]; - 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 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('/^(?[0-9.]+)\s*(?[°℃a-zA-Z_]+\s?\w{0,4})$/u', $value, $matches)) { + $value = $matches['value']; + $unit = $matches['unit']; + + return [$value, $unit]; + } + + return null; + } } \ No newline at end of file diff --git a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php index 0855cb78..6f005c73 100755 --- a/src/Services/InfoProviderSystem/Providers/LCSCProvider.php +++ b/src/Services/InfoProviderSystem/Providers/LCSCProvider.php @@ -249,7 +249,24 @@ class LCSCProvider implements InfoProviderInterface $result = []; 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; diff --git a/tests/Services/InfoProviderSystem/DTOs/ParameterDTOTest.php b/tests/Services/InfoProviderSystem/DTOs/ParameterDTOTest.php index 26a4483a..4300259f 100644 --- a/tests/Services/InfoProviderSystem/DTOs/ParameterDTOTest.php +++ b/tests/Services/InfoProviderSystem/DTOs/ParameterDTOTest.php @@ -161,4 +161,20 @@ class ParameterDTOTest extends TestCase { $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')); + } }