123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- <?php
- namespace Sabre\VObject\Property\ICalendar;
- use
- Sabre\VObject\Property,
- Sabre\VObject\Parser\MimeDir;
- /**
- * Recur property
- *
- * This object represents RECUR properties.
- * These values are just used for RRULE and the now deprecated EXRULE.
- *
- * The RRULE property may look something like this:
- *
- * RRULE:FREQ=MONTHLY;BYDAY=1,2,3;BYHOUR=5.
- *
- * This property exposes this as a key=>value array that is accessible using
- * getParts, and may be set using setParts.
- *
- * @copyright Copyright (C) 2007-2014 fruux GmbH. All rights reserved.
- * @author Evert Pot (http://evertpot.com/)
- * @license http://sabre.io/license/ Modified BSD License
- */
- class Recur extends Property {
- /**
- * Updates the current value.
- *
- * This may be either a single, or multiple strings in an array.
- *
- * @param string|array $value
- * @return void
- */
- public function setValue($value) {
- // If we're getting the data from json, we'll be receiving an object
- if ($value instanceof \StdClass) {
- $value = (array)$value;
- }
- if (is_array($value)) {
- $newVal = array();
- foreach($value as $k=>$v) {
- if (is_string($v)) {
- $v = strtoupper($v);
- // The value had multiple sub-values
- if (strpos($v,',')!==false) {
- $v = explode(',', $v);
- }
- } else {
- $v = array_map('strtoupper', $v);
- }
- $newVal[strtoupper($k)] = $v;
- }
- $this->value = $newVal;
- } elseif (is_string($value)) {
- $this->value = self::stringToArray($value);
- } else {
- throw new \InvalidArgumentException('You must either pass a string, or a key=>value array');
- }
- }
- /**
- * Returns the current value.
- *
- * This method will always return a singular value. If this was a
- * multi-value object, some decision will be made first on how to represent
- * it as a string.
- *
- * To get the correct multi-value version, use getParts.
- *
- * @return string
- */
- public function getValue() {
- $out = array();
- foreach($this->value as $key=>$value) {
- $out[] = $key . '=' . (is_array($value)?implode(',', $value):$value);
- }
- return strtoupper(implode(';',$out));
- }
- /**
- * Sets a multi-valued property.
- *
- * @param array $parts
- * @return void
- */
- public function setParts(array $parts) {
- $this->setValue($parts);
- }
- /**
- * Returns a multi-valued property.
- *
- * This method always returns an array, if there was only a single value,
- * it will still be wrapped in an array.
- *
- * @return array
- */
- public function getParts() {
- return $this->value;
- }
- /**
- * Sets a raw value coming from a mimedir (iCalendar/vCard) file.
- *
- * This has been 'unfolded', so only 1 line will be passed. Unescaping is
- * not yet done, but parameters are not included.
- *
- * @param string $val
- * @return void
- */
- public function setRawMimeDirValue($val) {
- $this->setValue($val);
- }
- /**
- * Returns a raw mime-dir representation of the value.
- *
- * @return string
- */
- public function getRawMimeDirValue() {
- return $this->getValue();
- }
- /**
- * Returns the type of value.
- *
- * This corresponds to the VALUE= parameter. Every property also has a
- * 'default' valueType.
- *
- * @return string
- */
- public function getValueType() {
- return "RECUR";
- }
- /**
- * Returns the value, in the format it should be encoded for json.
- *
- * This method must always return an array.
- *
- * @return array
- */
- public function getJsonValue() {
- $values = array();
- foreach($this->getParts() as $k=>$v) {
- $values[strtolower($k)] = $v;
- }
- return array($values);
- }
- /**
- * Parses an RRULE value string, and turns it into a struct-ish array.
- *
- * @param string $value
- * @return array
- */
- static function stringToArray($value) {
- $value = strtoupper($value);
- $newValue = array();
- foreach(explode(';', $value) as $part) {
- // Skipping empty parts.
- if (empty($part)) {
- continue;
- }
- list($partName, $partValue) = explode('=', $part);
- // The value itself had multiple values..
- if (strpos($partValue,',')!==false) {
- $partValue=explode(',', $partValue);
- }
- $newValue[$partName] = $partValue;
- }
- return $newValue;
- }
- }
|