truncate() public static method

Truncate a string syntax: {$var|truncate:max-length[:append-hellip][:closest-word]}
public static truncate ( string $var = null, integer $length, boolean $useHellip = true, boolean $closestWord = false ) : string
$var string The string passed from the template.
$length integer The maximum length of the truncated string.
$useHellip boolean Should a hellip be appended if the length exceeds the requested length?
$closestWord boolean Truncate on exact length or on closest word?
return string
Esempio n. 1
0
 public function test_truncate()
 {
     $containerMock = $this->getMockBuilder('Symfony\\Component\\DependencyInjection\\ContainerInterface')->disableOriginalConstructor()->getMock();
     $containerMock->expects($this->any())->method('getParameter')->with('kernel.charset')->will($this->returnValue('UTF-8'));
     BackendModel::setContainer($containerMock);
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 3, false, true), 'foo');
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 4, false, true), 'foo');
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 8, false, true), 'foo bar');
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 100, false, true), 'foo bar baz qux');
     // Hellip
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 5, true, true), 'foo…');
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 14, true, true), 'foo bar baz…');
     $this->assertEquals(TemplateModifiers::truncate('foo bar baz qux', 15, true, true), 'foo bar baz qux');
 }