annotate vendor/sabre/vobject/lib/Parser/Json.php @ 7:430dbd5346f7

vendor sabre as distributed
author Charlie Root
date Sat, 13 Jan 2018 09:06:10 -0500
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
7
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
1 <?php
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
2
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
3 namespace Sabre\VObject\Parser;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
4
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
5 use
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
6 Sabre\VObject\Component\VCalendar,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
7 Sabre\VObject\Component\VCard,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
8 Sabre\VObject\ParseException,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
9 Sabre\VObject\EofException;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
10
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
11 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
12 * Json Parser.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
13 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
14 * This parser parses both the jCal and jCard formats.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
15 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
16 * @copyright Copyright (C) 2011-2015 fruux GmbH (https://fruux.com/).
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
17 * @author Evert Pot (http://evertpot.com/)
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
18 * @license http://sabre.io/license/ Modified BSD License
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
19 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
20 class Json extends Parser {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
21
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
22 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
23 * The input data
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
24 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
25 * @var array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
26 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
27 protected $input;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
28
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
29 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
30 * Root component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
31 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
32 * @var Document
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
33 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
34 protected $root;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
35
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
36 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
37 * This method starts the parsing process.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
38 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
39 * If the input was not supplied during construction, it's possible to pass
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
40 * it here instead.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
41 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
42 * If either input or options are not supplied, the defaults will be used.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
43 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
44 * @param resource|string|array|null $input
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
45 * @param int|null $options
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
46 * @return array
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
47 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
48 public function parse($input = null, $options = null) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
49
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
50 if (!is_null($input)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
51 $this->setInput($input);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
52 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
53 if (is_null($this->input)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
54 throw new EofException('End of input stream, or no input supplied');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
55 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
56
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
57 if (!is_null($options)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
58 $this->options = $options;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
59 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
60
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
61 switch($this->input[0]) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
62 case 'vcalendar' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
63 $this->root = new VCalendar(array(), false);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
64 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
65 case 'vcard' :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
66 $this->root = new VCard(array(), false);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
67 break;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
68 default :
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
69 throw new ParseException('The root component must either be a vcalendar, or a vcard');
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
70
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
71 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
72 foreach($this->input[1] as $prop) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
73 $this->root->add($this->parseProperty($prop));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
74 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
75 if (isset($this->input[2])) foreach($this->input[2] as $comp) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
76 $this->root->add($this->parseComponent($comp));
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
77 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
78
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
79 // Resetting the input so we can throw an feof exception the next time.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
80 $this->input = null;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
81
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
82 return $this->root;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
83
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
84 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
85
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
86 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
87 * Parses a component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
88 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
89 * @param array $jComp
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
90 * @return \Sabre\VObject\Component
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
91 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
92 public function parseComponent(array $jComp) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
93
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
94 // We can remove $self from PHP 5.4 onward.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
95 $self = $this;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
96
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
97 $properties = array_map(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
98 function($jProp) use ($self) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
99 return $self->parseProperty($jProp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
100 },
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
101 $jComp[1]
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
102 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
103
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
104 if (isset($jComp[2])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
105
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
106 $components = array_map(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
107 function($jComp) use ($self) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
108 return $self->parseComponent($jComp);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
109 },
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
110 $jComp[2]
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
111 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
112
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
113 } else $components = array();
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
114
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
115 return $this->root->createComponent(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
116 $jComp[0],
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
117 array_merge($properties, $components),
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
118 $defaults = false
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
119 );
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
120
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
121 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
122
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
123 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
124 * Parses properties.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
125 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
126 * @param array $jProp
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
127 * @return \Sabre\VObject\Property
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
128 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
129 public function parseProperty(array $jProp) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
130
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
131 list(
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
132 $propertyName,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
133 $parameters,
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
134 $valueType
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
135 ) = $jProp;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
136
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
137 $propertyName = strtoupper($propertyName);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
138
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
139 // This is the default class we would be using if we didn't know the
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
140 // value type. We're using this value later in this function.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
141 $defaultPropertyClass = $this->root->getClassNameForPropertyName($propertyName);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
142
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
143 $parameters = (array)$parameters;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
144
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
145 $value = array_slice($jProp, 3);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
146
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
147 $valueType = strtoupper($valueType);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
148
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
149 if (isset($parameters['group'])) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
150 $propertyName = $parameters['group'] . '.' . $propertyName;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
151 unset($parameters['group']);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
152 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
153
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
154 $prop = $this->root->createProperty($propertyName, null, $parameters, $valueType);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
155 $prop->setJsonValue($value);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
156
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
157 // We have to do something awkward here. FlatText as well as Text
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
158 // represents TEXT values. We have to normalize these here. In the
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
159 // future we can get rid of FlatText once we're allowed to break BC
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
160 // again.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
161 if ($defaultPropertyClass === 'Sabre\VObject\Property\FlatText') {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
162 $defaultPropertyClass = 'Sabre\VObject\Property\Text';
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
163 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
164
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
165 // If the value type we received (e.g.: TEXT) was not the default value
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
166 // type for the given property (e.g.: BDAY), we need to add a VALUE=
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
167 // parameter.
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
168 if ($defaultPropertyClass !== get_class($prop)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
169 $prop["VALUE"] = $valueType;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
170 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
171
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
172 return $prop;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
173
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
174 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
175
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
176 /**
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
177 * Sets the input data
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
178 *
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
179 * @param resource|string|array $input
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
180 * @return void
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
181 */
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
182 public function setInput($input) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
183
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
184 if (is_resource($input)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
185 $input = stream_get_contents($input);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
186 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
187 if (is_string($input)) {
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
188 $input = json_decode($input);
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
189 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
190 $this->input = $input;
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
191
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
192 }
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
193
430dbd5346f7 vendor sabre as distributed
Charlie Root
parents:
diff changeset
194 }