123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194 |
- <?php
- namespace Sabre\VObject\Parser;
- use
- Sabre\VObject\Component\VCalendar,
- Sabre\VObject\Component\VCard,
- Sabre\VObject\ParseException,
- Sabre\VObject\EofException;
- class Json extends Parser {
-
- protected $input;
-
- protected $root;
-
- public function parse($input = null, $options = null) {
- if (!is_null($input)) {
- $this->setInput($input);
- }
- if (is_null($this->input)) {
- throw new EofException('End of input stream, or no input supplied');
- }
- if (!is_null($options)) {
- $this->options = $options;
- }
- switch($this->input[0]) {
- case 'vcalendar' :
- $this->root = new VCalendar(array(), false);
- break;
- case 'vcard' :
- $this->root = new VCard(array(), false);
- break;
- default :
- throw new ParseException('The root component must either be a vcalendar, or a vcard');
- }
- foreach($this->input[1] as $prop) {
- $this->root->add($this->parseProperty($prop));
- }
- if (isset($this->input[2])) foreach($this->input[2] as $comp) {
- $this->root->add($this->parseComponent($comp));
- }
-
- $this->input = null;
- return $this->root;
- }
-
- public function parseComponent(array $jComp) {
-
- $self = $this;
- $properties = array_map(
- function($jProp) use ($self) {
- return $self->parseProperty($jProp);
- },
- $jComp[1]
- );
- if (isset($jComp[2])) {
- $components = array_map(
- function($jComp) use ($self) {
- return $self->parseComponent($jComp);
- },
- $jComp[2]
- );
- } else $components = array();
- return $this->root->createComponent(
- $jComp[0],
- array_merge($properties, $components),
- $defaults = false
- );
- }
-
- public function parseProperty(array $jProp) {
- list(
- $propertyName,
- $parameters,
- $valueType
- ) = $jProp;
- $propertyName = strtoupper($propertyName);
-
-
- $defaultPropertyClass = $this->root->getClassNameForPropertyName($propertyName);
- $parameters = (array)$parameters;
- $value = array_slice($jProp, 3);
- $valueType = strtoupper($valueType);
- if (isset($parameters['group'])) {
- $propertyName = $parameters['group'] . '.' . $propertyName;
- unset($parameters['group']);
- }
- $prop = $this->root->createProperty($propertyName, null, $parameters, $valueType);
- $prop->setJsonValue($value);
-
-
-
-
- if ($defaultPropertyClass === 'Sabre\VObject\Property\FlatText') {
- $defaultPropertyClass = 'Sabre\VObject\Property\Text';
- }
-
-
-
- if ($defaultPropertyClass !== get_class($prop)) {
- $prop["VALUE"] = $valueType;
- }
- return $prop;
- }
-
- public function setInput($input) {
- if (is_resource($input)) {
- $input = stream_get_contents($input);
- }
- if (is_string($input)) {
- $input = json_decode($input);
- }
- $this->input = $input;
- }
- }
|