Задача: применить последовательный вызов методов к CFormatter'у
Для начала переопределим format в конфиге приложения
Код: Выделить всё
'format' => array(
'class' => 'ChainableFormatter',
),
Код: Выделить всё
class ChainableFormatter extends CFormatter
{
/**
* @var string
*/
protected $value = NULL;
public function __call( $name, array $arguments )
{
if( NULL !== $this->value )
{
array_unshift( $arguments, $this->value );
$this->value = parent::__call( $name, $arguments );
return $this;
}
return parent::__call( $name, $arguments );
}
public function __toString()
{
if( NULL !== $this->value )
{
$tmp = $this->value;
$this->value = NULL;
return (string)$tmp;
}
}
public function chain( $string )
{
$this->value = $string;
return $this;
}
static function formatPlural( $n, $form1, $form2, $form5 )
{
$n = abs( $n ) % 100;
$n1 = $n % 10;
if( $n > 10 && $n < 20 )
{
return $form5;
}
if( $n1 > 1 && $n1 < 5 )
{
return $form2;
}
if( $n1 == 1 )
{
return $form1;
}
return $form5;
}
}
Код: Выделить всё
Yii::app()->format->text('string<br>string');
Yii::app()->format->chain("string\string")->ntext()->text();