1<?php
2
3/**
4 * This file is part of Twig.
5 *
6 * (c) 2009 Fabien Potencier
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12/**
13 * @author Henrik Bjornskov <hb@peytz.dk>
14 */
15class Twig_Extensions_Extension_Text extends Twig_Extension
16{
17    /**
18     * {@inheritdoc}
19     */
20    public function getFilters()
21    {
22        return array(
23            new Twig_SimpleFilter('truncate', 'twig_truncate_filter', array('needs_environment' => true)),
24            new Twig_SimpleFilter('wordwrap', 'twig_wordwrap_filter', array('needs_environment' => true)),
25        );
26    }
27
28    /**
29     * {@inheritdoc}
30     */
31    public function getName()
32    {
33        return 'Text';
34    }
35}
36
37if (function_exists('mb_get_info')) {
38    function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
39    {
40        if (mb_strlen($value, $env->getCharset()) > $length) {
41            if ($preserve) {
42                // If breakpoint is on the last word, return the value without separator.
43                if (false === ($breakpoint = mb_strpos($value, ' ', $length, $env->getCharset()))) {
44                    return $value;
45                }
46
47                $length = $breakpoint;
48            }
49
50            return rtrim(mb_substr($value, 0, $length, $env->getCharset())).$separator;
51        }
52
53        return $value;
54    }
55
56    function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
57    {
58        $sentences = array();
59
60        $previous = mb_regex_encoding();
61        mb_regex_encoding($env->getCharset());
62
63        $pieces = mb_split($separator, $value);
64        mb_regex_encoding($previous);
65
66        foreach ($pieces as $piece) {
67            while (!$preserve && mb_strlen($piece, $env->getCharset()) > $length) {
68                $sentences[] = mb_substr($piece, 0, $length, $env->getCharset());
69                $piece = mb_substr($piece, $length, 2048, $env->getCharset());
70            }
71
72            $sentences[] = $piece;
73        }
74
75        return implode($separator, $sentences);
76    }
77} else {
78    function twig_truncate_filter(Twig_Environment $env, $value, $length = 30, $preserve = false, $separator = '...')
79    {
80        if (strlen($value) > $length) {
81            if ($preserve) {
82                if (false !== ($breakpoint = strpos($value, ' ', $length))) {
83                    $length = $breakpoint;
84                }
85            }
86
87            return rtrim(substr($value, 0, $length)).$separator;
88        }
89
90        return $value;
91    }
92
93    function twig_wordwrap_filter(Twig_Environment $env, $value, $length = 80, $separator = "\n", $preserve = false)
94    {
95        return wordwrap($value, $length, $separator, !$preserve);
96    }
97}
98
99class_alias('Twig_Extensions_Extension_Text', 'Twig\Extensions\TextExtension', false);
100