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

Allow to modify options without creating a new object #133

Merged
merged 4 commits into from
Aug 16, 2016
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
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