Skip to content

Refactor alias and title increment logic in StringHelper #53

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

Open
wants to merge 2 commits into
base: 3.x-dev
Choose a base branch
from
Open
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
41 changes: 19 additions & 22 deletions src/StringHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,29 +56,26 @@ public static function increment($string, $style = 'default', $n = 0)
{
$styleSpec = static::$incrementStyles[$style] ?? static::$incrementStyles['default'];

// Regular expression search and replace patterns.
if (\is_array($styleSpec[0])) {
$rxSearch = $styleSpec[0][0];
$rxReplace = $styleSpec[0][1];
} else {
$rxSearch = $rxReplace = $styleSpec[0];
}

// New and old (existing) sprintf formats.
if (\is_array($styleSpec[1])) {
$newFormat = $styleSpec[1][0];
$oldFormat = $styleSpec[1][1];
} else {
$newFormat = $oldFormat = $styleSpec[1];
}

// Check if we are incrementing an existing pattern, or appending a new one.
if (preg_match($rxSearch, $string, $matches)) {
$n = empty($n) ? ($matches[1] + 1) : $n;
$string = preg_replace($rxReplace, sprintf($oldFormat, $n), $string);
// Regular expression search and replace patterns for both cases
if ($style === 'dash') {
$rxSearch = '#-(\d+)$#'; // Match the trailing number after a hyphen (e.g., "dog-2")
$rxReplace = '-%d'; // Replace it with "-number"

if (preg_match($rxSearch, $string, $matches)) {
$n = empty($n) ? ($matches[1] + 1) : $n;
$string = preg_replace($rxReplace, sprintf($rxReplace, $n), $string);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll get an error here, you are replacing the n (value number) in the replacement string "-%d" which is not a valid replacement string for the preg_replace function, because it doesn't have the required delimiter -.

Your code will work, and replace report-2025 with report-2025-2, how it works?
Because, when it gets the error I mentioned, it will result in an empty string
Then when the empty alias reaches Content.php it will enter here
if (trim($this->alias) == '') { $this->alias = $this->title; }
Now the alias is typical as the title, lets say the title is report 2025 (2)

This following method will make behave the alias to be a safe URL, i.e. it will remove spaces and brackets and replace them with hyphen so report 2025 (2) will be report-2025-2
$this->alias = ApplicationHelper::stringURLSafe($this->alias, $this->language);

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing that out! Looking for some better solution. Will revert back soon!

} else {
$n = empty($n) ? 2 : $n;
$string .= sprintf($rxReplace, $n);
}
} else {
$n = empty($n) ? 2 : $n;
$string .= sprintf($newFormat, $n);
if (preg_match('#\((\d+)\)$#', $string, $matches)) {
$n = empty($n) ? ($matches[1] + 1) : $n;
$string = preg_replace('#\((\d+)\)$#', "($n)", $string);
} else {
$n = empty($n) ? 2 : $n;
$string .= " ($n)";
}
}

return $string;
Expand Down