diff --git a/app/app.php b/app/app.php index 9a0bf2b..1f1cc05 100644 --- a/app/app.php +++ b/app/app.php @@ -3,6 +3,7 @@ use Knp\Provider\ConsoleServiceProvider; use Poche\Api\EntryApi; use Poche\Api\ContentFullTextRssApi; use Poche\Repository\EntryRepository; +use Poche\Twig; use Symfony\Component\Translation\Loader\PoFileLoader; use Silex\Provider\SessionServiceProvider; @@ -21,21 +22,17 @@ $app->before(function () use ($app) { }); $app['twig'] = $app->share($app->extend('twig', function($twig) { - $twig->addFilter(new Twig_SimpleFilter('getDomain', function ($url) { - return parse_url($url, PHP_URL_HOST); - })); + $twig->addFilter(new Twig_SimpleFilter('getDomain', 'Poche\Twig\Filter::getDomain')); return $twig; })); $app['twig'] = $app->share($app->extend('twig', function($twig) { - $twig->addFilter(new Twig_SimpleFilter('getReadingTime', function ($text) { - $word = str_word_count(strip_tags($text)); - $minutes = floor($word / 200); - $seconds = floor($word % 200 / (200 / 60)); - $time = array('minutes' => $minutes, 'seconds' => $seconds); + $twig->addFilter(new Twig_SimpleFilter('getReadingTime', 'Poche\Twig\Filter::getReadingTime')); + return $twig; +})); - return $minutes; - })); +$app['twig'] = $app->share($app->extend('twig', function($twig) { + $twig->addFilter(new Twig_SimpleFilter('getPicture', 'Poche\Twig\Filter::getPicture')); return $twig; })); diff --git a/src/Poche/Twig/Filter.php b/src/Poche/Twig/Filter.php new file mode 100644 index 0000000..a950a14 --- /dev/null +++ b/src/Poche/Twig/Filter.php @@ -0,0 +1,36 @@ + + * @copyright 2013 + * @license http://www.wtfpl.net/ see COPYING file + */ + +namespace Poche\Twig; + +class Filter +{ + public function getDomain($url) + { + return parse_url($url, PHP_URL_HOST); + } + + public function getReadingTime($text) + { + $word = str_word_count(strip_tags($text)); + $minutes = floor($word / 200); + $seconds = floor($word % 200 / (200 / 60)); + $time = array('minutes' => $minutes, 'seconds' => $seconds); + + return $minutes; + } + + public function getPicture($text) + { + $output = preg_match('//Ui', $text, $result); + + return $output ? $result[1] : ''; + } +} \ No newline at end of file