SchemaFormatter.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * Copyright 2019 Huawei Technologies Co.,Ltd.
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not use
  5. * this file except in compliance with the License. You may obtain a copy of the
  6. * License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software distributed
  11. * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. * CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. * specific language governing permissions and limitations under the License.
  14. *
  15. */
  16. namespace Obs\Internal\Common;
  17. class SchemaFormatter
  18. {
  19. protected static $utcTimeZone;
  20. public static function format($fmt, $value)
  21. {
  22. if($fmt === 'date-time'){
  23. return self::formatDateTime($value);
  24. }
  25. if($fmt === 'data-time-http'){
  26. return self::formatDateTimeHttp($value);
  27. }
  28. if($fmt === 'data-time-middle'){
  29. return self::formatDateTimeMiddle($value);
  30. }
  31. if($fmt === 'date'){
  32. return self::formatDate($value);
  33. }
  34. if($fmt === 'timestamp'){
  35. return self::formatTimestamp($value);
  36. }
  37. if($fmt === 'boolean-string'){
  38. return self::formatBooleanAsString($value);
  39. }
  40. return $value;
  41. }
  42. public static function formatDateTimeMiddle($dateTime)
  43. {
  44. if (is_string($dateTime)) {
  45. $dateTime = new \DateTime($dateTime);
  46. }
  47. if ($dateTime instanceof \DateTime) {
  48. return $dateTime -> format('Y-m-d\T00:00:00\Z');
  49. }
  50. return null;
  51. }
  52. public static function formatDateTime($value)
  53. {
  54. return self::dateFormatter($value, 'Y-m-d\TH:i:s\Z');
  55. }
  56. public static function formatDateTimeHttp($value)
  57. {
  58. return self::dateFormatter($value, 'D, d M Y H:i:s \G\M\T');
  59. }
  60. public static function formatDate($value)
  61. {
  62. return self::dateFormatter($value, 'Y-m-d');
  63. }
  64. public static function formatTime($value)
  65. {
  66. return self::dateFormatter($value, 'H:i:s');
  67. }
  68. public static function formatBooleanAsString($value)
  69. {
  70. return filter_var($value, FILTER_VALIDATE_BOOLEAN) ? 'true' : 'false';
  71. }
  72. public static function formatTimestamp($value)
  73. {
  74. return (int) self::dateFormatter($value, 'U');
  75. }
  76. private static function dateFormatter($dt, $fmt)
  77. {
  78. if (is_numeric($dt)) {
  79. return gmdate($fmt, (int) $dt);
  80. }
  81. if (is_string($dt)) {
  82. $dt = new \DateTime($dt);
  83. }
  84. if ($dt instanceof \DateTime) {
  85. if (!self::$utcTimeZone) {
  86. self::$utcTimeZone = new \DateTimeZone('UTC');
  87. }
  88. return $dt->setTimezone(self::$utcTimeZone)->format($fmt);
  89. }
  90. return null;
  91. }
  92. }