SchemaFormatter.php 3.6 KB

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