Ampache PHP Cross Reference Groupware Applications

Source: /modules/Guzzle/Service/Description/SchemaFormatter.php - 156 lines - 4073 bytes - Summary - Text - Print

   1  <?php
   2  
   3  namespace Guzzle\Service\Description;
   4  
   5  use Guzzle\Common\Exception\InvalidArgumentException;
   6  
   7  /**
   8   * JSON Schema formatter class
   9   */
  10  class SchemaFormatter
  11  {
  12      /** @var \DateTimeZone */
  13      protected static $utcTimeZone;
  14  
  15      /**
  16       * Format a value by a registered format name
  17       *
  18       * @param string $format Registered format used to format the value
  19       * @param mixed  $value  Value being formatted
  20       *
  21       * @return mixed
  22       */
  23      public static function format($format, $value)
  24      {
  25          switch ($format) {
  26              case 'date-time':
  27                  return self::formatDateTime($value);
  28              case 'date-time-http':
  29                  return self::formatDateTimeHttp($value);
  30              case 'date':
  31                  return self::formatDate($value);
  32              case 'time':
  33                  return self::formatTime($value);
  34              case 'timestamp':
  35                  return self::formatTimestamp($value);
  36              case 'boolean-string':
  37                  return self::formatBooleanAsString($value);
  38              default:
  39                  return $value;
  40          }
  41      }
  42  
  43      /**
  44       * Create a ISO 8601 (YYYY-MM-DDThh:mm:ssZ) formatted date time value in UTC time
  45       *
  46       * @param string|integer|\DateTime $value Date time value
  47       *
  48       * @return string
  49       */
  50      public static function formatDateTime($value)
  51      {
  52          return self::dateFormatter($value, 'Y-m-d\TH:i:s\Z');
  53      }
  54  
  55      /**
  56       * Create an HTTP date (RFC 1123 / RFC 822) formatted UTC date-time string
  57       *
  58       * @param string|integer|\DateTime $value Date time value
  59       *
  60       * @return string
  61       */
  62      public static function formatDateTimeHttp($value)
  63      {
  64          return self::dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
  65      }
  66  
  67      /**
  68       * Create a YYYY-MM-DD formatted string
  69       *
  70       * @param string|integer|\DateTime $value Date time value
  71       *
  72       * @return string
  73       */
  74      public static function formatDate($value)
  75      {
  76          return self::dateFormatter($value, 'Y-m-d');
  77      }
  78  
  79      /**
  80       * Create a hh:mm:ss formatted string
  81       *
  82       * @param string|integer|\DateTime $value Date time value
  83       *
  84       * @return string
  85       */
  86      public static function formatTime($value)
  87      {
  88          return self::dateFormatter($value, 'H:i:s');
  89      }
  90  
  91      /**
  92       * Formats a boolean value as a string
  93       *
  94       * @param string|integer|bool $value Value to convert to a boolean 'true' / 'false' value
  95       *
  96       * @return string
  97       */
  98      public static function formatBooleanAsString($value)
  99      {
 100          return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
 101      }
 102  
 103      /**
 104       * Return a UNIX timestamp in the UTC timezone
 105       *
 106       * @param string|integer|\DateTime $value Time value
 107       *
 108       * @return int
 109       */
 110      public static function formatTimestamp($value)
 111      {
 112          return (int) self::dateFormatter($value, 'U');
 113      }
 114  
 115      /**
 116       * Get a UTC DateTimeZone object
 117       *
 118       * @return \DateTimeZone
 119       */
 120      protected static function getUtcTimeZone()
 121      {
 122          // @codeCoverageIgnoreStart
 123          if (!self::$utcTimeZone) {
 124              self::$utcTimeZone = new \DateTimeZone('UTC');
 125          }
 126          // @codeCoverageIgnoreEnd
 127  
 128          return self::$utcTimeZone;
 129      }
 130  
 131      /**
 132       * Perform the actual DateTime formatting
 133       *
 134       * @param int|string|\DateTime $dateTime Date time value
 135       * @param string               $format   Format of the result
 136       *
 137       * @return string
 138       * @throws InvalidArgumentException
 139       */
 140      protected static function dateFormatter($dateTime, $format)
 141      {
 142          if (is_numeric($dateTime)) {
 143              return gmdate($format, (int) $dateTime);
 144          }
 145  
 146          if (is_string($dateTime)) {
 147              $dateTime = new \DateTime($dateTime);
 148          }
 149  
 150          if ($dateTime instanceof \DateTime) {
 151              return $dateTime->setTimezone(self::getUtcTimeZone())->format($format);
 152          }
 153  
 154          throw new InvalidArgumentException('Date/Time values must be either a string, integer, or DateTime object');
 155      }
 156  }

title

Description

title

Description

title

Description

title

title

Body