Skip to content

Commit

Permalink
Merge pull request doctrine#96 from feychenie/master
Browse files Browse the repository at this point in the history
Added new alpha-numeric ID generator
  • Loading branch information
jwage committed Jul 27, 2011
2 parents bef17d8 + 20a7306 commit f0cfc81
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 2 deletions.
78 changes: 78 additions & 0 deletions lib/Doctrine/ODM/MongoDB/Id/AlnumGenerator.php
@@ -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;
}
}
11 changes: 10 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataFactory.php
Expand Up @@ -343,7 +343,6 @@ protected function getParentClasses($name)

private function completeIdGeneratorMapping(ClassMetadata $class)
{
$idGenType = $class->generatorType;
$idGenOptions = $class->generatorOptions;
switch ($class->generatorType) {
case ClassMetadata::GENERATOR_TYPE_AUTO:
Expand All @@ -357,6 +356,16 @@ private function completeIdGeneratorMapping(ClassMetadata $class)
$uuidGenerator->setSalt(isset($idGenOptions['salt']) ? $idGenOptions['salt'] : php_uname('n'));
$class->setIdGenerator($uuidGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_ALNUM:
$alnumGenerator = new \Doctrine\ODM\MongoDB\Id\AlnumGenerator($class);
if(isset($idGenOptions['pad'])) {
$alnumGenerator->setPad($idGenOptions['pad']);
}
if(isset($idGenOptions['awkwardSafe'])) {
$alnumGenerator->setAwkwardSafeMode($idGenOptions['awkwardSafe']);
}
$class->setIdGenerator($alnumGenerator);
break;
case ClassMetadata::GENERATOR_TYPE_NONE;
break;
default:
Expand Down
9 changes: 8 additions & 1 deletion lib/Doctrine/ODM/MongoDB/Mapping/ClassMetadataInfo.php
Expand Up @@ -62,11 +62,18 @@ class ClassMetadataInfo implements \Doctrine\Common\Persistence\Mapping\ClassMet
*/
const GENERATOR_TYPE_UUID = 3;

/**
* ALNUM means Doctrine will generate Alpha-numeric string identifiers, using the INCREMENT
* generator to ensure identifier uniqueness
*/
const GENERATOR_TYPE_ALNUM = 4;

/**
* NONE means Doctrine will not generate any id for us and you are responsible for manually
* assigning an id.
*/
const GENERATOR_TYPE_NONE = 4;
const GENERATOR_TYPE_NONE = 5;


const REFERENCE_ONE = 1;
const REFERENCE_MANY = 2;
Expand Down

0 comments on commit f0cfc81

Please sign in to comment.