annotate plugins/libcalendaring/lib/Sabre/VObject/Property/Compound.php @ 4:888e774ee983

libcalendar plugin as distributed
author Charlie Root
date Sat, 13 Jan 2018 08:57:56 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
1 <?php
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject\Property;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
5 use Sabre\VObject;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
6
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
7 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
8 * Compound property.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
9 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
10 * This class adds (de)serialization of compound properties to/from arrays.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
11 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
12 * Currently the following properties from RFC 6350 are mapped to use this
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
13 * class:
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
14 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
15 * N: Section 6.2.2
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
16 * ADR: Section 6.3.1
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
17 * ORG: Section 6.6.4
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
18 * CATEGORIES: Section 6.7.1
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
19 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
20 * In order to use this correctly, you must call setParts and getParts to
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
21 * retrieve and modify dates respectively.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
22 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
23 * @author Thomas Tanghus (http://tanghus.net/)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
24 * @author Lars Kneschke
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
25 * @author Evert Pot (http://evertpot.com/)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
26 * @copyright Copyright (C) 2007-2013 fruux GmbH (https://fruux.com/).
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
27 * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
28 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
29 class Compound extends VObject\Property {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
30
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
31 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
32 * If property names are added to this map, they will be (de)serialised as arrays
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
33 * using the getParts() and setParts() methods.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
34 * The keys are the property names, values are delimiter chars.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
35 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
36 * @var array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
37 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
38 static public $delimiterMap = array(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
39 'N' => ';',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
40 'ADR' => ';',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
41 'ORG' => ';',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
42 'CATEGORIES' => ',',
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
43 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
44
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
45 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
46 * The currently used delimiter.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
47 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
48 * @var string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
49 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
50 protected $delimiter = null;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
51
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
52 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
53 * Get a compound value as an array.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
54 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
55 * @param $name string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
56 * @return array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
57 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
58 public function getParts() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
59
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
60 if (is_null($this->value)) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
61 return array();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
62 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
63
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
64 $delimiter = $this->getDelimiter();
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
65
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
66 // split by any $delimiter which is NOT prefixed by a slash.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
67 // Note that this is not a a perfect solution. If a value is prefixed
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
68 // by two slashes, it should actually be split anyway.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
69 //
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
70 // Hopefully we can fix this better in a future version, where we can
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
71 // break compatibility a bit.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
72 $compoundValues = preg_split("/(?<!\\\)$delimiter/", $this->value);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
73
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
74 // remove slashes from any semicolon and comma left escaped in the single values
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
75 $compoundValues = array_map(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
76 function($val) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
77 return strtr($val, array('\,' => ',', '\;' => ';'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
78 }, $compoundValues);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
79
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
80 return $compoundValues;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
81
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
82 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
83
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
84 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
85 * Returns the delimiter for this property.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
86 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
87 * @return string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
88 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
89 public function getDelimiter() {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
90
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
91 if (!$this->delimiter) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
92 if (isset(self::$delimiterMap[$this->name])) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
93 $this->delimiter = self::$delimiterMap[$this->name];
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
94 } else {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
95 // To be a bit future proof, we are going to default the
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
96 // delimiter to ;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
97 $this->delimiter = ';';
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
98 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
99 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
100 return $this->delimiter;
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
101
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
102 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
103
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
104 /**
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
105 * Set a compound value as an array.
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
106 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
107 *
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
108 * @param $name string
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
109 * @return array
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
110 */
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
111 public function setParts(array $values) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
112
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
113 // add slashes to all semicolons and commas in the single values
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
114 $values = array_map(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
115 function($val) {
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
116 return strtr($val, array(',' => '\,', ';' => '\;'));
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
117 }, $values);
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
118
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
119 $this->setValue(
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
120 implode($this->getDelimiter(), $values)
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
121 );
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
122
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
123 }
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
124
888e774ee983 libcalendar plugin as distributed
Charlie Root
parents:
diff changeset
125 }