SchemaFormatter.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. *
  43. * @throws \InvalidArgumentException
  44. */
  45. protected function dateFormatter($dateTime, $format)
  46. {
  47. if (is_numeric($dateTime)) {
  48. return gmdate($format, (int) $dateTime);
  49. }
  50. if (is_string($dateTime)) {
  51. $dateTime = new \DateTime($dateTime);
  52. }
  53. if ($dateTime instanceof \DateTimeInterface) {
  54. static $utc;
  55. if (!$utc) {
  56. $utc = new \DateTimeZone('UTC');
  57. }
  58. return $dateTime->setTimezone($utc)->format($format);
  59. }
  60. throw new \InvalidArgumentException('Date/Time values must be either '
  61. .'be a string, integer, or DateTime object');
  62. }
  63. /**
  64. * Create a ISO 8601 (YYYY-MM-DDThh:mm:ssZ) formatted date time value in
  65. * UTC time.
  66. *
  67. * @param string|int|\DateTime $value Date time value
  68. *
  69. * @return string
  70. */
  71. private function formatDateTime($value)
  72. {
  73. return $this->dateFormatter($value, 'Y-m-d\TH:i:s\Z');
  74. }
  75. /**
  76. * Create an HTTP date (RFC 1123 / RFC 822) formatted UTC date-time string
  77. *
  78. * @param string|int|\DateTime $value Date time value
  79. *
  80. * @return string
  81. */
  82. private function formatDateTimeHttp($value)
  83. {
  84. return $this->dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
  85. }
  86. /**
  87. * Create a YYYY-MM-DD formatted string
  88. *
  89. * @param string|int|\DateTime $value Date time value
  90. *
  91. * @return string
  92. */
  93. private function formatDate($value)
  94. {
  95. return $this->dateFormatter($value, 'Y-m-d');
  96. }
  97. /**
  98. * Create a hh:mm:ss formatted string
  99. *
  100. * @param string|int|\DateTime $value Date time value
  101. *
  102. * @return string
  103. */
  104. private function formatTime($value)
  105. {
  106. return $this->dateFormatter($value, 'H:i:s');
  107. }
  108. /**
  109. * Formats a boolean value as a string
  110. *
  111. * @param string|int|bool $value Value to convert to a boolean
  112. * 'true' / 'false' value
  113. *
  114. * @return string
  115. */
  116. private function formatBooleanAsString($value)
  117. {
  118. return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
  119. }
  120. /**
  121. * Return a UNIX timestamp in the UTC timezone
  122. *
  123. * @param string|int|\DateTime $value Time value
  124. *
  125. * @return int
  126. */
  127. private function formatTimestamp($value)
  128. {
  129. return (int) $this->dateFormatter($value, 'U');
  130. }
  131. }