├── .gitignore ├── HappyRSlugifyBundle.php ├── Resources └── config │ ├── twig.xml │ └── slugify.xml ├── .travis.yml ├── composer.json ├── DependencyInjection ├── Configuration.php └── HappyRSlugifyExtension.php ├── README.md ├── Twig └── SlugifyExtension.php ├── phpunit.xml.dist ├── Services └── SlugifyService.php └── Tests └── Services └── SlugifyServiceTest.php /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | .DS_Store 3 | build 4 | phpunit.xml 5 | composer.lock 6 | vendor 7 | -------------------------------------------------------------------------------- /HappyRSlugifyBundle.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: php 2 | 3 | php: 4 | - 5.3 5 | - 5.4 6 | - 5.5 7 | - 5.6 8 | - hhvm 9 | 10 | env: 11 | - SYMFONY_VERSION=2.3.* 12 | 13 | before_script: 14 | - composer self-update 15 | - composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source 16 | 17 | script: phpunit --coverage-text 18 | 19 | matrix: 20 | allow_failures: 21 | - env: SYMFONY_VERSION=dev-master 22 | - php: hhvm 23 | include: 24 | - php: 5.5 25 | env: SYMFONY_VERSION=2.4.* 26 | - php: 5.5 27 | env: SYMFONY_VERSION=2.5.* 28 | - php: 5.5 29 | env: SYMFONY_VERSION=dev-master 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /Resources/config/slugify.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /composer.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "happyr/slugify-bundle", 3 | "type": "symfony-bundle", 4 | "description": "A Symfony2 Bundle to slugify swedish texts", 5 | "keywords": ["Slugify", "Swedish"], 6 | "homepage": "http://developer.happyr.com/symfony2-bundles/slugify-bundle", 7 | "license": "MIT", 8 | "authors": [ 9 | { 10 | "name": "Tobias Nyholm", 11 | "email": "tobias@happyr.com" 12 | } 13 | ], 14 | "require": { 15 | "php": ">=5.3.2", 16 | "symfony/framework-bundle": "2.*", 17 | "jbroadway/urlify": "1.0.*" 18 | }, 19 | "require-dev": { 20 | "mockery/mockery": "*" 21 | }, 22 | "autoload": { 23 | "psr-0": { "HappyR\\SlugifyBundle": "" } 24 | }, 25 | "target-dir": "HappyR/SlugifyBundle" 26 | 27 | } 28 | -------------------------------------------------------------------------------- /DependencyInjection/Configuration.php: -------------------------------------------------------------------------------- 1 | root('happy_r_slugify'); 21 | 22 | $rootNode 23 | ->children() 24 | ->booleanNode('twig')->defaultFalse()->end() 25 | ->end() 26 | ; 27 | 28 | return $treeBuilder; 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /DependencyInjection/HappyRSlugifyExtension.php: -------------------------------------------------------------------------------- 1 | processConfiguration($configuration, $configs); 24 | 25 | $loader = new Loader\XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); 26 | $loader->load('slugify.xml'); 27 | 28 | if ($config['twig']) { 29 | $loader->load('twig.xml'); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | HappyR SlugifyBundle 2 | ===================== 3 | 4 | This bundle provides integration of the [URLify](https://github.com/jbroadway/urlify) library into Symfony2. 5 | A slugify service and twig filter is provided. This bundle is a modified version of the 6 | [ZenstruckSlugifyBundle](https://github.com/kbond/ZenstruckSlugifyBundle). The ZenstruckSlugifyBundle uses the 7 | [Slugify](https://github.com/cocur/slugify) library. 8 | 9 | ## Installation 10 | 11 | 1. Install with composer: 12 | 13 | ``` 14 | php composer.phar require happyr/slugify-bundle 15 | ``` 16 | 17 | 2. Enable the bundle: 18 | 19 | ```php 20 | // app/AppKernel.php 21 | 22 | public function registerBundles() 23 | { 24 | $bundles = array( 25 | // ... 26 | new HappyR\SlugifyBundle\HappyRSlugifyBundle(), 27 | ); 28 | } 29 | ``` 30 | 31 | ## Using the service 32 | 33 | ```php 34 | 35 | $slugifier = $this->container->get('happyr.slugify.slugifier'); 36 | $text = $slugifier->slugify('Hello World!'); 37 | echo $text; //prints "hello-world" 38 | ``` 39 | 40 | ## Using the Twig filter 41 | 42 | ```html 43 | {{ 'Hello World!'|slugify }} {# hello-world #} 44 | 45 | ``` 46 | 47 | ## Full Default Configuration 48 | 49 | ```yaml 50 | happy_r_slugify: 51 | twig: false #set to true to enable twig filter 52 | ``` -------------------------------------------------------------------------------- /Twig/SlugifyExtension.php: -------------------------------------------------------------------------------- 1 | 12 | * @author Tobias Nyholm 13 | * 14 | * 15 | */ 16 | class SlugifyExtension extends \Twig_Extension 17 | { 18 | /** 19 | * @var \HappyR\SlugifyBundle\Services\SlugifyService slugify 20 | * 21 | * 22 | */ 23 | protected $slugify; 24 | 25 | /** 26 | * @param SlugifyService $slugify 27 | */ 28 | public function __construct(SlugifyService $slugify) 29 | { 30 | $this->slugify = $slugify; 31 | } 32 | 33 | /** 34 | * Get twig filters 35 | * 36 | * 37 | * @return array 38 | */ 39 | public function getFilters() 40 | { 41 | return array( 42 | new \Twig_SimpleFilter('slugify', array($this, 'slugify')), 43 | ); 44 | } 45 | 46 | /** 47 | * Do the actual slugify 48 | * 49 | * @param string $text 50 | * 51 | * @return string 52 | */ 53 | public function slugify($text) 54 | { 55 | return $this->slugify->slugify($text); 56 | } 57 | 58 | /** 59 | * 60 | * The name 61 | * 62 | * @return string 63 | */ 64 | public function getName() 65 | { 66 | return 'happyr_slugify'; 67 | } 68 | 69 | } 70 | -------------------------------------------------------------------------------- /phpunit.xml.dist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 15 | 16 | 17 | 18 | 19 | ./Tests 20 | 21 | 22 | 23 | 24 | 26 | 27 | 28 | 29 | 30 | 31 | ./vendor/ 32 | ./Tests/ 33 | ./DataFixtures/ 34 | ./Resources/ 35 | ./DependencyInjection/ 36 | 37 | 38 | ./ 39 | 40 | 41 | 42 | 43 | 44 | live 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /Services/SlugifyService.php: -------------------------------------------------------------------------------- 1 | urlify = $urlify; 28 | } 29 | 30 | 31 | /** 32 | * Transliterates characters to their ASCII equivalents. 33 | * 34 | * @param string $text 35 | * @param string $language 36 | * 37 | * @return mixed 38 | */ 39 | public function downcode($text, $language = ""){ 40 | return $this->urlify->downcode($text, $language); 41 | } 42 | 43 | /** 44 | * Filters a string, e.g., "Petty theft" to "petty-theft" 45 | * 46 | * @param string $text 47 | * @param int $length 48 | * @param string $language 49 | * @param bool $fileName 50 | * 51 | * @return string 52 | */ 53 | public function filter($text, $length = 60, $language = "", $fileName = false){ 54 | return $this->urlify->filter($text, $length, $language, $fileName); 55 | } 56 | 57 | 58 | /** 59 | * Alias of filter 60 | * 61 | * @param string $text 62 | * 63 | * @return string 64 | */ 65 | public function slugify($text){ 66 | return $this->filter($text); 67 | } 68 | 69 | /** 70 | * Alias of downcode 71 | * 72 | * @param string $text 73 | * 74 | * @return mixed 75 | */ 76 | public function transliterate($text){ 77 | return $this->urlify->transliterate($text); 78 | } 79 | } 80 | -------------------------------------------------------------------------------- /Tests/Services/SlugifyServiceTest.php: -------------------------------------------------------------------------------- 1 | shouldReceive('downcode')->with('foo', 'en')->andReturn('bar'); 32 | $slugifier = new SlugifyService(self::$urlify); 33 | 34 | $result=$slugifier->downcode('foo', 'en'); 35 | 36 | $this->assertEquals('bar', $result); 37 | } 38 | 39 | /** 40 | * Test filter 41 | */ 42 | public function testFilter() 43 | { 44 | self::$urlify->shouldReceive('filter')->with('foo', 4711, 'en', false)->andReturn('bar'); 45 | $slugifier = new SlugifyService(self::$urlify); 46 | 47 | $result=$slugifier->filter('foo', 4711, 'en', false); 48 | 49 | $this->assertEquals('bar', $result); 50 | } 51 | 52 | /** 53 | * Test transliterate 54 | */ 55 | public function testTransliterate() 56 | { 57 | 58 | self::$urlify->shouldReceive('transliterate')->with('foo')->andReturn('bar'); 59 | $slugifier = new SlugifyService(self::$urlify); 60 | 61 | $result=$slugifier->transliterate('foo'); 62 | 63 | $this->assertEquals('bar', $result); 64 | } 65 | 66 | /** 67 | * Test slugify 68 | */ 69 | public function testSlugify() 70 | { 71 | $slugifier=m::mock('HappyR\SlugifyBundle\Services\SlugifyService[filter]',array(self::$urlify)); 72 | $slugifier->shouldReceive('filter')->with('foo')->andReturn('bar'); 73 | 74 | $result=$slugifier->slugify('foo'); 75 | $this->assertEquals('bar', $result); 76 | 77 | } 78 | 79 | 80 | } 81 | --------------------------------------------------------------------------------