/
Ellipsis.php
159 lines (135 loc) · 3.58 KB
/
Ellipsis.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/**
* @author Stefano Torresi (http://stefanotorresi.it)
* @license See the file LICENSE.txt for copying permission.
* ************************************************
*/
namespace MyBase\Filter;
use Traversable;
use Zend\Filter\AbstractFilter;
use Zend\Stdlib\StringUtils;
use Zend\Stdlib\StringWrapper\StringWrapperInterface;
class Ellipsis extends AbstractFilter
{
/**
* @var StringWrapperInterface
*/
protected $stringWrapper;
/**
* @var int
*/
protected $options = array(
'maxLength' => null,
'ellipsis' => ' […]',
'encoding' => 'UTF-8',
);
/**
* @param mixed $maxLengthOrOptions
*/
public function __construct($maxLengthOrOptions = null)
{
if ($maxLengthOrOptions !== null) {
if (! is_array($maxLengthOrOptions) && ! $maxLengthOrOptions instanceof Traversable) {
$this->setMaxLength($maxLengthOrOptions);
} else {
$this->setOptions($maxLengthOrOptions);
}
}
}
/**
* Truncates a string to the next white space after the maximum size, and append an elision suffix
* Notes:
* - truncation is done by excess, so the limit is a soft one
* - elision length is taken into account with the max length
*
* @param string $value
* @return string
*/
public function filter($value)
{
if (!is_string($value)) {
return $value;
}
$sw = $this->getStringWrapper();
if ($sw->strlen($value) <= $this->getMaxLength()) {
return $value;
}
$maxLength = $this->getMaxLength() - $sw->strlen($this->getEllipsis());
// find the first space in the elided part
$boundary = $sw->strpos($value, ' ', $maxLength);
if ($boundary === false) {
$boundary = $maxLength;
}
return rtrim($sw->substr($value, 0, $boundary)) . $this->getEllipsis();
}
/**
* @return mixed
*/
public function getMaxLength()
{
return $this->options['maxLength'];
}
/**
* @param int $maxLength
* @return $this
*/
public function setMaxLength($maxLength)
{
if ($maxLength !== null) {
$maxLength = (int) $maxLength;
}
$this->options['maxLength'] = $maxLength;
return $this;
}
/**
* @return mixed
*/
public function getEllipsis()
{
return $this->options['ellipsis'];
}
/**
* @param string $character
* @return $this
*/
public function setEllipsis($character)
{
$this->options['ellipsis'] = $character;
return $this;
}
/**
* @return string
*/
public function getEncoding()
{
return $this->options['encoding'];
}
/**
* @param string $encoding
* @return $this
*/
public function setEncoding($encoding)
{
$this->options['encoding'] = $encoding;
$this->stringWrapper = StringUtils::getWrapper($encoding);
return $this;
}
/**
* @return StringWrapperInterface
*/
public function getStringWrapper()
{
if (!$this->stringWrapper) {
$this->stringWrapper = StringUtils::getWrapper($this->getEncoding());
}
return $this->stringWrapper;
}
/**
* @param StringWrapperInterface $stringWrapper
*/
public function setStringWrapper(StringWrapperInterface $stringWrapper)
{
$stringWrapper->setEncoding($this->getEncoding());
$this->stringWrapper = $stringWrapper;
}
}