Last active
May 16, 2019 15:41
-
-
Save mariovalney/8eb6c6bde1c7241bb3ae3d5a43a7dacc to your computer and use it in GitHub Desktop.
Return a string replacing a placeholder by a sequential number.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Return a string replacing placeholder by a number | |
* | |
* You can create 'item-1', 'item-2', 'item-3'... globally | |
* just calling unique_string_id('item{id}') | |
* | |
* @param string $string | |
* @param bool $includeOne | |
* @param string $placeholder | |
* @param string $separator | |
* | |
* @return string | |
*/ | |
function unique_string_id($string, $includeOne = true, $placeholder = '{id}', $separator = '-') | |
{ | |
global $unique_string_id; | |
if (! is_array($unique_string_id)) { | |
$unique_string_id = []; | |
} | |
$identifier = str_replace($placeholder, $separator, $string); | |
if (empty($unique_string_id[ $identifier ])) { | |
$unique_string_id[ $identifier ] = 0; | |
} | |
$unique_string_id[ $identifier ]++; | |
// Create a impossible placeholder, so we can trim at the end | |
$impossibleSeparator = '__impossible_%_separator__'; | |
// Add unique ID | |
$replace = $impossibleSeparator; | |
if ($unique_string_id[ $identifier ] > 1 || ($unique_string_id[ $identifier ] === 1 && $includeOne)) { | |
$replace .= $unique_string_id[ $identifier ] . $impossibleSeparator; | |
} | |
$string = str_replace($placeholder, $replace, $string); | |
// Trimming | |
while (strpos($string, $impossibleSeparator) === 0) { | |
$string = substr($string, strlen($impossibleSeparator)); | |
} | |
while (strrpos($string, $impossibleSeparator) === (strlen($string) - strlen($impossibleSeparator))) { | |
$string = substr($string, 0, strlen($string) - strlen($impossibleSeparator)); | |
} | |
return str_replace($impossibleSeparator, $separator, $string); | |
} | |
/* | |
Test | |
public function testUniqueStringIdFunction() | |
{ | |
// Test common | |
$this->assertSame('item-1', unique_string_id('item{id}')); | |
$this->assertSame('item-2', unique_string_id('item{id}')); | |
$this->assertSame('item-3', unique_string_id('item{id}')); | |
// Test without first | |
$this->assertSame('no-first', unique_string_id('no-first{id}', false)); | |
$this->assertSame('no-first-2', unique_string_id('no-first{id}', false)); | |
$this->assertSame('no-first-3', unique_string_id('no-first{id}', false)); | |
// Test with another placeholder | |
$this->assertSame('placeholder-{id}-1', unique_string_id('placeholder-{id}{key}', true, '{key}')); | |
$this->assertSame('placeholder-{id}-2', unique_string_id('placeholder-{id}{key}', true, '{key}')); | |
$this->assertSame('placeholder-{id}-3', unique_string_id('placeholder-{id}{key}', true, '{key}')); | |
// Test with another separator | |
$this->assertSame('item_1', unique_string_id('item{id}', true, '{id}', '_')); | |
$this->assertSame('item_2', unique_string_id('item{id}', true, '{id}', '_')); | |
$this->assertSame('item_3', unique_string_id('item{id}', true, '{id}', '_')); | |
// Test not remove char equal to separator with ID on start | |
$this->assertSame('starting-id-', unique_string_id('{id}starting-id-', false)); | |
$this->assertSame('2-starting-id-', unique_string_id('{id}starting-id-', false)); | |
// Test not remove char equal to separator with ID on end | |
$this->assertSame('-ending-id', unique_string_id('-ending-id{id}', false)); | |
$this->assertSame('-ending-id-2', unique_string_id('-ending-id{id}', false)); | |
} | |
**/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment