1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace allejo\stakx\Twig; |
4
|
|
|
|
5
|
|
|
use Twig_Environment; |
6
|
|
|
use Twig_Extension; |
7
|
|
|
use Twig_SimpleFilter; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* This file is part of Twig. |
11
|
|
|
* |
12
|
|
|
* (c) 2009 Fabien Potencier |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
* |
17
|
|
|
* @author Henrik Bjornskov <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
class TextExtension extends Twig_Extension |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* Returns a list of filters. |
23
|
|
|
* |
24
|
|
|
* @return Twig_SimpleFilter[] |
25
|
|
|
*/ |
26
|
14 |
|
public function getFilters() |
27
|
|
|
{ |
28
|
|
|
return array( |
29
|
14 |
|
new Twig_SimpleFilter('summary', array($this, 'twig_summary_filter')), |
30
|
14 |
|
new Twig_SimpleFilter('truncate', array($this, 'twig_truncate_filter'), array('needs_environment' => true)), |
31
|
14 |
|
new Twig_SimpleFilter('wordwrap', array($this, 'twig_wordwrap_filter'), array('needs_environment' => true)), |
32
|
|
|
); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Name of this extension. |
37
|
|
|
* |
38
|
|
|
* @return string |
39
|
|
|
*/ |
40
|
14 |
|
public function getName() |
41
|
|
|
{ |
42
|
14 |
|
return 'Text'; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function twig_summary_filter($value, $paragraphCount = 1) |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
if (function_exists('simplexml_load_string')) |
48
|
|
|
{ |
49
|
|
|
$content = simplexml_load_string('<html>' . $value . '</html>'); |
50
|
|
|
$count = min($paragraphCount, $content->count()); |
51
|
|
|
$children = $content->children(); |
52
|
|
|
|
53
|
|
|
$summary = ''; |
54
|
|
|
|
55
|
|
|
for ($i = 0; $i < $count; ++$i) |
56
|
|
|
{ |
57
|
|
|
$summary .= $children[$i]->asXml(); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $summary; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $value; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...') |
|
|
|
|
67
|
|
|
{ |
68
|
|
|
if (mb_strlen($value, $env->getCharset()) > $length) |
69
|
|
|
{ |
70
|
|
|
if ($preserve) |
71
|
|
|
{ |
72
|
|
|
// If breakpoint is on the last word, return the value without separator. |
73
|
|
|
if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) |
74
|
|
|
{ |
75
|
|
|
return $value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$length = $breakpoint; |
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
return rtrim(mb_substr($value, 0, $length, $env->getCharset())) . $separator; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
return $value; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$sentences = array(); |
90
|
|
|
|
91
|
|
|
$previous = mb_regex_encoding(); |
92
|
|
|
mb_regex_encoding($env->getCharset()); |
93
|
|
|
|
94
|
|
|
$pieces = mb_split($separator, $value); |
95
|
|
|
mb_regex_encoding($previous); |
96
|
|
|
|
97
|
|
|
foreach ($pieces as $piece) |
98
|
|
|
{ |
99
|
|
|
while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) |
100
|
|
|
{ |
101
|
|
|
$sentences[] = mb_substr($piece, 0, $length, $env->getCharset()); |
102
|
|
|
$piece = mb_substr($piece, $length, 2048, $env->getCharset()); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$sentences[] = $piece; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
return implode($separator, $sentences); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.