Overview

Namespaces

  • Contrib
    • Bundle
      • CoverallsBundle
        • Console
        • Entity
      • CoverallsV1Bundle
        • Api
        • Collector
        • Command
        • Config
        • Entity
          • Git
    • Component
      • File
      • Log
      • System
        • Git
  • Guzzle
    • Batch
      • Exception
    • Cache
    • Common
      • Exception
    • Http
      • Curl
      • Exception
      • Message
      • QueryAggregator
    • Inflection
    • Iterator
    • Log
    • Parser
      • Cookie
      • Message
      • UriTemplate
      • Url
    • Plugin
      • Async
      • Backoff
      • Cache
      • Cookie
        • CookieJar
        • Exception
      • CurlAuth
      • ErrorResponse
        • Exception
      • History
      • Log
      • Md5
      • Mock
      • Oauth
    • Service
      • Builder
      • Command
        • Factory
        • LocationVisitor
          • Request
          • Response
      • Description
      • Exception
      • Resource
    • Stream
  • PHP
  • Psr
    • Log
  • Symfony
    • Component
      • Config
        • Definition
          • Builder
          • Exception
        • Exception
        • Loader
        • Resource
        • Util
      • Console
        • Command
        • Formatter
        • Helper
        • Input
        • Output
        • Tester
      • EventDispatcher
        • Debug
      • Finder
        • Adapter
        • Comparator
        • Exception
        • Expression
        • Iterator
        • Shell
      • Stopwatch
      • Yaml
        • Exception

Classes

  • Operation
  • Parameter
  • SchemaFormatter
  • SchemaValidator
  • ServiceDescription
  • ServiceDescriptionLoader

Interfaces

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