Skip to content

Commit

Permalink
Merge pull request #5 from phpmentors-jp/add-string-uniquename
Browse files Browse the repository at this point in the history
add String/UniqueName
  • Loading branch information
iteman committed Sep 30, 2015
2 parents 29aee76 + e312e49 commit 9ca6eef
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ foreach ($period as $one) {

- ZeroableInterface

## String Utility

- UniqueName

# Support

If you find a bug or have a question, or want to request a feature, create an issue or pull request for it on [Issues](https://github.com/phpmentors-jp/domain-commons/issues).
Expand Down
74 changes: 74 additions & 0 deletions src/String/UniqueName.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
/*
* Copyright (c) 2015 GOTO Hidenori <[email protected]>,
* All rights reserved.
*
* This file is part of Domain Commons.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/

namespace PHPMentors\DomainCommons\String;

/**
* @since Class available since Release 1.1.1
*/
class UniqueName
{
/**
* @var array
*/
private $source;
/**
* @var string
*/
private $numberingPattern;
/**
* @var string
*/
private $numberingPatternRexex;
private $numberingPatternExtractRegex;

public function __construct($source, $numberingPattern = ' (n)')
{
$this->source = $source;
$this->numberingPattern = $numberingPattern;
$this->numberingPatternRegex = sprintf('/(%s)$/',
str_replace('n', '\d+', preg_quote($this->numberingPattern)));
$this->numberingPatternExtractRegex = sprintf('/#BASE#(%s)$/',
str_replace('n', '(\d+)', preg_quote($this->numberingPattern)));
}

public function __invoke($name, $alreadyInSource = true)
{
$sameList = array_filter($this->source, function ($targetName) use ($name) {
return $targetName === $name;
});
if (count($sameList) <= ($alreadyInSource ? 1 : 0)) {
return $name;
}

$baseName = preg_replace($this->numberingPatternRegex, '', $name);

$matchedList = array_filter($this->source, function($targetName) use ($baseName) {
return strpos($targetName, $baseName) !== false;
});

$max = 0;
$regex = str_replace('#BASE#', $baseName, $this->numberingPatternExtractRegex);
foreach ($matchedList as $targetName) {
if (preg_match($regex, $targetName, $match)) {
$max = max($max, $match[2]);
}
}

return $baseName . $this->makeNumber($max + 1);
}

private function makeNumber($number)
{
return str_replace('n', $number, $this->numberingPattern);
}
}
51 changes: 51 additions & 0 deletions tests/String/UniqueNameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php
/*
* Copyright (c) 2015 GOTO Hidenori <[email protected]>,
* All rights reserved.
*
* This file is part of Domain Commons.
*
* This program and the accompanying materials are made available under
* the terms of the BSD 2-Clause License which accompanies this
* distribution, and is available at http://opensource.org/licenses/BSD-2-Clause
*/

namespace PHPMentors\DomainCommons\String;

/**
* @since Class available since Release 1.1.1
*/
class UniqueNameTest extends \PHPUnit_Framework_TestCase
{
public function test()
{
$list = [
'test1','test2','apple banana','test1 (1)','test2 (3)','php',''
];
$uniqueName = new UniqueName($list);
$result = $uniqueName('test1');
$this->assertThat($result, $this->equalTo('test1'));
$result = $uniqueName('test1', false);
$this->assertThat($result, $this->equalTo('test1 (2)'));
$result = $uniqueName('test2 (2)');
$this->assertThat($result, $this->equalTo('test2 (2)'));


$list = [
'test1','test1',
];
$uniqueName = new UniqueName($list);
$result = $uniqueName('test1');
$this->assertThat($result, $this->equalTo('test1 (1)'));
$result = $uniqueName('test1', false);
$this->assertThat($result, $this->equalTo('test1 (1)'));


$list = [
'test1','test1 (1)',
];
$uniqueName = new UniqueName($list);
$result = $uniqueName('test1', false);
$this->assertThat($result, $this->equalTo('test1 (2)'));
}
}

0 comments on commit 9ca6eef

Please sign in to comment.