Period.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace Sabre\VObject\Property\ICalendar;
  3. use
  4. Sabre\VObject\Property,
  5. Sabre\VObject\Parser\MimeDir,
  6. Sabre\VObject\DateTimeParser;
  7. /**
  8. * Period property
  9. *
  10. * This object represents PERIOD values, as defined here:
  11. *
  12. * http://tools.ietf.org/html/rfc5545#section-3.8.2.6
  13. *
  14. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  15. * @author Evert Pot (http://evertpot.com/)
  16. * @license http://sabre.io/license/ Modified BSD License
  17. */
  18. class Period extends Property {
  19. /**
  20. * In case this is a multi-value property. This string will be used as a
  21. * delimiter.
  22. *
  23. * @var string|null
  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. * @return void
  34. */
  35. public function setRawMimeDirValue($val) {
  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. return implode($this->delimiter, $this->getParts());
  45. }
  46. /**
  47. * Returns the type of value.
  48. *
  49. * This corresponds to the VALUE= parameter. Every property also has a
  50. * 'default' valueType.
  51. *
  52. * @return string
  53. */
  54. public function getValueType() {
  55. return "PERIOD";
  56. }
  57. /**
  58. * Sets the json value, as it would appear in a jCard or jCal object.
  59. *
  60. * The value must always be an array.
  61. *
  62. * @param array $value
  63. * @return void
  64. */
  65. public function setJsonValue(array $value) {
  66. $value = array_map(
  67. function($item) {
  68. return strtr(implode('/', $item), array(':' => '', '-' => ''));
  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. $return = array();
  83. foreach($this->getParts() as $item) {
  84. list($start, $end) = explode('/', $item, 2);
  85. $start = DateTimeParser::parseDateTime($start);
  86. // This is a duration value.
  87. if ($end[0]==='P') {
  88. $return[] = array(
  89. $start->format('Y-m-d\\TH:i:s'),
  90. $end
  91. );
  92. } else {
  93. $end = DateTimeParser::parseDateTime($end);
  94. $return[] = array(
  95. $start->format('Y-m-d\\TH:i:s'),
  96. $end->format('Y-m-d\\TH:i:s'),
  97. );
  98. }
  99. }
  100. return $return;
  101. }
  102. }