Added tests and fixed certain edge cases in parsing parameters

This commit is contained in:
Jan Böhmer 2024-04-15 22:18:17 +02:00
parent 2b80ec8805
commit ff03f8217a
2 changed files with 63 additions and 6 deletions

View file

@ -44,7 +44,7 @@ class ParameterDTO
/** /**
* This function tries to decide on the value, if it is a numerical value (which is then stored in one of the value_*) fields) or a text value (which is stored in value_text). * This function tries to decide on the value, if it is a numerical value (which is then stored in one of the value_*) fields) or a text value (which is stored in value_text).
* It is possible to give ranges like 1...2 here, which will be parsed as value_min: 1.0, value_max: 2.0. * It is possible to give ranges like 1...2 (or 1~2) here, which will be parsed as value_min: 1.0, value_max: 2.0.
* @param string $name * @param string $name
* @param string|float $value * @param string|float $value
* @param string|null $unit * @param string|null $unit
@ -54,6 +54,7 @@ class ParameterDTO
*/ */
public static function parseValueField(string $name, string|float $value, ?string $unit = null, ?string $symbol = null, ?string $group = null): self public static function parseValueField(string $name, string|float $value, ?string $unit = null, ?string $symbol = null, ?string $group = null): self
{ {
//If we encounter something like 2.5@text, then put the "@text" into text_value and continue with the number parsing
if (is_string($value) && preg_match('/^(.+)(@.+)$/', $value, $matches) === 1) { if (is_string($value) && preg_match('/^(.+)(@.+)$/', $value, $matches) === 1) {
$value = $matches[1]; $value = $matches[1];
$value_text = $matches[2]; $value_text = $matches[2];
@ -61,8 +62,10 @@ class ParameterDTO
$value_text = null; $value_text = null;
} }
//If the value is just a number, we assume thats the typical value
if (is_float($value) || is_numeric($value)) { if (is_float($value) || is_numeric($value)) {
return new self($name, value_typ: (float) $value, value_text: $value_text, unit: $unit, symbol: $symbol, group: $group); return new self($name, value_text: $value_text, value_typ: (float) $value, unit: $unit, symbol: $symbol,
group: $group);
} }
//If the attribute contains "..." or a tilde we assume it is a range //If the attribute contains "..." or a tilde we assume it is a range
@ -75,8 +78,9 @@ class ParameterDTO
} else { } else {
$number = $parts[0]; $number = $parts[0];
} }
// If the second part has some extra info, we'll save that into value_text // 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) { if (!empty($unit) && preg_match('/^(.+' . preg_quote($unit, '/') . ')\s*(.+)$/', $parts[1], $matches) > 0) {
$parts[1] = $matches[1]; $parts[1] = $matches[1];
$value_text2 = $matches[2]; $value_text2 = $matches[2];
} else { } else {
@ -86,12 +90,13 @@ class ParameterDTO
//If both parts have the same unit and both values are numerical, we'll save it as range //If both parts have the same unit and both values are numerical, we'll save it as range
if ($unit === $unit2 && is_numeric($number) && is_numeric($number2)) { if ($unit === $unit2 && is_numeric($number) && is_numeric($number2)) {
return new self(name: $name, value_min: (float) $number, value_max: (float) $number2, value_text: $value_text2, unit: $unit, symbol: $symbol, group: $group); return new self(name: $name, value_text: $value_text2, value_min: (float) $number,
value_max: (float) $number2, unit: $unit, symbol: $symbol, group: $group);
} }
} }
//If it's a plus/minus value, we'll also treat it as a range //If it's a plus/minus value, we'll also treat it as a range
} elseif (str_starts_with($value, '±')) { } elseif (str_starts_with($value, '±')) {
[$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [$value, null]; [$number, $unit] = self::splitIntoValueAndUnit(ltrim($value, " ±")) ?? [ltrim($value, ' ±'), $unit];
if (is_numeric($number)) { if (is_numeric($number)) {
return new self(name: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, symbol: $symbol, group: $group); return new self(name: $name, value_min: -abs((float) $number), value_max: abs((float) $number), unit: $unit, symbol: $symbol, group: $group);
} }
@ -104,7 +109,8 @@ class ParameterDTO
//Were we successful in trying to reduce the value to a number? //Were we successful in trying to reduce the value to a number?
if ($value_text !== null && is_numeric($value)) { if ($value_text !== null && is_numeric($value)) {
return new self($name, value_typ: (float) $value, value_text: $value_text, unit: $unit, symbol: $symbol, group: $group); return new self($name, value_text: $value_text, value_typ: (float) $value, unit: $unit, symbol: $symbol,
group: $group);
} }
return new self($name, value_text: $value.$value_text, unit: $unit, symbol: $symbol, group: $group); return new self($name, value_text: $value.$value_text, unit: $unit, symbol: $symbol, group: $group);

View file

@ -67,6 +67,56 @@ class ParameterDTOTest extends TestCase
'm', 'm',
'test' 'test'
]; ];
//Test ranges with tilde
yield [
new ParameterDTO('test', value_min: -1.0, value_max: 2.0, unit: 'kg', symbol: 'm', group: 'test'),
'test',
'-1.0~+2.0', //Leading signs are parsed correctly
'kg',
'm',
'test'
];
//Test ranges with comment
yield [
new ParameterDTO('test', value_text: "Test", value_min: -1.0, value_max: 2.0, unit: 'kg', symbol: 'm',
group: 'test'),
'test',
'-1.0~+2.0 kg Test', //Leading signs are parsed correctly
'kg',
'm',
'test'
];
//Test @comment
yield [
new ParameterDTO('test', value_text: "@comment", value_typ: 1.0, unit: 'kg', symbol: 'm', group: 'test'),
'test',
'1.0@comment',
'kg',
'm',
'test'
];
//Test plus minus range (without unit)
yield [
new ParameterDTO('test', value_min: -1.0, value_max: +1.0, unit: 'kg', symbol: 'm', group: 'test'),
'test',
'±1.0',
'kg',
'm',
'test'
];
yield [ //And with unit
new ParameterDTO('test', value_min: -1.0, value_max: +1.0, unit: 'kg', symbol: 'm', group: 'test'),
'test',
'±1.0kg',
'kg',
'm',
'test'
];
} }
public function parseValueIncludingUnitDataProvider(): \Generator public function parseValueIncludingUnitDataProvider(): \Generator
@ -175,6 +225,7 @@ class ParameterDTOTest extends TestCase
$this->assertEquals(["70", ""], ParameterDTO::splitIntoValueAndUnit("70℃")); $this->assertEquals(["70", ""], ParameterDTO::splitIntoValueAndUnit("70℃"));
$this->assertEquals(["-5.0", "kg"], ParameterDTO::splitIntoValueAndUnit("-5.0 kg")); $this->assertEquals(["-5.0", "kg"], ParameterDTO::splitIntoValueAndUnit("-5.0 kg"));
$this->assertEquals(["-5.0", "µg"], ParameterDTO::splitIntoValueAndUnit("-5.0 µg"));
$this->assertNull(ParameterDTO::splitIntoValueAndUnit('kg')); $this->assertNull(ParameterDTO::splitIntoValueAndUnit('kg'));
$this->assertNull(ParameterDTO::splitIntoValueAndUnit('Test')); $this->assertNull(ParameterDTO::splitIntoValueAndUnit('Test'));