--- /dev/null
+<?php
+/**
+ * @category Horde
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * @package Support
+ */
+
+/**
+ * Class for generating a random ID string. This string uses all characters
+ * in the class [0-9a-z].
+ *
+ * <code>
+ * <?php
+ *
+ * $rid = (string)new Horde_Support_Randomid([$length = 16]);
+ *
+ * ?>
+ * </code>
+ *
+ * @author Michael Slusarz <slusarz@horde.org>
+ * @category Horde
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php BSD
+ * @package Support
+ */
+class Horde_Support_Randomid
+{
+ /**
+ * Generated ID.
+ *
+ * @var string
+ */
+ private $_rid;
+
+ /**
+ * New random ID.
+ *
+ * @param integer $length The length of the ID.
+ */
+ public function __construct($length = 16)
+ {
+ $this->generate($length);
+ }
+
+ /**
+ * Generate a random ID.
+ *
+ * @param integer $length The length of the ID.
+ */
+ public function generate($length = 16)
+ {
+ $this->_rid = substr(base_convert(dechex(strtr(microtime(), array('0.' => '', ' ' => ''))) . strtr(uniqid(mt_rand(), true), array('.' => '')), 16, 36), 0, $length);
+ }
+
+ /**
+ * Cooerce to string.
+ *
+ * @return string The random ID.
+ */
+ public function __toString()
+ {
+ return $this->_rid;
+ }
+
+}
<api>beta</api>
</stability>
<license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
- <notes>
-* Add Portuguese numerizer.
+ <notes> * Add Horde_Support_Randomid::.
+ * Add Portuguese numerizer.
</notes>
<contents>
<dir baseinstalldir="/" name="/">
<file name="Guid.php" role="php" />
<file name="Inflector.php" role="php" />
<file name="Numerizer.php" role="php" />
+ <file name="Randomid.php" role="php" />
<file name="Stack.php" role="php" />
<file name="StringStream.php" role="php" />
<file name="Stub.php" role="php" />
<file name="CombineStreamTest.php" role="test" />
<file name="ConsistentHashTest.php" role="test" />
<file name="InflectorTest.php" role="test" />
+ <file name="RandomidTest.php" role="test" />
<file name="StringStreamTest.php" role="test" />
<file name="StubTest.php" role="test" />
<file name="TimerTest.php" role="test" />
<install as="Horde/Support/Guid.php" name="lib/Horde/Support/Guid.php" />
<install as="Horde/Support/Inflector.php" name="lib/Horde/Support/Inflector.php" />
<install as="Horde/Support/Numerizer.php" name="lib/Horde/Support/Numerizer.php" />
+ <install as="Horde/Support/Randomid.php" name="lib/Horde/Support/Randomid.php" />
<install as="Horde/Support/Stack.php" name="lib/Horde/Support/Stack.php" />
<install as="Horde/Support/StringStream.php" name="lib/Horde/Support/StringStream.php" />
<install as="Horde/Support/Stub.php" name="lib/Horde/Support/Stub.php" />
<install as="Horde/Support/CombineStreamTest.php" name="test/Horde/Support/CombineStreamTest.php" />
<install as="Horde/Support/ConsistentHashTest.php" name="test/Horde/Support/ConsistentHashTest.php" />
<install as="Horde/Support/InflectorTest.php" name="test/Horde/Support/InflectorTest.php" />
+ <install as="Horde/Support/RandomidTest.php" name="test/Horde/Support/RandomidTest.php" />
<install as="Horde/Support/StringStreamTest.php" name="test/Horde/Support/StringStreamTest.php" />
<install as="Horde/Support/StubTest.php" name="test/Horde/Support/StubTest.php" />
<install as="Horde/Support/TimerTest.php" name="test/Horde/Support/TimerTest.php" />
* Initial Horde_Support_Numerizer objects
</notes>
</release>
- <release>
- <version>
- <release>0.2.0</release>
- <api>0.1.0</api>
- </version>
- <stability>
- <release>beta</release>
- <api>beta</api>
- </stability>
- <date>2010-07-10</date>
- <license uri="http://opensource.org/licenses/bsd-license.php">BSD</license>
- <notes>
-* Add Portuguese numerizer.
- </notes>
- </release>
</changelog>
</package>
--- /dev/null
+<?php
+/**
+ * @category Horde
+ * @package Support
+ * @subpackage UnitTests
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php
+ */
+
+/**
+ * @group support
+ * @category Horde
+ * @package Support
+ * @subpackage UnitTests
+ * @copyright 2010 The Horde Project (http://www.horde.org/)
+ * @license http://opensource.org/licenses/bsd-license.php
+ */
+class Horde_Support_RandomidTest extends PHPUnit_Framework_TestCase
+{
+ public function testRandomidLength()
+ {
+ $rid = (string)new Horde_Support_Randomid;
+ $this->assertEquals(16, strlen($rid));
+ }
+
+}