/library/Zend/Gdata/App/Util.php

https://github.com/coder-int21h/noses · PHP · 110 lines · 48 code · 10 blank · 52 comment · 11 complexity · e39c0117ed2f6f6d9a618f9bb7d15d34 MD5 · raw file

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