From e50e45d6fa64aea8b6b96f60073fb36960322aa8 Mon Sep 17 00:00:00 2001 From: Kevin Decherf Date: Sat, 9 Sep 2017 19:34:41 +0200 Subject: [PATCH] Twig: add removeSchemeAndWww filter This twig filter removes scheme (only http and https are supported) and pass the result to removeWww filter to also remove 'www.' at the beginning of an url. Signed-off-by: Kevin Decherf --- .../CoreBundle/Twig/WallabagExtension.php | 8 ++++++ .../CoreBundle/Twig/WallabagExtensionTest.php | 27 +++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 351172c4..8992117e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -28,6 +28,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { return [ new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), + new \Twig_SimpleFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), ]; } @@ -45,6 +46,13 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return preg_replace('/^www\./i', '', $url); } + public function removeSchemeAndWww($url) + { + return $this->removeWww( + preg_replace('@^https?://@i', '', $url) + ); + } + /** * Return number of entries depending of the type (unread, archive, starred or all). * diff --git a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php index ceec4b37..27989346 100644 --- a/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php +++ b/tests/Wallabag/CoreBundle/Twig/WallabagExtensionTest.php @@ -30,4 +30,31 @@ class WallabagExtensionTest extends \PHPUnit_Framework_TestCase $this->assertSame('lemonde.fr', $extension->removeWww('lemonde.fr')); $this->assertSame('gist.github.com', $extension->removeWww('gist.github.com')); } + + public function testRemoveSchemeAndWww() + { + $entryRepository = $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository') + ->disableOriginalConstructor() + ->getMock(); + + $tagRepository = $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') + ->disableOriginalConstructor() + ->getMock(); + + $tokenStorage = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface') + ->disableOriginalConstructor() + ->getMock(); + + $translator = $this->getMockBuilder('Symfony\Component\Translation\TranslatorInterface') + ->disableOriginalConstructor() + ->getMock(); + + $extension = new WallabagExtension($entryRepository, $tagRepository, $tokenStorage, 0, $translator); + + $this->assertSame('lemonde.fr', $extension->removeSchemeAndWww('www.lemonde.fr')); + $this->assertSame('lemonde.fr', $extension->removeSchemeAndWww('http://lemonde.fr')); + $this->assertSame('lemonde.fr', $extension->removeSchemeAndWww('https://www.lemonde.fr')); + $this->assertSame('gist.github.com', $extension->removeSchemeAndWww('https://gist.github.com')); + $this->assertSame('ftp://gist.github.com', $extension->removeSchemeAndWww('ftp://gist.github.com')); + } } -- 2.41.0