Add Horde_Support_Guid to generate the Horde-style GUIDs.
authorJan Schneider <jan@horde.org>
Tue, 8 Sep 2009 21:55:17 +0000 (23:55 +0200)
committerJan Schneider <jan@horde.org>
Tue, 8 Sep 2009 21:55:33 +0000 (23:55 +0200)
framework/Support/lib/Horde/Support/Guid.php [new file with mode: 0644]
framework/Support/package.xml

diff --git a/framework/Support/lib/Horde/Support/Guid.php b/framework/Support/lib/Horde/Support/Guid.php
new file mode 100644 (file)
index 0000000..6b44516
--- /dev/null
@@ -0,0 +1,65 @@
+<?php
+/**
+ * @category   Horde
+ * @package    Support
+ * @copyright  2009 The Horde Project (http://www.horde.org/)
+ * @license    http://opensource.org/licenses/bsd-license.php
+ */
+
+/**
+ * Class for generating GUIDs. Usage:
+ *
+ * <code>
+ *  <?php
+ *
+ *  $uid = (string)new Horde_Support_Guid;
+ *
+ *  ?>
+ * </code>
+ *
+ * @category   Horde
+ * @package    Support
+ * @copyright  2009 The Horde Project (http://www.horde.org/)
+ * @license    http://opensource.org/licenses/bsd-license.php
+ */
+class Horde_Support_Guid
+{
+    /**
+     * Generated GUID
+     * @var string
+     */
+    private $_guid;
+
+    /**
+     * New GUID
+     */
+    public function __construct()
+    {
+        $this->generate();
+    }
+
+    /**
+     * Generates a GUID.
+     *
+     * @return string
+     */
+    public function generate()
+    {
+        $this->_guid = date('YmdHis')
+            . '.'
+            . substr(str_pad(base_convert(microtime(), 10, 36), 16, uniqid(mt_rand()), STR_PAD_LEFT), -16)
+            . '@'
+            . (isset($_SERVER['SERVER_NAME']) ? $_SERVER['SERVER_NAME'] : 'localhost');
+    }
+
+    /**
+     * Cooerce to string
+     *
+     * @return string
+     */
+    public function __toString()
+    {
+        return $this->_guid;
+    }
+
+}
index 4b6704f..b6ba892 100644 (file)
@@ -50,6 +50,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
       <file name="Array.php" role="php" />
       <file name="Backtrace.php" role="php" />
       <file name="ConsistentHash.php" role="php" />
+      <file name="Guid.php" role="php" />
       <file name="Inflector.php" role="php" />
       <file name="Numerizer.php" role="php" />
       <file name="Stack.php" role="php" />
@@ -76,6 +77,7 @@ http://pear.php.net/dtd/package-2.0.xsd">
    <install name="lib/Horde/Support/Array.php" as="Horde/Support/Array.php" />
    <install name="lib/Horde/Support/Backtrace.php" as="Horde/Support/Backtrace.php" />
    <install name="lib/Horde/Support/ConsistentHash.php" as="Horde/Support/ConsistentHash.php" />
+   <install name="lib/Horde/Support/Guid.php" as="Horde/Support/Guid.php" />
    <install name="lib/Horde/Support/Inflector.php" as="Horde/Support/Inflector.php" />
    <install name="lib/Horde/Support/Numerizer/Locale/Base.php" as="Horde/Support/Numerizer/Locale/Base.php" />
    <install name="lib/Horde/Support/Numerizer/Locale/De.php" as="Horde/Support/Numerizer/Locale/De.php" />