|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
4
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
5
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
6
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
7
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
8
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
9
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
10
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
11
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
12
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
13
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
14
|
|
|
* |
|
15
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
16
|
|
|
* and is licensed under the MIT license. For more information, see |
|
17
|
|
|
* <http://www.doctrine-project.org>. |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
namespace Doctrine\ODM\MongoDB\Id; |
|
21
|
|
|
|
|
22
|
|
|
use Doctrine\ODM\MongoDB\DocumentManager; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* AlnumGenerator is responsible for generating cased alpha-numeric unique identifiers. |
|
26
|
|
|
* It extends IncrementGenerator in order to ensure uniqueness even with short strings. |
|
27
|
|
|
* |
|
28
|
|
|
* "Awkward safe mode" avoids combinations that results in 'dirty' words by removing |
|
29
|
|
|
* the vowels from chars index |
|
30
|
|
|
* |
|
31
|
|
|
* A minimum identifier length can be enforced by setting a numeric value to the "pad" option |
|
32
|
|
|
* (with only 6 chars you will have more than 56 billion unique id's, 15 billion in 'awkward safe mode') |
|
33
|
|
|
* |
|
34
|
|
|
* The character set used for ID generation can be explicitly set with the "chars" option (e.g. base36, etc.) |
|
35
|
|
|
* |
|
36
|
|
|
* @since 1.0 |
|
37
|
|
|
*/ |
|
38
|
|
|
class AlnumGenerator extends IncrementGenerator |
|
39
|
|
|
{ |
|
40
|
|
|
|
|
41
|
|
|
protected $pad = null; |
|
42
|
|
|
|
|
43
|
|
|
protected $awkwardSafeMode = false; |
|
44
|
|
|
|
|
45
|
|
|
protected $chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'; |
|
46
|
|
|
|
|
47
|
|
|
protected $awkwardSafeChars = '0123456789BCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz'; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Set padding on generated id |
|
51
|
|
|
* |
|
52
|
|
|
* @param int $pad |
|
53
|
|
|
*/ |
|
54
|
|
|
public function setPad($pad) |
|
55
|
|
|
{ |
|
56
|
|
|
$this->pad = intval($pad); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* Enable awkwardSafeMode character set |
|
61
|
|
|
* |
|
62
|
|
|
* @param bool $awkwardSafeMode |
|
63
|
|
|
*/ |
|
64
|
|
|
public function setAwkwardSafeMode($awkwardSafeMode = false) |
|
65
|
|
|
{ |
|
66
|
|
|
$this->awkwardSafeMode = $awkwardSafeMode; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the character set used for ID generation |
|
71
|
|
|
* |
|
72
|
|
|
* @param string $chars ID character set |
|
73
|
|
|
*/ |
|
74
|
1 |
|
public function setChars($chars) |
|
75
|
|
|
{ |
|
76
|
1 |
|
$this->chars = $chars; |
|
77
|
1 |
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** @inheritDoc */ |
|
80
|
1 |
|
public function generate(DocumentManager $dm, $document) |
|
81
|
|
|
{ |
|
82
|
1 |
|
$id = parent::generate($dm, $document); |
|
83
|
1 |
|
$index = $this->awkwardSafeMode ? $this->awkwardSafeChars : $this->chars; |
|
84
|
1 |
|
$base = strlen($index); |
|
85
|
|
|
|
|
86
|
1 |
|
$out = ''; |
|
87
|
|
|
do { |
|
88
|
1 |
|
$out = $index[bcmod($id, $base)] . $out; |
|
89
|
1 |
|
$id = bcdiv($id, $base); |
|
90
|
1 |
|
} while (bccomp($id, 0) == 1); |
|
91
|
|
|
|
|
92
|
1 |
|
if (is_numeric($this->pad)) { |
|
93
|
|
|
$out = str_pad($out, $this->pad, '0', STR_PAD_LEFT); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
1 |
|
return $out; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|