Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/regexp #45

Merged
merged 6 commits into from
Sep 29, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 31 additions & 3 deletions src/Slugify.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class Slugify implements SlugifyInterface
{
const LOWERCASE_NUMBERS_DASHES = '/([^a-z0-9]|-)+/';

/** @var array */
protected $rules = array(
// Numeric characters
Expand Down Expand Up @@ -459,6 +461,18 @@ class Slugify implements SlugifyInterface
)
);

/** @var string */
protected $regExp;

/**
*
* @param string $regExp
*/
public function __construct($regExp = self::LOWERCASE_NUMBERS_DASHES)
{
$this->regExp = $regExp;
}

/**
* Returns the slug-version of the string.
*
Expand All @@ -470,7 +484,7 @@ class Slugify implements SlugifyInterface
public function slugify($string, $separator = '-')
{
$string = strtolower(strtr($string, $this->rules));
$string = preg_replace('/([^a-z0-9]|-)+/', $separator, $string);
$string = preg_replace($this->regExp, $separator, $string);
$string = strtolower($string);

return trim($string, $separator);
Expand Down Expand Up @@ -546,13 +560,27 @@ public function getRulesets()
return $this->rulesets;
}

/**
* Sets the regular expression used to sanitize the slug
*
* @param string $regExp
*/
public function setRegExp($regExp)
{
$this->regExp = $regExp;

return $this;
}

/**
* Static method to create new instance of {@see Slugify}.
*
* @param string $regExp The regular expression to be applied to strings when calling slugify
*
* @return Slugify
*/
public static function create()
public static function create($regExp = self::LOWERCASE_NUMBERS_DASHES)
{
return new static();
return new static($regExp);
}
}
27 changes: 27 additions & 0 deletions tests/SlugifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,33 @@ public function createReturnsAnInstance()
$this->assertInstanceOf('Cocur\\Slugify\\SlugifyInterface', Slugify::create());
}

/**
* @test
* @covers Cocur\Slugify\Slugify::setRegExp()
*/
public function otherRegExpsProduceOtherResults()
{
$actual = 'File Name.tar.gz';
$expected = 'file-name.tar.gz';

$this->assertNotEquals($expected, $this->slugify->slugify($actual));
$this->slugify->setRegExp('/([^a-z0-9.]|-)+/');
$this->assertEquals($expected, $this->slugify->slugify($actual));
}

/**
* @test
* @covers Cocur\Slugify\Slugify::__construct()
*/
public function constructWithOtherRegexp()
{
$actual = 'File Name.tar.gz';
$expected = 'file-name.tar.gz';

$this->slugify = new Slugify('/([^a-z0-9.]|-)+/');
$this->assertEquals($expected, $this->slugify->slugify($actual));
}

public function provider()
{
return array(
Expand Down