Completed
Push — master ( c2b5cc...89bfcf )
by Vladimir
03:33
created

TextExtension::getFilters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 8
ccs 4
cts 4
cp 1
rs 9.4285
c 1
b 0
f 0
cc 1
eloc 5
nc 1
nop 0
crap 1
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)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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 = '...')
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

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.

Loading history...
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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;
0 ignored issues
show
Coding Style introduced by
Consider using a different name than the parameter $length. This often makes code more readable.
Loading history...
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)
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
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