8 * This source file is subject to the new BSD license that is bundled
9 * with this package in the file LICENSE.txt.
10 * It is also available through the world-wide-web at this URL:
11 * http://framework.zend.com/license/new-bsd
12 * If you did not receive a copy of the license and are unable to
13 * obtain it through the world-wide-web, please send an email
14 * to license@zend.com so we can send you a copy immediately.
19 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
20 * @license http://framework.zend.com/license/new-bsd New BSD License
21 * @version $Id: Util.php 16971 2009-07-22 18:05:45Z mikaelkael $
25 * Utility class for static functions needed by Zend_Gdata_App
30 * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
31 * @license http://framework.zend.com/license/new-bsd New BSD License
33 class Zend_Gdata_App_Util
37 * Convert timestamp into RFC 3339 date string.
40 * @param int $timestamp
41 * @throws Zend_Gdata_App_InvalidArgumentException
43 public static function formatTimestamp($timestamp)
45 $rfc3339 = '/^(\d{4})\-?(\d{2})\-?(\d{2})((T|t)(\d{2})\:?(\d{2})' .
46 '\:?(\d{2})(\.\d{1,})?((Z|z)|([\+\-])(\d{2})\:?(\d{2})))?$/';
48 if (ctype_digit($timestamp)) {
49 return gmdate('Y-m-d\TH:i:sP', $timestamp);
50 } elseif (preg_match($rfc3339, $timestamp) > 0) {
51 // timestamp is already properly formatted
54 $ts = strtotime($timestamp);
56 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
57 throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp.");
59 return date('Y-m-d\TH:i:s', $ts);
63 /** Find the greatest key that is less than or equal to a given upper
64 * bound, and return the value associated with that key.
66 * @param integer|null $maximumKey The upper bound for keys. If null, the
67 * maxiumum valued key will be found.
68 * @param array $collection An two-dimensional array of key/value pairs
70 * @returns mixed The value corresponding to the located key.
71 * @throws Zend_Gdata_App_Exception Thrown if $collection is empty.
73 public static function findGreatestBoundedValue($maximumKey, $collection)
76 $foundKey = $maximumKey;
78 // Sanity check: Make sure that the collection isn't empty
79 if (sizeof($collection) == 0) {
80 require_once 'Zend/Gdata/App/Exception.php';
81 throw new Zend_Gdata_App_Exception("Empty namespace collection encountered.");
84 if ($maximumKey === null) {
85 // If the key is null, then we return the maximum available
86 $keys = array_keys($collection);
89 $foundKey = end($keys);
91 // Otherwise, we optimistically guess that the current version
92 // will have a matching namespce. If that fails, we decrement the
93 // version until we find a match.
94 while (!$found && $foundKey >= 0) {
95 if (array_key_exists($foundKey, $collection))
102 // Guard: A namespace wasn't found. Either none were registered, or
103 // the current protcol version is lower than the maximum namespace.
105 require_once 'Zend/Gdata/App/Exception.php';
106 throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found.");