diff --git a/core/attributedef.class.inc.php b/core/attributedef.class.inc.php index 8174cc391b..57f249289e 100644 --- a/core/attributedef.class.inc.php +++ b/core/attributedef.class.inc.php @@ -897,6 +897,26 @@ abstract class AttributeDefinition return null; } + /** + * Helper to set a value that fits the attribute max size + * + * Default behavior is what DBObject::SetTrim used to do, now delegated to AttributeDefinition + * + * @param string $sValue + * + * @return string + * @since 3.2.0 N°9643 + */ + public function TrimValue(string $sValue) + { + $iMaxSize = $this->GetMaxSize(); + if ($iMaxSize && (strlen($sValue) > $iMaxSize)) + { + $sValue = substr($sValue, 0, $iMaxSize); + } + return $sValue; + } + /** * @return mixed|null * @deprecated never used diff --git a/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectTest.php b/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectTest.php index da8316add8..ed33740987 100644 --- a/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectTest.php +++ b/tests/php-unit-tests/unitary-tests/core/DBObject/DBObjectTest.php @@ -1387,6 +1387,17 @@ class DBObjectTest extends ItopDataTestCase $this->assertEquals($sResult, $oOrganisation->Get('name'), 'SetTrim must limit string to 255 characters'); } + /** + * @covers DBObject::SetTrim + */ + public function testSetTrimOnNonStringAttributeDoesNotTrim() + { + $oTicket = MetaModel::NewObject(UserRequest::class); + $oTicket->SetTrim('caller_id', '15'); + + $this->assertEquals(15, $oTicket->Get('caller_id'), 'SetTrim should keep non-string attributes untouched before regular Set conversion'); + } + /** * @covers DBObject::SetComputedDate * @return void