Move TrimValue() base definition to AttributeDefinition class, assign DBObject::SetTrim old behavior to it

This commit is contained in:
Stephen Abello
2026-07-13 15:34:09 +02:00
parent e08897a7ca
commit 39cadd64b6
2 changed files with 31 additions and 0 deletions

View File

@@ -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

View File

@@ -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