2
0

Util.php 3.9 KB

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