JBZoo\Utils\Str::inc PHP 메소드

inc() 공개 정적인 메소드

Used to easily create distinct labels when copying objects. The method has the following styles: - default: "Label" becomes "Label (2)" - dash: "Label" becomes "Label-2"
public static inc ( string $string, string $style = 'default', integer $next ) : string
$string string The source string.
$style string The the style (default|dash).
$next integer If supplied, this number is used for the copy, otherwise it is the 'next' number.
리턴 string
    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;
    }

Usage Example

예제 #1
0
파일: StringTest.php 프로젝트: jbzoo/utils
 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));
 }