|
Moodle
2.2.1
http://www.collinsharper.com
|
00001 <?php 00002 00033 class Zend_Gdata_App_Util 00034 { 00035 00043 public static function formatTimestamp($timestamp) 00044 { 00045 $rfc3339 = '/^(\d{4})\-?(\d{2})\-?(\d{2})((T|t)(\d{2})\:?(\d{2})' . 00046 '\:?(\d{2})(\.\d{1,})?((Z|z)|([\+\-])(\d{2})\:?(\d{2})))?$/'; 00047 00048 if (ctype_digit($timestamp)) { 00049 return gmdate('Y-m-d\TH:i:sP', $timestamp); 00050 } elseif (preg_match($rfc3339, $timestamp) > 0) { 00051 // timestamp is already properly formatted 00052 return $timestamp; 00053 } else { 00054 $ts = strtotime($timestamp); 00055 if ($ts === false) { 00056 require_once 'Zend/Gdata/App/InvalidArgumentException.php'; 00057 throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp."); 00058 } 00059 return date('Y-m-d\TH:i:s', $ts); 00060 } 00061 } 00062 00073 public static function findGreatestBoundedValue($maximumKey, $collection) 00074 { 00075 $found = false; 00076 $foundKey = $maximumKey; 00077 00078 // Sanity check: Make sure that the collection isn't empty 00079 if (sizeof($collection) == 0) { 00080 require_once 'Zend/Gdata/App/Exception.php'; 00081 throw new Zend_Gdata_App_Exception("Empty namespace collection encountered."); 00082 } 00083 00084 if ($maximumKey === null) { 00085 // If the key is null, then we return the maximum available 00086 $keys = array_keys($collection); 00087 sort($keys); 00088 $found = true; 00089 $foundKey = end($keys); 00090 } else { 00091 // Otherwise, we optimistically guess that the current version 00092 // will have a matching namespce. If that fails, we decrement the 00093 // version until we find a match. 00094 while (!$found && $foundKey >= 0) { 00095 if (array_key_exists($foundKey, $collection)) 00096 $found = true; 00097 else 00098 $foundKey--; 00099 } 00100 } 00101 00102 // Guard: A namespace wasn't found. Either none were registered, or 00103 // the current protcol version is lower than the maximum namespace. 00104 if (!$found) { 00105 require_once 'Zend/Gdata/App/Exception.php'; 00106 throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found."); 00107 } 00108 00109 return $foundKey; 00110 } 00111 00112 }