public static function inc($string, $style = 'default', $next = 0)
{
$styles = array('dash' => array('#-(\\d+)$#', '-%d'), 'default' => array(array('#\\((\\d+)\\)$#', '#\\(\\d+\\)$#'), array(' (%d)', '(%d)')));
$styleSpec = isset($styles[$style]) ? $styles[$style] : $styles['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)) {
$next = empty($next) ? $matches[1] + 1 : $next;
$string = preg_replace($rxReplace, sprintf($oldFormat, $next), $string);
} else {
$next = empty($next) ? 2 : $next;
$string .= sprintf($newFormat, $next);
}
return $string;
}
public function testInc() { isSame('title (2)', Str::inc('title', null, 0)); isSame('title(3)', Str::inc('title(2)', null, 0)); isSame('title-2', Str::inc('title', 'dash', 0)); isSame('title-3', Str::inc('title-2', 'dash', 0)); isSame('title (4)', Str::inc('title', null, 4)); isSame('title (2)', Str::inc('title', 'foo', 0)); }