Completed
Push — master ( 13c4ce...bedf48 )
by Dev
05:57
created

AppExtension::renderTxtAnchor()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 2
dl 0
loc 6
ccs 0
cts 5
cp 0
crap 2
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace PiedWeb\CMSBundle\Twig;
4
5
use Cocur\Slugify\Slugify;
6
use PiedWeb\CMSBundle\Service\PageCanonicalService;
7
use PiedWeb\RenderAttributes\AttributesTrait;
8
use Twig\Extension\AbstractExtension;
9
use Twig\TwigFilter;
10
use Twig\TwigFunction;
11
use Twig_Environment;
12
13
class AppExtension extends AbstractExtension
14
{
15
    use AttributesTrait;
16
17
    public function __construct(PageCanonicalService $pageCanonical)
18
    {
19
        $this->pageCanonical = $pageCanonical;
20
    }
21
22
    public function getFilters()
23
    {
24
        return [
25
            new TwigFilter('html_entity_decode', 'html_entity_decode'),
26
            new TwigFilter(
27
                'punctuation_beautifer',
28
                [AppExtension::class, 'punctuationBeautifer'],
29
                ['is_safe' => ['html']]
30
            ),
31
        ];
32
    }
33
34
    public function getFunctions()
35
    {
36
        return [
37
            new TwigFunction('homepage', [$this->pageCanonical, 'generatePathForHomepage']),
38
            new TwigFunction('page', [$this->pageCanonical, 'generatePathForPage']),
39
            new TwigFunction('jslink', [AppExtension::class, 'renderJavascriptLink'], [
40
                'is_safe' => ['html'],
41
                'needs_environment' => true,
42
            ]),
43
            new TwigFunction('link', [AppExtension::class, 'renderJavascriptLink'], [
44
                'is_safe' => ['html'],
45
                'needs_environment' => true,
46
            ]),
47
            new TwigFunction('mail', [AppExtension::class, 'renderEncodedMail'], [
48
                'is_safe' => ['html'],
49
                'needs_environment' => true,
50
            ]),
51
            new TwigFunction(
52
                'bookmark', // = anchor
53
                [AppExtension::class, 'renderTxtAnchor'],
54
                ['is_safe' => ['html'], 'needs_environment' => true]
55
            ),
56
            new TwigFunction(
57
                'anchor', // = bookmark
58
                [AppExtension::class, 'renderTxtAnchor'],
59
                ['is_safe' => ['html'], 'needs_environment' => true]
60
            ),
61
        ];
62
    }
63
64
    public static function renderTxtAnchor(Twig_Environment $env, $name)
65
    {
66
        $slugify = new Slugify();
67
        $name = $slugify->slugify($name);
68
69
        return $env->render('@PiedWebCMS/component/_txt_bookmark.html.twig', ['name' => $name]);
70
    }
71
72
    public static function renderEncodedMail(Twig_Environment $env, $mail)
73
    {
74
        return $env->render('@PiedWebCMS/component/_encoded_mail.html.twig', ['mail' => str_rot13($mail)]);
75
    }
76
77
    public static function punctuationBeautifer($text)
78
    {
79
        return str_replace(
80
            [' ;', ' :', ' ?', ' !', '« ', ' »', '&laquo; ', ' &raquo;'],
81
            ['&nbsp;;', '&nbsp;:', '&nbsp;?', '&nbsp;!', '«&nbsp;', '&nbsp;»', '&laquo;&nbsp;', '&nbsp;&raquo;'],
82
            $text
83
        );
84
    }
85
86
    public static function renderJavascriptLink(Twig_Environment $env, $anchor, $path, $attr = [])
87
    {
88
        if (0 === strpos($path, 'http://')) {
89
            $path = '-'.substr($path, 7);
90
        } elseif (0 === strpos($path, 'https://')) {
91
            $path = '_'.substr($path, 8);
92
        } elseif (0 === strpos($path, 'mailto:')) {
93
            $path = '@'.substr($path, 7);
94
        }
95
96
        return $env->render('@PiedWebCMS/component/_javascript_link.html.twig', [
97
            'attr' => self::mergeAndMapAttributes($attr, ['data-rot' => str_rot13($path)]),
98
            'anchor' => $anchor,
99
        ]);
100
    }
101
}
102