Skip to content

Commit

Permalink
ParameterDTO: Don't overwrite $unit if it's not empty
Browse files Browse the repository at this point in the history
  • Loading branch information
frank-f committed Mar 31, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
1 parent f8db489 commit c9e391b
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/Services/InfoProviderSystem/DTOs/ParameterDTO.php
Original file line number Diff line number Diff line change
@@ -70,26 +70,33 @@ public static function parseValueField(string $name, string|float $value, ?strin
$parts = preg_split('/\s*(\.{3}|~)\s*/', $value);
if (count($parts) === 2) {
//Try to extract number and unit from value (allow leading +)
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
if (empty($unit)) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($parts[0], " +")) ?? [$parts[0], null];
$unit2 = null;
} else {
$unit2 = $unit;
}
// If the second part has some extra info, we'll save that into value_text
if (!empty($unit) && preg_match('/^(.+' . preg_quote($unit) . ')\s*(.+)$/', $parts[1], $matches) > 0) {
$parts[1] = $matches[1];
$value_text2 = $matches[2];
} else {
$value_text2 = null;
}
[$number2, $unit2] = self::splitIntoValueAndUnit(ltrim($parts[1], " +")) ?? [$parts[1], null];
if (empty($unit2)) {
[$number2, $unit2] = self::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)) {

Check failure on line 91 in src/Services/InfoProviderSystem/DTOs/ParameterDTO.php

GitHub Actions / Static analysis

Variable $number might not be defined.

Check failure on line 91 in src/Services/InfoProviderSystem/DTOs/ParameterDTO.php

GitHub Actions / Static analysis

Variable $number2 might not be defined.
return new self(name: $name, value_min: (float) $number, value_max: (float) $number2, value_text: $value_text2, unit: $unit, group: null);
return new self(name: $name, value_min: (float) $number, value_max: (float) $number2, value_text: $value_text2, unit: $unit, symbol: $symbol, group: $group);
}
}
//If it's a plus/minus value, we'll also treat it as a range
} elseif (str_starts_with($value, '±')) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [$value, null];
if (is_numeric($number)) {
return new self(name: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, group: null);
return new self(name: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, symbol: $symbol, group: $group);
}
}

0 comments on commit c9e391b

Please sign in to comment.