Skip to content

Commit

Permalink
Merge pull request #2 from Rumi-Aguirre/feat/allow-all-chars
Browse files Browse the repository at this point in the history
Feat/allow all chars
  • Loading branch information
Rumi-Aguirre authored Sep 26, 2023
2 parents 4855909 + be188c6 commit 3ee5f04
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Composer
/composer.lock
/vendor/
.idea

# PHP Unit
/.phpunit.result.cache
28 changes: 19 additions & 9 deletions src/SlugGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,21 @@ public function generate(string $text, iterable $options = []): string

/** @var string $text */
$text = \Normalizer::normalize($text, \Normalizer::FORM_C);
$text = $this->removeIgnored($text, $options->getIgnoreChars());
if (!$options->getAllowAllChars()) {
$text = $this->removeIgnored($text, $options->getIgnoreChars());
}
$text = $this->transform($text, $options->getValidChars(), $options->getTransforms(), $options->getLocale());
$text = $this->removeIgnored($text, $options->getIgnoreChars());
if (!$options->getAllowAllChars()) {
$text = $this->removeIgnored($text, $options->getIgnoreChars());
}

return $this->replaceWithDelimiter(
$text,
$options->getValidChars(),
$options->getDelimiter(),
$options->getKeepBeginningDelimiter(),
$options->getKeepEndDelimiter()
$options->getKeepEndDelimiter(),
$options->getAllowAllChars(),
);
}

Expand Down Expand Up @@ -119,17 +124,22 @@ private function replaceWithDelimiter(
string $valid,
string $delimiter,
bool $keepBeginningDelimiter,
bool $keepEndDelimiter
bool $keepEndDelimiter,
bool $allowAllChars,
): string
{
$quoted = preg_quote($delimiter);

// Replace all invalid characters with a single delimiter
$replaced = preg_replace(
'((?:[^'.$valid.']|'.$quoted.')+)us',
$delimiter,
$text
);
if ($allowAllChars) {
$replaced = str_replace(' ', $delimiter, $text);
} else {
$replaced = preg_replace(
'((?:[^' . $valid . ']|' . $quoted . ')+)us',
$delimiter,
$text
);
}

if ($replaced === null) {
throw new \RuntimeException(sprintf('Failed to replace "%s" with "%s" in "%s".', '(?:[^'.$valid.']|'.$quoted.')+', $delimiter, $text));
Expand Down
16 changes: 16 additions & 0 deletions src/SlugOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class SlugOptions implements \IteratorAggregate

private bool $keepBeginningDelimiter = false;
private bool $keepEndDelimiter = false;
private bool $allowAllChars = false;

/**
* @param iterable<string,mixed> $options See the setter methods for available options
Expand Down Expand Up @@ -316,6 +317,20 @@ public function getKeepEndDelimiter(): bool
return $this->keepEndDelimiter;
}

public function setAllowAllChars(bool $allowAllChars): self
{
$this->allowAllChars = $allowAllChars;
$this->setOptions['allowAllChars'] = null;

return $this;
}

public function getAllowAllChars(): bool
{
return $this->allowAllChars;
}


/**
* @throws \InvalidArgumentException If it’s an invalid option name
*/
Expand All @@ -331,6 +346,7 @@ private function assertOptionName(string $option): void
'postTransforms',
'keepBeginningDelimiter',
'keepEndDelimiter',
'allowAllChars',
];

if (!\in_array($option, $validOptions, true)) {
Expand Down

0 comments on commit 3ee5f04

Please sign in to comment.