Period.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. namespace Sabre\VObject\Property\ICalendar;
  3. use Sabre\VObject\DateTimeParser;
  4. use Sabre\VObject\Property;
  5. use Sabre\Xml;
  6. /**
  7. * Period property.
  8. *
  9. * This object represents PERIOD values, as defined here:
  10. *
  11. * http://tools.ietf.org/html/rfc5545#section-3.8.2.6
  12. *
  13. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  14. * @author Evert Pot (http://evertpot.com/)
  15. * @license http://sabre.io/license/ Modified BSD License
  16. */
  17. class Period extends Property
  18. {
  19. /**
  20. * In case this is a multi-value property. This string will be used as a
  21. * delimiter.
  22. *
  23. * @var string
  24. */
  25. public $delimiter = ',';
  26. /**
  27. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  28. *
  29. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  30. * not yet done, but parameters are not included.
  31. *
  32. * @param string $val
  33. */
  34. public function setRawMimeDirValue($val)
  35. {
  36. $this->setValue(explode($this->delimiter, $val));
  37. }
  38. /**
  39. * Returns a raw mime-dir representation of the value.
  40. *
  41. * @return string
  42. */
  43. public function getRawMimeDirValue()
  44. {
  45. return implode($this->delimiter, $this->getParts());
  46. }
  47. /**
  48. * Returns the type of value.
  49. *
  50. * This corresponds to the VALUE= parameter. Every property also has a
  51. * 'default' valueType.
  52. *
  53. * @return string
  54. */
  55. public function getValueType()
  56. {
  57. return 'PERIOD';
  58. }
  59. /**
  60. * Sets the json value, as it would appear in a jCard or jCal object.
  61. *
  62. * The value must always be an array.
  63. */
  64. public function setJsonValue(array $value)
  65. {
  66. $value = array_map(
  67. function ($item) {
  68. return strtr(implode('/', $item), [':' => '', '-' => '']);
  69. },
  70. $value
  71. );
  72. parent::setJsonValue($value);
  73. }
  74. /**
  75. * Returns the value, in the format it should be encoded for json.
  76. *
  77. * This method must always return an array.
  78. *
  79. * @return array
  80. */
  81. public function getJsonValue()
  82. {
  83. $return = [];
  84. foreach ($this->getParts() as $item) {
  85. list($start, $end) = explode('/', $item, 2);
  86. $start = DateTimeParser::parseDateTime($start);
  87. // This is a duration value.
  88. if ('P' === $end[0]) {
  89. $return[] = [
  90. $start->format('Y-m-d\\TH:i:s'),
  91. $end,
  92. ];
  93. } else {
  94. $end = DateTimeParser::parseDateTime($end);
  95. $return[] = [
  96. $start->format('Y-m-d\\TH:i:s'),
  97. $end->format('Y-m-d\\TH:i:s'),
  98. ];
  99. }
  100. }
  101. return $return;
  102. }
  103. /**
  104. * This method serializes only the value of a property. This is used to
  105. * create xCard or xCal documents.
  106. *
  107. * @param Xml\Writer $writer XML writer
  108. */
  109. protected function xmlSerializeValue(Xml\Writer $writer)
  110. {
  111. $writer->startElement(strtolower($this->getValueType()));
  112. $value = $this->getJsonValue();
  113. $writer->writeElement('start', $value[0][0]);
  114. if ('P' === $value[0][1][0]) {
  115. $writer->writeElement('duration', $value[0][1]);
  116. } else {
  117. $writer->writeElement('end', $value[0][1]);
  118. }
  119. $writer->endElement();
  120. }
  121. }