Time.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace Sabre\VObject\Property;
  3. use Sabre\VObject\DateTimeParser;
  4. /**
  5. * Time property.
  6. *
  7. * This object encodes TIME values.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. class Time extends Text
  14. {
  15. /**
  16. * In case this is a multi-value property. This string will be used as a
  17. * delimiter.
  18. *
  19. * @var string
  20. */
  21. public $delimiter = '';
  22. /**
  23. * Returns the type of value.
  24. *
  25. * This corresponds to the VALUE= parameter. Every property also has a
  26. * 'default' valueType.
  27. *
  28. * @return string
  29. */
  30. public function getValueType()
  31. {
  32. return 'TIME';
  33. }
  34. /**
  35. * Sets the JSON value, as it would appear in a jCard or jCal object.
  36. *
  37. * The value must always be an array.
  38. */
  39. public function setJsonValue(array $value)
  40. {
  41. // Removing colons from value.
  42. $value = str_replace(
  43. ':',
  44. '',
  45. $value
  46. );
  47. if (1 === count($value)) {
  48. $this->setValue(reset($value));
  49. } else {
  50. $this->setValue($value);
  51. }
  52. }
  53. /**
  54. * Returns the value, in the format it should be encoded for json.
  55. *
  56. * This method must always return an array.
  57. *
  58. * @return array
  59. */
  60. public function getJsonValue()
  61. {
  62. $parts = DateTimeParser::parseVCardTime($this->getValue());
  63. $timeStr = '';
  64. // Hour
  65. if (!is_null($parts['hour'])) {
  66. $timeStr .= $parts['hour'];
  67. if (!is_null($parts['minute'])) {
  68. $timeStr .= ':';
  69. }
  70. } else {
  71. // We know either minute or second _must_ be set, so we insert a
  72. // dash for an empty value.
  73. $timeStr .= '-';
  74. }
  75. // Minute
  76. if (!is_null($parts['minute'])) {
  77. $timeStr .= $parts['minute'];
  78. if (!is_null($parts['second'])) {
  79. $timeStr .= ':';
  80. }
  81. } else {
  82. if (isset($parts['second'])) {
  83. // Dash for empty minute
  84. $timeStr .= '-';
  85. }
  86. }
  87. // Second
  88. if (!is_null($parts['second'])) {
  89. $timeStr .= $parts['second'];
  90. }
  91. // Timezone
  92. if (!is_null($parts['timezone'])) {
  93. if ('Z' === $parts['timezone']) {
  94. $timeStr .= 'Z';
  95. } else {
  96. $timeStr .=
  97. preg_replace('/([0-9]{2})([0-9]{2})$/', '$1:$2', $parts['timezone']);
  98. }
  99. }
  100. return [$timeStr];
  101. }
  102. /**
  103. * Hydrate data from a XML subtree, as it would appear in a xCard or xCal
  104. * object.
  105. */
  106. public function setXmlValue(array $value)
  107. {
  108. $value = array_map(
  109. function ($value) {
  110. return str_replace(':', '', $value);
  111. },
  112. $value
  113. );
  114. parent::setXmlValue($value);
  115. }
  116. }