Skip to content

Commit

Permalink
Allow to modify options without creating a new object (#133)
Browse files Browse the repository at this point in the history
* Allow to pass an options array to the slugify() method.

* Also update the README.md file to document the new feature.

* Also allow to use a custom ruleset without touching the default rules.

* Fix the coding style.
  • Loading branch information
leofeyer authored and Florian Eckerstorfer committed Aug 16, 2016
1 parent 57a00e0 commit 11c0566
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 13 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,31 @@ $slugify = new Slugify(['separator' => '_']);
$slugify->slugify('Hello World'); // -> "hello_world"
```

### Changing options on the fly

You can overwrite any of the above options on the fly by passing an options array as second argument to the `slugify()`
method. For example:

```php
$slugify = new Slugify();
$slugify->slugify('Hello World', ['lowercase' => false]); // -> "Hello-World"
```

You can also modify the separator this way:

```php
$slugify = new Slugify();
$slugify->slugify('Hello World', ['separator' => '_']); // -> "hello_world"
```

You can even activate a custom ruleset without touching the default rules:

```php
$slugify = new Slugify();
$slugify->slugify('für', ['ruleset' => 'turkish']); // -> "fur"
$slugify->slugify('für'); // -> "fuer"
```

### Contributing

Feel free to ask for new rules for languages that is not already here.
Expand Down
36 changes: 26 additions & 10 deletions src/Slugify.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,23 +86,39 @@ public function __construct(array $options = [], RuleProviderInterface $provider
/**
* Returns the slug-version of the string.
*
* @param string $string String to slugify
* @param string|null $separator Separator
* @param string $string String to slugify
* @param string|array|null $options Options
*
* @return string Slugified version of the string
*/
public function slugify($string, $separator = null)
public function slugify($string, $options = null)
{
$string = strtr($string, $this->rules);
if ($this->options['lowercase']) {
$string = mb_strtolower($string);
// BC: the second argument used to be the separator
if (is_string($options)) {
$separator = $options;
$options = [];
$options['separator'] = $separator;
}

$options = array_merge($this->options, (array) $options);

// Add a custom ruleset without touching the default rules
if (isset($options['ruleset'])) {
$rules = array_merge($this->rules, $this->provider->getRules($options['ruleset']));
} else {
$rules = $this->rules;
}
if ($separator === null) {
$separator = $this->options['separator'];

$string = strtr($string, $rules);
unset($rules);

if ($options['lowercase']) {
$string = mb_strtolower($string);
}
$string = preg_replace($this->options['regexp'], $separator, $string);

return trim($string, $separator);
$string = preg_replace($options['regexp'], $options['separator'], $string);

return trim($string, $options['separator']);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/SlugifyInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@ interface SlugifyInterface
/**
* Return a URL safe version of a string.
*
* @param string $string
* @param string|null $separator
* @param string $string
* @param string|array|null $options
*
* @return string
*
* @api
*/
public function slugify($string, $separator = null);
public function slugify($string, $options = null);
}
28 changes: 28 additions & 0 deletions tests/SlugifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,34 @@ public function slugifyHonorsSeparatorArgument()
$this->assertEquals($expected, $this->slugify->slugify($actual, '__'));
}

/**
* @test
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyOptionsArray()
{
$this->assertEquals('file-name', $this->slugify->slugify('file name'));
$this->assertEquals('file+name', $this->slugify->slugify('file name', ['separator' => '+']));

$this->assertEquals('name-1', $this->slugify->slugify('name(1)'));
$this->assertEquals('name(1)', $this->slugify->slugify('name(1)', ['regexp' => '/([^a-z0-9.()]|-)+/']));

$this->assertEquals('file-name', $this->slugify->slugify('FILE NAME'));
$this->assertEquals('FILE-NAME', $this->slugify->slugify('FILE NAME', ['lowercase' => false]));
}

/**
* @test
* @covers Cocur\Slugify\Slugify::slugify()
*/
public function slugifyCustomRuleSet()
{
$slugify = new Slugify();

$this->assertSame('fur', $slugify->slugify('für', ['ruleset' => 'turkish']));
$this->assertSame('fuer', $slugify->slugify('für'));
}

public function defaultRuleProvider()
{
return [
Expand Down

0 comments on commit 11c0566

Please sign in to comment.