Update AbstractParameter.php

Make lazy null conditionals explicit.
Try to handle LaTeX special chars gracefully.
Fixes #958
This commit is contained in:
d-buchmann 2025-07-01 14:36:31 +02:00
parent c44535990b
commit 186e0c8561

View file

@ -217,7 +217,7 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu
$str = '';
$bracket_opened = false;
if ($this->value_typical) {
if ($this->value_typical !== null) {
$str .= $this->getValueTypicalWithUnit($latex_formatted);
if ($this->value_min || $this->value_max) {
$bracket_opened = true;
@ -225,11 +225,11 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu
}
}
if ($this->value_max && $this->value_min) {
if ($this->value_max !== null && $this->value_min !== null) {
$str .= $this->getValueMinWithUnit($latex_formatted).' ... '.$this->getValueMaxWithUnit($latex_formatted);
} elseif ($this->value_max) {
} elseif ($this->value_max !== null) {
$str .= 'max. '.$this->getValueMaxWithUnit($latex_formatted);
} elseif ($this->value_min) {
} elseif ($this->value_min !== null) {
$str .= 'min. '.$this->getValueMinWithUnit($latex_formatted);
}
@ -449,7 +449,15 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu
if (!$with_latex) {
$unit = $this->unit;
} else {
$unit = '$\mathrm{'.$this->unit.'}$';
// if chars occur that will mess up the latex formatting, display them plain, regardless of the desired output
if (str_contains($this->unit, '~') || str_contains($this->unit, '^') || str_contains($this->unit, '\\'))
{
$unit = $this->unit;
}
else // otherwise, style the chars with latex, but escape latex special chars
{
$unit = '$\mathrm{'.$this->latexEscape($this->unit).'}$';
}
}
return $str.' '.$unit;
@ -457,7 +465,15 @@ abstract class AbstractParameter extends AbstractNamedDBElement implements Uniqu
return $str;
}
/**
* Return a latex escaped string
*/
protected function latexEscape(string $latex): string
{
return preg_replace('/(\&|\%|\$|\#|\_|\{|\})/', "\\\\$1", $latex);
}
/**
* Returns the class of the element that is allowed to be associated with this attachment.
* @return string