diff --git a/core/attributedef/AttributeDecimal.php b/core/attributedef/AttributeDecimal.php new file mode 100644 index 000000000..69cc6aa3e --- /dev/null +++ b/core/attributedef/AttributeDecimal.php @@ -0,0 +1,157 @@ +Get('digits') . "," . $this->Get('decimals') . ")" . ($bFullSpec ? $this->GetSQLColSpec() : ''); + } + + public function GetValidationPattern() + { + $iNbDigits = $this->Get('digits'); + $iPrecision = $this->Get('decimals'); + $iNbIntegerDigits = $iNbDigits - $iPrecision; + + return "^[\-\+]?\d{1,$iNbIntegerDigits}(\.\d{0,$iPrecision})?$"; + } + + /** + * @inheritDoc + * @since 3.2.0 + */ + public function CheckFormat($value) + { + $sRegExp = $this->GetValidationPattern(); + return preg_match("/$sRegExp/", $value); + } + + public function GetBasicFilterOperators() + { + return array( + "!=" => "differs from", + "=" => "equals", + ">" => "greater (strict) than", + ">=" => "greater than", + "<" => "less (strict) than", + "<=" => "less than", + "in" => "in" + ); + } + + public function GetBasicFilterLooseOperator() + { + // Unless we implement an "equals approximately..." or "same order of magnitude" + return "="; + } + + public function GetBasicFilterSQLExpr($sOpCode, $value) + { + $sQValue = CMDBSource::Quote($value); + switch ($sOpCode) { + case '!=': + return $this->GetSQLExpr() . " != $sQValue"; + break; + case '>': + return $this->GetSQLExpr() . " > $sQValue"; + break; + case '>=': + return $this->GetSQLExpr() . " >= $sQValue"; + break; + case '<': + return $this->GetSQLExpr() . " < $sQValue"; + break; + case '<=': + return $this->GetSQLExpr() . " <= $sQValue"; + break; + case 'in': + if (!is_array($value)) { + throw new CoreException("Expected an array for argument value (sOpCode='$sOpCode')"); + } + + return $this->GetSQLExpr() . " IN ('" . implode("', '", $value) . "')"; + break; + + case '=': + default: + return $this->GetSQLExpr() . " = \"$value\""; + } + } + + public function GetNullValue() + { + return null; + } + + public function IsNull($proposedValue) + { + return is_null($proposedValue); + } + + /** + * @inheritDoc + */ + public function HasAValue($proposedValue): bool + { + return utils::IsNotNullOrEmptyString($proposedValue); + } + + public function MakeRealValue($proposedValue, $oHostObj) + { + if (is_null($proposedValue)) { + return null; + } + if ($proposedValue === '') { + return null; + } + + return $this->ScalarToSQL($proposedValue); + } + + public function ScalarToSQL($value) + { + assert(is_null($value) || preg_match('/' . $this->GetValidationPattern() . '/', $value)); + + if (!is_null($value) && ($value !== '')) { + $value = sprintf("%1." . $this->Get('decimals') . "F", $value); + } + return $value; // null or string + } +} \ No newline at end of file