Skip to content

Commit

Permalink
Behat transliterator
Browse files Browse the repository at this point in the history
  • Loading branch information
cv65kr committed Nov 20, 2017
1 parent 2b4b2f5 commit 66ed9a5
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 54 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@
"twig/extensions": "~1.0",
"egulias/email-validator": "~1.2",
"box/spout": "^2.5",
"ruflin/elastica": "^5.1"
"ruflin/elastica": "^5.1",
"behat/transliterator": "~1.2"
},
"require-dev": {
"behat/behat": "3.1.0rc1",
Expand Down
2 changes: 1 addition & 1 deletion src/Kunstmaan/NodeBundle/Form/Type/SlugType.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public function getBlockPrefix()
public function buildView(FormView $view, FormInterface $form, array $options)
{
$nodeTranslation = $form->getParent()->getData();
$view->vars['reset'] = $this->slugifier->slugify($nodeTranslation->getTitle(), '');
$view->vars['reset'] = $this->slugifier->slugify($nodeTranslation->getTitle());
$parentNode = $nodeTranslation->getNode()->getParent();
if ($parentNode !== null) {
$nodeTranslation = $parentNode->getNodeTranslation($nodeTranslation->getLang(), true);
Expand Down
31 changes: 6 additions & 25 deletions src/Kunstmaan/UtilitiesBundle/Helper/Slugifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,41 +2,22 @@

namespace Kunstmaan\UtilitiesBundle\Helper;

use Behat\Transliterator\Transliterator;

/**
* Sulgifier is a helper to slugify a certain string
*/
class Slugifier implements SlugifierInterface
final class Slugifier implements SlugifierInterface
{
/**
* Slugify a string
*
* @param string $text Text to slugify
* @param string $default Default return value (override when slugify would return an empty string)
* @param string $text
*
* @return string
*/
public function slugify($text, $default = '', $replace = array("'"), $delimiter = '-')
public function slugify($text)
{
if (!empty($replace)) {
$text = str_replace($replace, ' ', $text);
}

// transliterate
if (class_exists('Transliterator')) {
$text = mb_convert_encoding((string)$text, 'UTF-8', mb_list_encodings());

$transliterator = \Transliterator::create('Any-Latin; Latin-ASCII');
$text = $transliterator->transliterate($text);
}

$text = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $text);
$text = strtolower(trim($text, $delimiter));
$text = preg_replace("/[\/_|+ -]+/", $delimiter, $text);

if (empty($text)) {
return empty($default) ? '' : $default;
}

return $text;
return Transliterator::transliterate($text);
}
}
5 changes: 1 addition & 4 deletions src/Kunstmaan/UtilitiesBundle/Helper/SlugifierInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@ interface SlugifierInterface

/**
* @param $text
* @param $default
* @param $replace
* @param $delimiter
* @return mixed
*/
public function slugify($text, $default = 'n-a', $replace = array("'"), $delimiter = '-');
public function slugify($text);

}
29 changes: 12 additions & 17 deletions src/Kunstmaan/UtilitiesBundle/Tests/Helper/SlugifierTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php

namespace Kunstmaan\NodeBundle\Tests;

use Kunstmaan\UtilitiesBundle\Helper\Slugifier;
use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;

Expand All @@ -25,18 +26,13 @@ public function setUp()
}
/**
* @param string $text The text to slugify
* @param string $default The default alternative
* @param string $result The slug it should generate
*
* @dataProvider getSlugifyData
*/
public function testSlugify($text, $default, $result)
public function testSlugify($text, $result)
{
if (!is_null($default)) {
$this->assertEquals($result, $this->slugifier->slugify($text, $default));
} else {
$this->assertEquals($result, $this->slugifier->slugify($text));
}
$this->assertEquals($result, $this->slugifier->slugify($text));
}

/**
Expand All @@ -46,16 +42,15 @@ public function testSlugify($text, $default, $result)
*/
public function getSlugifyData()
{
return array(
array('', '', ''),
array('', null, ''),
array('test', '', 'test'),
array('een titel met spaties', '', 'een-titel-met-spaties'),
array('à partir d\'aujourd\'hui', null, 'a-partir-d-aujourd-hui'),
array('CaPs ShOulD be LoweRCasEd', null, 'caps-should-be-lowercased'),
array('áàäåéèëíìïóòöúùüñßæ', null, 'aaaaeeeiiiooouuunssae'),
array('polish-ążśźęćńół', null, 'polish-azszecnol'),
);
return [
['', ''],
['test', 'test'],
['een titel met spaties', 'een-titel-met-spaties'],
['à partir d\'aujourd\'hui', 'a-partir-daujourdhui'],
['CaPs ShOulD be LoweRCasEd', 'caps-should-be-lowercased'],
['áàäåéèëíìïóòöúùüñßæ', 'aaaaeeeiiiooouuunssae'],
['polish-ążśźęćńół', 'polish-azszecnol']
];
}

/**
Expand Down
9 changes: 4 additions & 5 deletions src/Kunstmaan/UtilitiesBundle/Twig/UtilitiesTwigExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
namespace Kunstmaan\UtilitiesBundle\Twig;

use Kunstmaan\UtilitiesBundle\Helper\SlugifierInterface;
use Twig_Extension;


class UtilitiesTwigExtension extends Twig_Extension
class UtilitiesTwigExtension extends \Twig_Extension
{
/**
* @var SlugifierInterface
Expand All @@ -16,7 +15,7 @@ class UtilitiesTwigExtension extends Twig_Extension
/**
* @param $slugifier
*/
function __construct($slugifier)
public function __construct($slugifier)
{
$this->slugifier = $slugifier;
}
Expand All @@ -29,7 +28,7 @@ function __construct($slugifier)
public function getFilters()
{
return array(
new \Twig_SimpleFilter('slugify', array($this, 'slugify')),
new \Twig_SimpleFilter('slugify', [$this, 'slugify']),
);
}

Expand All @@ -40,6 +39,6 @@ public function getFilters()
*/
public function slugify($text)
{
return $this->slugifier->slugify($text, '');
return $this->slugifier->slugify($text);
}
}
3 changes: 2 additions & 1 deletion src/Kunstmaan/UtilitiesBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"require": {
"php": ">=5.5.0",
"symfony/symfony": "~2.8",
"doctrine/orm": "~2.2,>=2.2.3"
"doctrine/orm": "~2.2,>=2.2.3",
"behat/transliterator": "~1.2"
},
"suggest": {
"ekino/newrelic-bundle": "To use the UrlTransactionNamingStrategy"
Expand Down

0 comments on commit 66ed9a5

Please sign in to comment.