File indexing completed on 2025-03-02 05:29:26

0001 <?php
0002 
0003 /**
0004  * Zend Framework
0005  *
0006  * LICENSE
0007  *
0008  * This source file is subject to the new BSD license that is bundled
0009  * with this package in the file LICENSE.txt.
0010  * It is also available through the world-wide-web at this URL:
0011  * http://framework.zend.com/license/new-bsd
0012  * If you did not receive a copy of the license and are unable to
0013  * obtain it through the world-wide-web, please send an email
0014  * to license@zend.com so we can send you a copy immediately.
0015  *
0016  * @category   Zend
0017  * @package    Zend_Gdata
0018  * @subpackage App
0019  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0020  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0021  * @version    $Id$
0022  */
0023 
0024 /**
0025  * Utility class for static functions needed by Zend_Gdata_App
0026  *
0027  * @category   Zend
0028  * @package    Zend_Gdata
0029  * @subpackage App
0030  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0031  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0032  */
0033 class Zend_Gdata_App_Util
0034 {
0035 
0036     /**
0037      *  Convert timestamp into RFC 3339 date string.
0038      *  2005-04-19T15:30:00
0039      *
0040      * @param int $timestamp
0041      * @throws Zend_Gdata_App_InvalidArgumentException
0042      */
0043     public static function formatTimestamp($timestamp)
0044     {
0045         $rfc3339 = '/^(\d{4})\-?(\d{2})\-?(\d{2})((T|t)(\d{2})\:?(\d{2})' .
0046                    '\:?(\d{2})(\.\d{1,})?((Z|z)|([\+\-])(\d{2})\:?(\d{2})))?$/';
0047 
0048         if (ctype_digit((string)$timestamp)) {
0049             return gmdate('Y-m-d\TH:i:sP', $timestamp);
0050         } elseif (preg_match($rfc3339, $timestamp) > 0) {
0051             // timestamp is already properly formatted
0052             return $timestamp;
0053         } else {
0054             $ts = strtotime($timestamp);
0055             if ($ts === false) {
0056                 // require_once 'Zend/Gdata/App/InvalidArgumentException.php';
0057                 throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp.");
0058             }
0059             return date('Y-m-d\TH:i:s', $ts);
0060         }
0061     }
0062 
0063     /** Find the greatest key that is less than or equal to a given upper
0064       * bound, and return the value associated with that key.
0065       *
0066       * @param integer|null $maximumKey The upper bound for keys. If null, the
0067       *        maxiumum valued key will be found.
0068       * @param array $collection An two-dimensional array of key/value pairs
0069       *        to search through.
0070       * @returns mixed The value corresponding to the located key.
0071       * @throws Zend_Gdata_App_Exception Thrown if $collection is empty.
0072       */
0073     public static function findGreatestBoundedValue($maximumKey, $collection)
0074     {
0075         $found = false;
0076         $foundKey = $maximumKey;
0077 
0078         // Sanity check: Make sure that the collection isn't empty
0079         if (sizeof($collection) == 0) {
0080             // require_once 'Zend/Gdata/App/Exception.php';
0081             throw new Zend_Gdata_App_Exception("Empty namespace collection encountered.");
0082         }
0083 
0084         if ($maximumKey === null) {
0085             // If the key is null, then we return the maximum available
0086             $keys = array_keys($collection);
0087             sort($keys);
0088             $found = true;
0089             $foundKey = end($keys);
0090         } else {
0091             // Otherwise, we optimistically guess that the current version
0092             // will have a matching namespce. If that fails, we decrement the
0093             // version until we find a match.
0094             while (!$found && $foundKey >= 0) {
0095                 if (array_key_exists($foundKey, $collection))
0096                     $found = true;
0097                 else
0098                     $foundKey--;
0099             }
0100         }
0101 
0102         // Guard: A namespace wasn't found. Either none were registered, or
0103         // the current protcol version is lower than the maximum namespace.
0104         if (!$found) {
0105             // require_once 'Zend/Gdata/App/Exception.php';
0106             throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found.");
0107         }
0108 
0109         return $foundKey;
0110     }
0111 
0112 }