1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Xml\Response; 4*a1a3b679SAndreas Boehler 5*a1a3b679SAndreas Boehleruse Sabre\Xml\Element; 6*a1a3b679SAndreas Boehleruse Sabre\Xml\Reader; 7*a1a3b679SAndreas Boehleruse Sabre\Xml\Writer; 8*a1a3b679SAndreas Boehler 9*a1a3b679SAndreas Boehler/** 10*a1a3b679SAndreas Boehler * WebDAV MultiStatus parser 11*a1a3b679SAndreas Boehler * 12*a1a3b679SAndreas Boehler * This class parses the {DAV:}multistatus response, as defined in: 13*a1a3b679SAndreas Boehler * https://tools.ietf.org/html/rfc4918#section-14.16 14*a1a3b679SAndreas Boehler * 15*a1a3b679SAndreas Boehler * And it also adds the {DAV:}synctoken change from: 16*a1a3b679SAndreas Boehler * http://tools.ietf.org/html/rfc6578#section-6.4 17*a1a3b679SAndreas Boehler * 18*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 19*a1a3b679SAndreas Boehler * @author Evert Pot (http://evertpot.com/) 20*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 21*a1a3b679SAndreas Boehler */ 22*a1a3b679SAndreas Boehlerclass MultiStatus implements Element { 23*a1a3b679SAndreas Boehler 24*a1a3b679SAndreas Boehler /** 25*a1a3b679SAndreas Boehler * The responses 26*a1a3b679SAndreas Boehler * 27*a1a3b679SAndreas Boehler * @var \Sabre\DAV\Xml\Element\Response[] 28*a1a3b679SAndreas Boehler */ 29*a1a3b679SAndreas Boehler protected $responses; 30*a1a3b679SAndreas Boehler 31*a1a3b679SAndreas Boehler /** 32*a1a3b679SAndreas Boehler * A sync token (from RFC6578). 33*a1a3b679SAndreas Boehler * 34*a1a3b679SAndreas Boehler * @var string 35*a1a3b679SAndreas Boehler */ 36*a1a3b679SAndreas Boehler protected $syncToken; 37*a1a3b679SAndreas Boehler 38*a1a3b679SAndreas Boehler /** 39*a1a3b679SAndreas Boehler * Constructor 40*a1a3b679SAndreas Boehler * 41*a1a3b679SAndreas Boehler * @param \Sabre\DAV\Xml\Element\Response[] $responses 42*a1a3b679SAndreas Boehler * @param string $syncToken 43*a1a3b679SAndreas Boehler */ 44*a1a3b679SAndreas Boehler function __construct(array $responses, $syncToken = null) { 45*a1a3b679SAndreas Boehler 46*a1a3b679SAndreas Boehler $this->responses = $responses; 47*a1a3b679SAndreas Boehler $this->syncToken = $syncToken; 48*a1a3b679SAndreas Boehler 49*a1a3b679SAndreas Boehler } 50*a1a3b679SAndreas Boehler 51*a1a3b679SAndreas Boehler /** 52*a1a3b679SAndreas Boehler * Returns the response list. 53*a1a3b679SAndreas Boehler * 54*a1a3b679SAndreas Boehler * @return \Sabre\DAV\Xml\Element\Response[] 55*a1a3b679SAndreas Boehler */ 56*a1a3b679SAndreas Boehler function getResponses() { 57*a1a3b679SAndreas Boehler 58*a1a3b679SAndreas Boehler return $this->responses; 59*a1a3b679SAndreas Boehler 60*a1a3b679SAndreas Boehler } 61*a1a3b679SAndreas Boehler 62*a1a3b679SAndreas Boehler /** 63*a1a3b679SAndreas Boehler * Returns the sync-token, if available. 64*a1a3b679SAndreas Boehler * 65*a1a3b679SAndreas Boehler * @return string|null 66*a1a3b679SAndreas Boehler */ 67*a1a3b679SAndreas Boehler function getSyncToken() { 68*a1a3b679SAndreas Boehler 69*a1a3b679SAndreas Boehler return $this->syncToken; 70*a1a3b679SAndreas Boehler 71*a1a3b679SAndreas Boehler } 72*a1a3b679SAndreas Boehler 73*a1a3b679SAndreas Boehler /** 74*a1a3b679SAndreas Boehler * The serialize method is called during xml writing. 75*a1a3b679SAndreas Boehler * 76*a1a3b679SAndreas Boehler * It should use the $writer argument to encode this object into XML. 77*a1a3b679SAndreas Boehler * 78*a1a3b679SAndreas Boehler * Important note: it is not needed to create the parent element. The 79*a1a3b679SAndreas Boehler * parent element is already created, and we only have to worry about 80*a1a3b679SAndreas Boehler * attributes, child elements and text (if any). 81*a1a3b679SAndreas Boehler * 82*a1a3b679SAndreas Boehler * Important note 2: If you are writing any new elements, you are also 83*a1a3b679SAndreas Boehler * responsible for closing them. 84*a1a3b679SAndreas Boehler * 85*a1a3b679SAndreas Boehler * @param Writer $writer 86*a1a3b679SAndreas Boehler * @return void 87*a1a3b679SAndreas Boehler */ 88*a1a3b679SAndreas Boehler function xmlSerialize(Writer $writer) { 89*a1a3b679SAndreas Boehler 90*a1a3b679SAndreas Boehler foreach ($this->getResponses() as $response) { 91*a1a3b679SAndreas Boehler $writer->writeElement('{DAV:}response', $response); 92*a1a3b679SAndreas Boehler } 93*a1a3b679SAndreas Boehler if ($syncToken = $this->getSyncToken()) { 94*a1a3b679SAndreas Boehler $writer->writeElement('{DAV:}sync-token', $syncToken); 95*a1a3b679SAndreas Boehler } 96*a1a3b679SAndreas Boehler 97*a1a3b679SAndreas Boehler } 98*a1a3b679SAndreas Boehler 99*a1a3b679SAndreas Boehler /** 100*a1a3b679SAndreas Boehler * The deserialize method is called during xml parsing. 101*a1a3b679SAndreas Boehler * 102*a1a3b679SAndreas Boehler * This method is called statictly, this is because in theory this method 103*a1a3b679SAndreas Boehler * may be used as a type of constructor, or factory method. 104*a1a3b679SAndreas Boehler * 105*a1a3b679SAndreas Boehler * Often you want to return an instance of the current class, but you are 106*a1a3b679SAndreas Boehler * free to return other data as well. 107*a1a3b679SAndreas Boehler * 108*a1a3b679SAndreas Boehler * You are responsible for advancing the reader to the next element. Not 109*a1a3b679SAndreas Boehler * doing anything will result in a never-ending loop. 110*a1a3b679SAndreas Boehler * 111*a1a3b679SAndreas Boehler * If you just want to skip parsing for this element altogether, you can 112*a1a3b679SAndreas Boehler * just call $reader->next(); 113*a1a3b679SAndreas Boehler * 114*a1a3b679SAndreas Boehler * $reader->parseInnerTree() will parse the entire sub-tree, and advance to 115*a1a3b679SAndreas Boehler * the next element. 116*a1a3b679SAndreas Boehler * 117*a1a3b679SAndreas Boehler * @param Reader $reader 118*a1a3b679SAndreas Boehler * @return mixed 119*a1a3b679SAndreas Boehler */ 120*a1a3b679SAndreas Boehler static function xmlDeserialize(Reader $reader) { 121*a1a3b679SAndreas Boehler 122*a1a3b679SAndreas Boehler $elementMap = $reader->elementMap; 123*a1a3b679SAndreas Boehler $elementMap['{DAV:}prop'] = 'Sabre\\DAV\\Xml\\Element\\Prop'; 124*a1a3b679SAndreas Boehler $elements = $reader->parseInnerTree($elementMap); 125*a1a3b679SAndreas Boehler 126*a1a3b679SAndreas Boehler $responses = []; 127*a1a3b679SAndreas Boehler $syncToken = null; 128*a1a3b679SAndreas Boehler 129*a1a3b679SAndreas Boehler if ($elements) foreach ($elements as $elem) { 130*a1a3b679SAndreas Boehler if ($elem['name'] === '{DAV:}response') { 131*a1a3b679SAndreas Boehler $responses[] = $elem['value']; 132*a1a3b679SAndreas Boehler } 133*a1a3b679SAndreas Boehler if ($elem['name'] === '{DAV:}sync-token') { 134*a1a3b679SAndreas Boehler $syncToken = $elem['value']; 135*a1a3b679SAndreas Boehler } 136*a1a3b679SAndreas Boehler } 137*a1a3b679SAndreas Boehler 138*a1a3b679SAndreas Boehler return new self($responses, $syncToken); 139*a1a3b679SAndreas Boehler 140*a1a3b679SAndreas Boehler } 141*a1a3b679SAndreas Boehler 142*a1a3b679SAndreas Boehler} 143