forked from doctrine/mongodb-odm
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request doctrine#96 from feychenie/master
Added new alpha-numeric ID generator
- Loading branch information
Showing
3 changed files
with
96 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php | ||
/* | ||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
* | ||
* This software consists of voluntary contributions made by many individuals | ||
* and is licensed under the LGPL. For more information, see | ||
* <http://www.doctrine-project.org>. | ||
*/ | ||
|
||
namespace Doctrine\ODM\MongoDB\Id; | ||
|
||
use Doctrine\ODM\MongoDB\DocumentManager; | ||
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata; | ||
|
||
/** | ||
* AlnumGenerator is responsible for generating cased alpha-numeric unique identifiers. | ||
* It extends IncrementGenerator in order to ensure uniqueness even with short strings. | ||
* | ||
* "Awkward safe mode" avoids combinations that results in 'dirty' words by removing | ||
* the vouwels from chars index | ||
* | ||
* A minimum identifier length can be enforced by setting a numeric value to the "pad" option | ||
* (with only 6 chars you will have more than 56 billion unique id's, 15 billion in 'awkward safe mode') | ||
* | ||
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL | ||
* @link www.doctrine-project.com | ||
* @since 1.0 | ||
* @author Frederik Eychenié <feychenie@gmail.com> | ||
*/ | ||
class AlnumGenerator extends IncrementGenerator | ||
{ | ||
|
||
protected $pad = null; | ||
|
||
protected $awkwardSafeMode = false; | ||
|
||
protected $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; | ||
|
||
protected $awkwardSafeChars = '0123456789BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz'; | ||
|
||
public function setPad($pad){ | ||
$this->pad = intval($pad); | ||
} | ||
|
||
public function setAwkwardSafeMode($awkwardSafeMode = false) | ||
{ | ||
$this->awkwardSafeMode = $awkwardSafeMode; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function generate(DocumentManager $dm, $document) | ||
{ | ||
$id = parent::generate($dm, $document); | ||
$index = $this->awkwardSafeMode ? $this->awkwardSafeChars : $this->chars; | ||
$base = strlen($index); | ||
|
||
$out = ""; | ||
for ( $t = floor( log10( $id ) / log10( $base ) ); $t >= 0; $t-- ) { | ||
$a = floor( $id / pow( $base, $t ) ); | ||
$out = $out . substr( $index, $a, 1 ); | ||
$id = $id - ( $a * pow( $base, $t ) ); | ||
} | ||
if(is_numeric($this->pad)) { | ||
$out = str_pad( $out, $this->pad, "0", STR_PAD_LEFT); | ||
} | ||
return $out; | ||
} | ||
} |
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
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