Skip to content

Commit

Permalink
Add lowercase_after_regexp option (#216)
Browse files Browse the repository at this point in the history
  • Loading branch information
julienfalque authored and florianeckerstorfer committed Oct 25, 2018
1 parent edb835c commit af3c81f
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 1 deletion.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,17 @@ $slugify = new Slugify(['lowercase' => false]);
$slugify->slugify('Hello World'); // -> "Hello-World"
```

Lowercasing is done before using the regular expression. If you want to keep the lowercasing behavior but your regular
expression needs to match uppercase letters, you can set the `lowercase_after_regexp` option to `true`.

```php
$slugify = new Slugify([
'regexp' => '/(?<=[[:^upper:]])(?=[[:upper:]])/',
'lowercase_after_regexp' => false,
]);
$slugify->slugify('FooBar'); // -> "foo-bar"
```

By default Slugify will use dashes as separators. If you want to use a different default separator, you can set the
`separator` option.

Expand Down
1 change: 1 addition & 0 deletions src/Bridge/Symfony/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function getConfigTreeBuilder()
$rootNode
->children()
->booleanNode('lowercase')->end()
->booleanNode('lowercase_after_regexp')->end()
->booleanNode('trim')->end()
->booleanNode('strip_tags')->end()
->scalarNode('separator')->end()
Expand Down
7 changes: 6 additions & 1 deletion src/Slugify.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class Slugify implements SlugifyInterface
'regexp' => self::LOWERCASE_NUMBERS_DASHES,
'separator' => '-',
'lowercase' => true,
'lowercase_after_regexp' => false,
'trim' => true,
'strip_tags' => false,
'rulesets' => [
Expand Down Expand Up @@ -119,12 +120,16 @@ public function slugify($string, $options = null)
$string = strtr($string, $rules);
unset($rules);

if ($options['lowercase']) {
if ($options['lowercase'] && !$options['lowercase_after_regexp']) {
$string = mb_strtolower($string);
}

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

if ($options['lowercase'] && $options['lowercase_after_regexp']) {
$string = mb_strtolower($string);
}

return ($options['trim'])
? trim($string, $options['separator'])
: $string;
Expand Down
10 changes: 10 additions & 0 deletions tests/Bridge/Symfony/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testAll()
$configs = [
[
'lowercase' => true,
'lowercase_after_regexp' => false,
'strip_tags' => false,
'separator' => '_',
'regexp' => 'abcd',
Expand All @@ -41,6 +42,15 @@ public function testLowercaseOnlyAcceptsBoolean()
$this->process($configs);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
public function testLowercaseAfterRegexpOnlyAcceptsBoolean()
{
$configs = [['lowercase_after_regexp' => 'abc']];
$this->process($configs);
}

/**
* @expectedException \Symfony\Component\Config\Definition\Exception\InvalidTypeException
*/
Expand Down
40 changes: 40 additions & 0 deletions tests/SlugifyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,44 @@ public function defaultRuleProvider()
['Ą Č Ę Ė Į Š Ų Ū Ž ą č ę ė į š ų ū ž', 'a-c-e-e-i-s-u-u-z-a-c-e-e-i-s-u-u-z'],
];
}

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

// Matches any non-uppercase letter followed by an uppercase letter,
// which means it must be used before lowercasing the result.
$regexp = '/(?<=[[:^upper:]])(?=[[:upper:]])/';

$this->assertSame('foobar', $slugify->slugify('FooBar', [
'regexp' => $regexp,
'lowercase' => true,
'lowercase_after_regexp' => false,
'separator' => '_',
]));
}

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

// Matches any non-uppercase letter followed by an uppercase letter,
// which means it must be used before lowercasing the result.
$regexp = '/(?<=[[:^upper:]])(?=[[:upper:]])/';

$this->assertSame('foo_bar', $slugify->slugify('FooBar', [
'regexp' => $regexp,
'lowercase' => true,
'lowercase_after_regexp' => true,
'separator' => '_',
]));
}
}

0 comments on commit af3c81f

Please sign in to comment.