PropPatch.php 3.5 KB

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