-
Notifications
You must be signed in to change notification settings - Fork 0
/
YFormatter.php
52 lines (47 loc) · 1.41 KB
/
YFormatter.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
namespace yasheena\view;
use Yii;
use yii\i18n\Formatter;
use yii\helpers\Html;
class YFormatter extends Formatter
{
/**
* @inheritdoc
*/
public function init()
{
// Use the original fomatter settings also for this instance
$config = Yii::$app->getComponents();
$config = $config['formatter'];
unset($config['class']); // Ignore the class setting
Yii::configure($this, $config);
parent::init();
}
/**
* Formats the value as an HTML-encoded plain text. If the value contains \n or \r
* the first line will be returned only.
* @param string $value the value to be formatted.
* @return string the formatted result.
*/
public function asNtext1line($value, $maxlen = null)
{
if ($value === null) {
return $this->nullDisplay;
}
$suffix = '';
if ((false !== ($n = strpos($value, "\n"))) || (false !== ($n = strpos($value, "\r")))) {
if (($n > 0) && (substr($value, $n - 1, 1) == "\r")) {
$n--;
}
$value = substr($value, 0, $n - 1);
$suffix = ' ...';
}
if ($maxlen) {
if ((strlen($value) + strlen($suffix) > $maxlen)) {
$suffix = ' ...';
$value = substr($value, 0, $maxlen - strlen($suffix));
}
}
return Html::encode($value . $suffix);
}
}