diff -r 5b37998e522e -r 162c1de6545a web/lib/Zend/Gdata/App/Util.php --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/web/lib/Zend/Gdata/App/Util.php Fri Mar 11 15:05:35 2011 +0100 @@ -0,0 +1,112 @@ + 0) { + // timestamp is already properly formatted + return $timestamp; + } else { + $ts = strtotime($timestamp); + if ($ts === false) { + require_once 'Zend/Gdata/App/InvalidArgumentException.php'; + throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp."); + } + return date('Y-m-d\TH:i:s', $ts); + } + } + + /** Find the greatest key that is less than or equal to a given upper + * bound, and return the value associated with that key. + * + * @param integer|null $maximumKey The upper bound for keys. If null, the + * maxiumum valued key will be found. + * @param array $collection An two-dimensional array of key/value pairs + * to search through. + * @returns mixed The value corresponding to the located key. + * @throws Zend_Gdata_App_Exception Thrown if $collection is empty. + */ + public static function findGreatestBoundedValue($maximumKey, $collection) + { + $found = false; + $foundKey = $maximumKey; + + // Sanity check: Make sure that the collection isn't empty + if (sizeof($collection) == 0) { + require_once 'Zend/Gdata/App/Exception.php'; + throw new Zend_Gdata_App_Exception("Empty namespace collection encountered."); + } + + if ($maximumKey === null) { + // If the key is null, then we return the maximum available + $keys = array_keys($collection); + sort($keys); + $found = true; + $foundKey = end($keys); + } else { + // Otherwise, we optimistically guess that the current version + // will have a matching namespce. If that fails, we decrement the + // version until we find a match. + while (!$found && $foundKey >= 0) { + if (array_key_exists($foundKey, $collection)) + $found = true; + else + $foundKey--; + } + } + + // Guard: A namespace wasn't found. Either none were registered, or + // the current protcol version is lower than the maximum namespace. + if (!$found) { + require_once 'Zend/Gdata/App/Exception.php'; + throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found."); + } + + return $foundKey; + } + +}