ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Json.php
Go to the documentation of this file.
1 <?php
2 
4 
9 
19 class Json extends Parser {
20 
26  protected $input;
27 
33  protected $root;
34 
48  function parse($input = null, $options = 0) {
49 
50  if (!is_null($input)) {
51  $this->setInput($input);
52  }
53  if (is_null($this->input)) {
54  throw new EofException('End of input stream, or no input supplied');
55  }
56 
57  if (0 !== $options) {
58  $this->options = $options;
59  }
60 
61  switch ($this->input[0]) {
62  case 'vcalendar' :
63  $this->root = new VCalendar([], false);
64  break;
65  case 'vcard' :
66  $this->root = new VCard([], false);
67  break;
68  default :
69  throw new ParseException('The root component must either be a vcalendar, or a vcard');
70 
71  }
72  foreach ($this->input[1] as $prop) {
73  $this->root->add($this->parseProperty($prop));
74  }
75  if (isset($this->input[2])) foreach ($this->input[2] as $comp) {
76  $this->root->add($this->parseComponent($comp));
77  }
78 
79  // Resetting the input so we can throw an feof exception the next time.
80  $this->input = null;
81 
82  return $this->root;
83 
84  }
85 
93  function parseComponent(array $jComp) {
94 
95  // We can remove $self from PHP 5.4 onward.
96  $self = $this;
97 
98  $properties = array_map(
99  function($jProp) use ($self) {
100  return $self->parseProperty($jProp);
101  },
102  $jComp[1]
103  );
104 
105  if (isset($jComp[2])) {
106 
107  $components = array_map(
108  function($jComp) use ($self) {
109  return $self->parseComponent($jComp);
110  },
111  $jComp[2]
112  );
113 
114  } else $components = [];
115 
116  return $this->root->createComponent(
117  $jComp[0],
118  array_merge($properties, $components),
119  $defaults = false
120  );
121 
122  }
123 
131  function parseProperty(array $jProp) {
132 
133  list(
134  $propertyName,
135  $parameters,
136  $valueType
137  ) = $jProp;
138 
139  $propertyName = strtoupper($propertyName);
140 
141  // This is the default class we would be using if we didn't know the
142  // value type. We're using this value later in this function.
143  $defaultPropertyClass = $this->root->getClassNameForPropertyName($propertyName);
144 
145  $parameters = (array)$parameters;
146 
147  $value = array_slice($jProp, 3);
148 
149  $valueType = strtoupper($valueType);
150 
151  if (isset($parameters['group'])) {
152  $propertyName = $parameters['group'] . '.' . $propertyName;
153  unset($parameters['group']);
154  }
155 
156  $prop = $this->root->createProperty($propertyName, null, $parameters, $valueType);
157  $prop->setJsonValue($value);
158 
159  // We have to do something awkward here. FlatText as well as Text
160  // represents TEXT values. We have to normalize these here. In the
161  // future we can get rid of FlatText once we're allowed to break BC
162  // again.
163  if ($defaultPropertyClass === 'Sabre\VObject\Property\FlatText') {
164  $defaultPropertyClass = 'Sabre\VObject\Property\Text';
165  }
166 
167  // If the value type we received (e.g.: TEXT) was not the default value
168  // type for the given property (e.g.: BDAY), we need to add a VALUE=
169  // parameter.
170  if ($defaultPropertyClass !== get_class($prop)) {
171  $prop["VALUE"] = $valueType;
172  }
173 
174  return $prop;
175 
176  }
177 
185  function setInput($input) {
186 
187  if (is_resource($input)) {
188  $input = stream_get_contents($input);
189  }
190  if (is_string($input)) {
191  $input = json_decode($input);
192  }
193  $this->input = $input;
194 
195  }
196 
197 }
setInput($input)
Sets the input data.
Definition: Json.php:185
Json Parser.
Definition: Json.php:19
parseComponent(array $jComp)
Parses a component.
Definition: Json.php:93
parse($input=null, $options=0)
This method starts the parsing process.
Definition: Json.php:48
The VCalendar component.
Definition: VCalendar.php:23
parseProperty(array $jProp)
Parses properties.
Definition: Json.php:131
input
Definition: langcheck.php:166
Abstract parser.
Definition: Parser.php:14
Exception thrown by Reader if an invalid object was attempted to be parsed.
Exception thrown by parser when the end of the stream has been reached, before this was expected...
The VCard component.
Definition: VCard.php:18