PageRenderTime 37ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/application/libraries/Zend/Gdata/App/Util.php

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