aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/Wallabag/CoreBundle/Twig/Extension/WallabagExtension.php
blob: 18388948edb01ab78a48cc52b045aa888323f906 (plain) (blame)
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
<?php

namespace Wallabag\CoreBundle\Twig\Extension;

class WallabagExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            new \Twig_SimpleFilter('readingTime', array($this, 'getReadingTime')),
            new \Twig_SimpleFilter('domainName', array($this, 'getDomainName')),
        );
    }

    /**
     * Returns the domain name for a URL.
     *
     * @param $url
     *
     * @return string
     */
    public static function getDomainName($url)
    {
        return parse_url($url, PHP_URL_HOST);
    }

    /**
     * For a given text, we calculate reading time for an article.
     *
     * @param $text
     *
     * @return float
     */
    public static function getReadingTime($text)
    {
        return floor(str_word_count(strip_tags($text)) / 200);
    }

    public function getName()
    {
        return 'wallabag_extension';
    }
}