Recur.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <?php
  2. namespace Sabre\VObject\Property\ICalendar;
  3. use
  4. Sabre\VObject\Property,
  5. Sabre\VObject\Parser\MimeDir;
  6. /**
  7. * Recur property
  8. *
  9. * This object represents RECUR properties.
  10. * These values are just used for RRULE and the now deprecated EXRULE.
  11. *
  12. * The RRULE property may look something like this:
  13. *
  14. * RRULE:FREQ=MONTHLY;BYDAY=1,2,3;BYHOUR=5.
  15. *
  16. * This property exposes this as a key=>value array that is accessible using
  17. * getParts, and may be set using setParts.
  18. *
  19. * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
  20. * @author Evert Pot (http://evertpot.com/)
  21. * @license http://sabre.io/license/ Modified BSD License
  22. */
  23. class Recur extends Property {
  24. /**
  25. * Updates the current value.
  26. *
  27. * This may be either a single, or multiple strings in an array.
  28. *
  29. * @param string|array $value
  30. * @return void
  31. */
  32. public function setValue($value) {
  33. // If we're getting the data from json, we'll be receiving an object
  34. if ($value instanceof \StdClass) {
  35. $value = (array)$value;
  36. }
  37. if (is_array($value)) {
  38. $newVal = array();
  39. foreach($value as $k=>$v) {
  40. if (is_string($v)) {
  41. $v = strtoupper($v);
  42. // The value had multiple sub-values
  43. if (strpos($v,',')!==false) {
  44. $v = explode(',', $v);
  45. }
  46. } else {
  47. $v = array_map('strtoupper', $v);
  48. }
  49. $newVal[strtoupper($k)] = $v;
  50. }
  51. $this->value = $newVal;
  52. } elseif (is_string($value)) {
  53. $this->value = self::stringToArray($value);
  54. } else {
  55. throw new \InvalidArgumentException('You must either pass a string, or a key=>value array');
  56. }
  57. }
  58. /**
  59. * Returns the current value.
  60. *
  61. * This method will always return a singular value. If this was a
  62. * multi-value object, some decision will be made first on how to represent
  63. * it as a string.
  64. *
  65. * To get the correct multi-value version, use getParts.
  66. *
  67. * @return string
  68. */
  69. public function getValue() {
  70. $out = array();
  71. foreach($this->value as $key=>$value) {
  72. $out[] = $key . '=' . (is_array($value)?implode(',', $value):$value);
  73. }
  74. return strtoupper(implode(';',$out));
  75. }
  76. /**
  77. * Sets a multi-valued property.
  78. *
  79. * @param array $parts
  80. * @return void
  81. */
  82. public function setParts(array $parts) {
  83. $this->setValue($parts);
  84. }
  85. /**
  86. * Returns a multi-valued property.
  87. *
  88. * This method always returns an array, if there was only a single value,
  89. * it will still be wrapped in an array.
  90. *
  91. * @return array
  92. */
  93. public function getParts() {
  94. return $this->value;
  95. }
  96. /**
  97. * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
  98. *
  99. * This has been 'unfolded', so only 1 line will be passed. Unescaping is
  100. * not yet done, but parameters are not included.
  101. *
  102. * @param string $val
  103. * @return void
  104. */
  105. public function setRawMimeDirValue($val) {
  106. $this->setValue($val);
  107. }
  108. /**
  109. * Returns a raw mime-dir representation of the value.
  110. *
  111. * @return string
  112. */
  113. public function getRawMimeDirValue() {
  114. return $this->getValue();
  115. }
  116. /**
  117. * Returns the type of value.
  118. *
  119. * This corresponds to the VALUE= parameter. Every property also has a
  120. * 'default' valueType.
  121. *
  122. * @return string
  123. */
  124. public function getValueType() {
  125. return "RECUR";
  126. }
  127. /**
  128. * Returns the value, in the format it should be encoded for json.
  129. *
  130. * This method must always return an array.
  131. *
  132. * @return array
  133. */
  134. public function getJsonValue() {
  135. $values = array();
  136. foreach($this->getParts() as $k=>$v) {
  137. $values[strtolower($k)] = $v;
  138. }
  139. return array($values);
  140. }
  141. /**
  142. * Parses an RRULE value string, and turns it into a struct-ish array.
  143. *
  144. * @param string $value
  145. * @return array
  146. */
  147. static function stringToArray($value) {
  148. $value = strtoupper($value);
  149. $newValue = array();
  150. foreach(explode(';', $value) as $part) {
  151. // Skipping empty parts.
  152. if (empty($part)) {
  153. continue;
  154. }
  155. list($partName, $partValue) = explode('=', $part);
  156. // The value itself had multiple values..
  157. if (strpos($partValue,',')!==false) {
  158. $partValue=explode(',', $partValue);
  159. }
  160. $newValue[$partName] = $partValue;
  161. }
  162. return $newValue;
  163. }
  164. }