]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - Zend/Gdata/App/Util.php
Release 6.5.0
[Github/sugarcrm.git] / Zend / Gdata / App / Util.php
1 <?php
2
3 /**
4  * Zend Framework
5  *
6  * LICENSE
7  *
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.
15  *
16  * @category   Zend
17  * @package    Zend_Gdata
18  * @subpackage App
19  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
20  * @license    http://framework.zend.com/license/new-bsd     New BSD License
21
22  */
23
24 /**
25  * Utility class for static functions needed by Zend_Gdata_App
26  *
27  * @category   Zend
28  * @package    Zend_Gdata
29  * @subpackage App
30  * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
31  * @license    http://framework.zend.com/license/new-bsd     New BSD License
32  */
33 class Zend_Gdata_App_Util
34 {
35
36     /**
37      *  Convert timestamp into RFC 3339 date string.
38      *  2005-04-19T15:30:00
39      *
40      * @param int $timestamp
41      * @throws Zend_Gdata_App_InvalidArgumentException
42      */
43     public static function formatTimestamp($timestamp)
44     {
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})))?$/';
47
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
52             return $timestamp;
53         } else {
54             $ts = strtotime($timestamp);
55             if ($ts === false) {
56                 require_once 'Zend/Gdata/App/InvalidArgumentException.php';
57                 throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp.");
58             }
59             return date('Y-m-d\TH:i:s', $ts);
60         }
61     }
62
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.
65       *
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
69       *        to search through.
70       * @returns mixed The value corresponding to the located key.
71       * @throws Zend_Gdata_App_Exception Thrown if $collection is empty.
72       */
73     public static function findGreatestBoundedValue($maximumKey, $collection)
74     {
75         $found = false;
76         $foundKey = $maximumKey;
77
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.");
82         }
83
84         if ($maximumKey === null) {
85             // If the key is null, then we return the maximum available
86             $keys = array_keys($collection);
87             sort($keys);
88             $found = true;
89             $foundKey = end($keys);
90         } else {
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))
96                     $found = true;
97                 else
98                     $foundKey--;
99             }
100         }
101
102         // Guard: A namespace wasn't found. Either none were registered, or
103         // the current protcol version is lower than the maximum namespace.
104         if (!$found) {
105             require_once 'Zend/Gdata/App/Exception.php';
106             throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found.");
107         }
108
109         return $foundKey;
110     }
111
112 }