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

Add trimDelimiter option #36

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm

## [Unreleased]

* Add `trimDelimiter` option. [#36]

## [1.1.1] (2020-10-19)

Expand Down Expand Up @@ -51,6 +52,7 @@ This project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.htm
[1.0.0-alpha2]: https://github.com/ausi/slug-generator/compare/v1.0.0-alpha1...v1.0.0-alpha2
[1.0.0-alpha1]: https://github.com/ausi/slug-generator/commits/v1.0.0-alpha1

[#36]: https://github.com/ausi/slug-generator/issues/36
[#23]: https://github.com/ausi/slug-generator/issues/23
[#13]: https://github.com/ausi/slug-generator/issues/13
[#11]: https://github.com/ausi/slug-generator/issues/11
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ $generator->generate('Hello World!', ['delimiter' => '_']); // Result: hello_w
$generator->generate('Hello World!', ['delimiter' => '%20']); // Result: hello%20world
```

### `trimDelimiter`, default `true`

If set to `true` the delimiter will be stripped
from the beginning and the end of the slug.

```php
$generator->generate('Hello World!'); // Result: hello-world
$generator->generate('Hello World!', ['trimDelimiter' => false]); // Result: hello-world-
```

### `validChars`, default `"a-z0-9"`

Valid characters that are allowed in the slug.
Expand Down
15 changes: 12 additions & 3 deletions src/SlugGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public function generate(string $text, iterable $options = []): string
$options = $this->options->merge($options);

if ($options->getValidChars() === '') {
return '';
return $options->getTrimDelimiter() || $text === '' ? '' : $options->getDelimiter();
}

/** @var string $text */
Expand All @@ -83,7 +83,12 @@ public function generate(string $text, iterable $options = []): string
$text = $this->transform($text, $options->getValidChars(), $options->getTransforms(), $options->getLocale());
$text = $this->removeIgnored($text, $options->getIgnoreChars());

return $this->replaceWithDelimiter($text, $options->getValidChars(), $options->getDelimiter());
return $this->replaceWithDelimiter(
$text,
$options->getValidChars(),
$options->getDelimiter(),
$options->getTrimDelimiter()
);
}

/**
Expand All @@ -108,7 +113,7 @@ private function removeIgnored(string $text, string $ignore): string
* Replace all invalid characters with a delimiter
* and strip the delimiter from the beginning and the end.
*/
private function replaceWithDelimiter(string $text, string $valid, string $delimiter): string
private function replaceWithDelimiter(string $text, string $valid, string $delimiter, bool $trimDelimiter): string
{
$quoted = preg_quote($delimiter);

Expand All @@ -123,6 +128,10 @@ private function replaceWithDelimiter(string $text, string $valid, string $delim
throw new \RuntimeException(sprintf('Failed to replace "%s" with "%s" in "%s".', '(?:[^'.$valid.']|'.$quoted.')+', $delimiter, $text));
}

if (!$trimDelimiter) {
return $replaced;
}

// Remove delimiters from the beginning and the end
$removed = preg_replace('(^(?:'.$quoted.')+|(?:'.$quoted.')+$)us', '', $replaced);

Expand Down
24 changes: 24 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ class SlugOptions implements \IteratorAggregate
*/
private $delimiter = '-';

/**
* @var bool
*/
private $trimDelimiter = true;

/**
* @var string
*/
Expand Down Expand Up @@ -126,6 +131,24 @@ public function getDelimiter(): string
return $this->delimiter;
}

/**
* @param bool $trimDelimiter True if the delimiter should be trimmed from the beginning and end of the slug
*
* @return static
*/
public function setTrimDelimiter(bool $trimDelimiter): self
{
$this->trimDelimiter = $trimDelimiter;
$this->setOptions['trimDelimiter'] = null;

return $this;
}

public function getTrimDelimiter(): bool
{
return $this->trimDelimiter;
}

/**
* @param string $chars Character range for allowed characters
* in the form of a regular expression character set,
Expand Down Expand Up @@ -294,6 +317,7 @@ private function assertOptionName(string $option): void
{
static $validOptions = [
'delimiter',
'trimDelimiter',
'validChars',
'ignoreChars',
'locale',
Expand Down
15 changes: 15 additions & 0 deletions tests/SlugGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,21 @@ public function getGenerate(): array
'',
['validChars' => ''],
],
[
'-A B C-',
'-',
['validChars' => '-', 'trimDelimiter' => false],
],
[
'-A B C-',
'-a-b-c-',
['validChars' => 'a-z', 'trimDelimiter' => false],
],
[
'- -A B C- -',
'...a...b...c...',
['validChars' => 'a-z', 'delimiter' => '...', 'trimDelimiter' => false],
],
[
'contextöcontextöcontext',
'CONTEXTöCONTEXTöCONTEXT',
Expand Down
14 changes: 13 additions & 1 deletion tests/SlugOptionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,13 @@ public function testMerge(): void
$this->assertSame('x', $options->getDelimiter());
$this->assertSame('a-z0-9', $options->getValidChars());

$options2 = $options->merge(new SlugOptions(['validChars' => 'x']));
$options2 = $options->merge(new SlugOptions(['validChars' => 'x', 'trimDelimiter' => false]));
$this->assertSame('x', $options->getDelimiter());
$this->assertSame('a-z0-9', $options->getValidChars());
$this->assertTrue($options->getTrimDelimiter());
$this->assertSame('x', $options2->getDelimiter());
$this->assertSame('x', $options2->getValidChars());
$this->assertFalse($options2->getTrimDelimiter());
}

public function testGetIterator(): void
Expand Down Expand Up @@ -69,6 +71,16 @@ public function testSetDelimiter(): void
$this->assertSame('xx', $options->setDelimiter('xx')->getDelimiter());
}

public function testSetTrimDelimiter(): void
{
$options = new SlugOptions;
$this->assertTrue($options->getTrimDelimiter());

$options = new SlugOptions(['trimDelimiter' => false]);
$this->assertFalse($options->getTrimDelimiter());
$this->assertTrue($options->setTrimDelimiter(true)->getTrimDelimiter());
}

public function testSetValidChars(): void
{
$options = new SlugOptions;
Expand Down