Add Horde_Support_Randomid::
authorMichael M Slusarz <slusarz@curecanti.org>
Tue, 3 Aug 2010 04:16:47 +0000 (22:16 -0600)
committerMichael M Slusarz <slusarz@curecanti.org>
Tue, 3 Aug 2010 04:18:54 +0000 (22:18 -0600)
framework/Support/lib/Horde/Support/Randomid.php [new file with mode: 0644]
framework/Support/package.xml
framework/Support/test/Horde/Support/RandomidTest.php [new file with mode: 0644]

diff --git a/framework/Support/lib/Horde/Support/Randomid.php b/framework/Support/lib/Horde/Support/Randomid.php
new file mode 100644 (file)
index 0000000..703c70f
--- /dev/null
@@ -0,0 +1,66 @@
+<?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;
+    }
+
+}
index 85fa66e..2e655ff 100644 (file)
@@ -21,8 +21,8 @@
   <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="/">
@@ -43,6 +43,7 @@
       <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" />
@@ -66,6 +67,7 @@
       <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>
diff --git a/framework/Support/test/Horde/Support/RandomidTest.php b/framework/Support/test/Horde/Support/RandomidTest.php
new file mode 100644 (file)
index 0000000..26be46a
--- /dev/null
@@ -0,0 +1,26 @@
+<?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));
+    }
+
+}