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-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
20 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
21 | */
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Utility class for static functions needed by Zend_Gdata_App
|
---|
25 | *
|
---|
26 | * @category Zend
|
---|
27 | * @package Zend_Gdata
|
---|
28 | * @subpackage App
|
---|
29 | * @copyright Copyright (c) 2005-2008 Zend Technologies USA Inc. (http://www.zend.com)
|
---|
30 | * @license http://framework.zend.com/license/new-bsd New BSD License
|
---|
31 | */
|
---|
32 | class Zend_Gdata_App_Util
|
---|
33 | {
|
---|
34 |
|
---|
35 | /**
|
---|
36 | * Convert timestamp into RFC 3339 date string.
|
---|
37 | * 2005-04-19T15:30:00
|
---|
38 | *
|
---|
39 | * @param int $timestamp
|
---|
40 | * @throws Zend_Gdata_App_InvalidArgumentException
|
---|
41 | */
|
---|
42 | public static function formatTimestamp($timestamp)
|
---|
43 | {
|
---|
44 | $rfc3339 = '/^(\d{4})\-?(\d{2})\-?(\d{2})((T|t)(\d{2})\:?(\d{2})' .
|
---|
45 | '\:?(\d{2})(\.\d{1,})?((Z|z)|([\+\-])(\d{2})\:?(\d{2})))?$/';
|
---|
46 |
|
---|
47 | if (ctype_digit($timestamp)) {
|
---|
48 | return gmdate('Y-m-d\TH:i:sP', $timestamp);
|
---|
49 | } elseif (preg_match($rfc3339, $timestamp) > 0) {
|
---|
50 | // timestamp is already properly formatted
|
---|
51 | return $timestamp;
|
---|
52 | } else {
|
---|
53 | $ts = strtotime($timestamp);
|
---|
54 | if ($ts === false) {
|
---|
55 | require_once 'Zend/Gdata/App/InvalidArgumentException.php';
|
---|
56 | throw new Zend_Gdata_App_InvalidArgumentException("Invalid timestamp: $timestamp.");
|
---|
57 | }
|
---|
58 | return date('Y-m-d\TH:i:s', $ts);
|
---|
59 | }
|
---|
60 | }
|
---|
61 |
|
---|
62 | /** Find the greatest key that is less than or equal to a given upper
|
---|
63 | * bound, and return the value associated with that key.
|
---|
64 | *
|
---|
65 | * @param integer|null $maximumKey The upper bound for keys. If null, the
|
---|
66 | * maxiumum valued key will be found.
|
---|
67 | * @param array $collection An two-dimensional array of key/value pairs
|
---|
68 | * to search through.
|
---|
69 | * @returns mixed The value corresponding to the located key.
|
---|
70 | * @throws Zend_Gdata_App_Exception Thrown if $collection is empty.
|
---|
71 | */
|
---|
72 | public static function findGreatestBoundedValue($maximumKey, $collection)
|
---|
73 | {
|
---|
74 | $found = false;
|
---|
75 | $foundKey = $maximumKey;
|
---|
76 |
|
---|
77 | // Sanity check: Make sure that the collection isn't empty
|
---|
78 | if (sizeof($collection) == 0) {
|
---|
79 | require_once 'Zend/Gdata/App/Exception.php';
|
---|
80 | throw new Zend_Gdata_App_Exception("Empty namespace collection encountered.");
|
---|
81 | }
|
---|
82 |
|
---|
83 | if ($maximumKey === null) {
|
---|
84 | // If the key is null, then we return the maximum available
|
---|
85 | $keys = array_keys($collection);
|
---|
86 | sort($keys);
|
---|
87 | $found = true;
|
---|
88 | $foundKey = end($keys);
|
---|
89 | } else {
|
---|
90 | // Otherwise, we optimistically guess that the current version
|
---|
91 | // will have a matching namespce. If that fails, we decrement the
|
---|
92 | // version until we find a match.
|
---|
93 | while (!$found && $foundKey >= 0) {
|
---|
94 | if (array_key_exists($foundKey, $collection))
|
---|
95 | $found = true;
|
---|
96 | else
|
---|
97 | $foundKey--;
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | // Guard: A namespace wasn't found. Either none were registered, or
|
---|
102 | // the current protcol version is lower than the maximum namespace.
|
---|
103 | if (!$found) {
|
---|
104 | require_once 'Zend/Gdata/App/Exception.php';
|
---|
105 | throw new Zend_Gdata_App_Exception("Namespace compatible with current protocol not found.");
|
---|
106 | }
|
---|
107 |
|
---|
108 | return $foundKey;
|
---|
109 | }
|
---|
110 |
|
---|
111 | }
|
---|