1*a1a3b679SAndreas Boehler<?php 2*a1a3b679SAndreas Boehler 3*a1a3b679SAndreas Boehlernamespace Sabre\DAV\Xml\Request; 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 PROPPATCH request parser. 11*a1a3b679SAndreas Boehler * 12*a1a3b679SAndreas Boehler * This class parses the {DAV:}propertyupdate request, as defined in: 13*a1a3b679SAndreas Boehler * 14*a1a3b679SAndreas Boehler * https://tools.ietf.org/html/rfc4918#section-14.20 15*a1a3b679SAndreas Boehler * 16*a1a3b679SAndreas Boehler * @copyright Copyright (C) 2007-2015 fruux GmbH (https://fruux.com/). 17*a1a3b679SAndreas Boehler * @author Evert Pot (http://www.rooftopsolutions.nl/) 18*a1a3b679SAndreas Boehler * @license http://sabre.io/license/ Modified BSD License 19*a1a3b679SAndreas Boehler */ 20*a1a3b679SAndreas Boehlerclass PropPatch implements Element { 21*a1a3b679SAndreas Boehler 22*a1a3b679SAndreas Boehler /** 23*a1a3b679SAndreas Boehler * The list of properties that will be updated and removed. 24*a1a3b679SAndreas Boehler * 25*a1a3b679SAndreas Boehler * If a property will be removed, it's value will be set to null. 26*a1a3b679SAndreas Boehler * 27*a1a3b679SAndreas Boehler * @var array 28*a1a3b679SAndreas Boehler */ 29*a1a3b679SAndreas Boehler public $properties = []; 30*a1a3b679SAndreas Boehler 31*a1a3b679SAndreas Boehler /** 32*a1a3b679SAndreas Boehler * The xmlSerialize metod is called during xml writing. 33*a1a3b679SAndreas Boehler * 34*a1a3b679SAndreas Boehler * Use the $writer argument to write its own xml serialization. 35*a1a3b679SAndreas Boehler * 36*a1a3b679SAndreas Boehler * An important note: do _not_ create a parent element. Any element 37*a1a3b679SAndreas Boehler * implementing XmlSerializble should only ever write what's considered 38*a1a3b679SAndreas Boehler * its 'inner xml'. 39*a1a3b679SAndreas Boehler * 40*a1a3b679SAndreas Boehler * The parent of the current element is responsible for writing a 41*a1a3b679SAndreas Boehler * containing element. 42*a1a3b679SAndreas Boehler * 43*a1a3b679SAndreas Boehler * This allows serializers to be re-used for different element names. 44*a1a3b679SAndreas Boehler * 45*a1a3b679SAndreas Boehler * If you are opening new elements, you must also close them again. 46*a1a3b679SAndreas Boehler * 47*a1a3b679SAndreas Boehler * @param Writer $writer 48*a1a3b679SAndreas Boehler * @return void 49*a1a3b679SAndreas Boehler */ 50*a1a3b679SAndreas Boehler function xmlSerialize(Writer $writer) { 51*a1a3b679SAndreas Boehler 52*a1a3b679SAndreas Boehler foreach ($this->properties as $propertyName => $propertyValue) { 53*a1a3b679SAndreas Boehler 54*a1a3b679SAndreas Boehler if (is_null($propertyValue)) { 55*a1a3b679SAndreas Boehler $writer->startElement("{DAV:}remove"); 56*a1a3b679SAndreas Boehler $writer->write(['{DAV:}prop' => [$propertyName => $propertyValue]]); 57*a1a3b679SAndreas Boehler $writer->endElement(); 58*a1a3b679SAndreas Boehler } else { 59*a1a3b679SAndreas Boehler $writer->startElement("{DAV:}set"); 60*a1a3b679SAndreas Boehler $writer->write(['{DAV:}prop' => [$propertyName => $propertyValue]]); 61*a1a3b679SAndreas Boehler $writer->endElement(); 62*a1a3b679SAndreas Boehler } 63*a1a3b679SAndreas Boehler 64*a1a3b679SAndreas Boehler } 65*a1a3b679SAndreas Boehler 66*a1a3b679SAndreas Boehler } 67*a1a3b679SAndreas Boehler 68*a1a3b679SAndreas Boehler /** 69*a1a3b679SAndreas Boehler * The deserialize method is called during xml parsing. 70*a1a3b679SAndreas Boehler * 71*a1a3b679SAndreas Boehler * This method is called statictly, this is because in theory this method 72*a1a3b679SAndreas Boehler * may be used as a type of constructor, or factory method. 73*a1a3b679SAndreas Boehler * 74*a1a3b679SAndreas Boehler * Often you want to return an instance of the current class, but you are 75*a1a3b679SAndreas Boehler * free to return other data as well. 76*a1a3b679SAndreas Boehler * 77*a1a3b679SAndreas Boehler * You are responsible for advancing the reader to the next element. Not 78*a1a3b679SAndreas Boehler * doing anything will result in a never-ending loop. 79*a1a3b679SAndreas Boehler * 80*a1a3b679SAndreas Boehler * If you just want to skip parsing for this element altogether, you can 81*a1a3b679SAndreas Boehler * just call $reader->next(); 82*a1a3b679SAndreas Boehler * 83*a1a3b679SAndreas Boehler * $reader->parseInnerTree() will parse the entire sub-tree, and advance to 84*a1a3b679SAndreas Boehler * the next element. 85*a1a3b679SAndreas Boehler * 86*a1a3b679SAndreas Boehler * @param Reader $reader 87*a1a3b679SAndreas Boehler * @return mixed 88*a1a3b679SAndreas Boehler */ 89*a1a3b679SAndreas Boehler static function xmlDeserialize(Reader $reader) { 90*a1a3b679SAndreas Boehler 91*a1a3b679SAndreas Boehler $self = new self(); 92*a1a3b679SAndreas Boehler 93*a1a3b679SAndreas Boehler $elementMap = $reader->elementMap; 94*a1a3b679SAndreas Boehler $elementMap['{DAV:}prop'] = 'Sabre\DAV\Xml\Element\Prop'; 95*a1a3b679SAndreas Boehler $elementMap['{DAV:}set'] = 'Sabre\Xml\Element\KeyValue'; 96*a1a3b679SAndreas Boehler $elementMap['{DAV:}remove'] = 'Sabre\Xml\Element\KeyValue'; 97*a1a3b679SAndreas Boehler 98*a1a3b679SAndreas Boehler $elems = $reader->parseInnerTree($elementMap); 99*a1a3b679SAndreas Boehler 100*a1a3b679SAndreas Boehler foreach ($elems as $elem) { 101*a1a3b679SAndreas Boehler if ($elem['name'] === '{DAV:}set') { 102*a1a3b679SAndreas Boehler $self->properties = array_merge($self->properties, $elem['value']['{DAV:}prop']); 103*a1a3b679SAndreas Boehler } 104*a1a3b679SAndreas Boehler if ($elem['name'] === '{DAV:}remove') { 105*a1a3b679SAndreas Boehler 106*a1a3b679SAndreas Boehler // Ensuring there are no values. 107*a1a3b679SAndreas Boehler foreach ($elem['value']['{DAV:}prop'] as $remove => $value) { 108*a1a3b679SAndreas Boehler $self->properties[$remove] = null; 109*a1a3b679SAndreas Boehler } 110*a1a3b679SAndreas Boehler 111*a1a3b679SAndreas Boehler } 112*a1a3b679SAndreas Boehler } 113*a1a3b679SAndreas Boehler 114*a1a3b679SAndreas Boehler return $self; 115*a1a3b679SAndreas Boehler 116*a1a3b679SAndreas Boehler } 117*a1a3b679SAndreas Boehler 118*a1a3b679SAndreas Boehler} 119