ShareAccess.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Property;
  4. use Sabre\DAV\Exception\BadRequest;
  5. use Sabre\DAV\Sharing\Plugin as SharingPlugin;
  6. use Sabre\Xml\Element;
  7. use Sabre\Xml\Reader;
  8. use Sabre\Xml\Writer;
  9. /**
  10. * This class represents the {DAV:}share-access property.
  11. *
  12. * This property is defined here:
  13. * https://tools.ietf.org/html/draft-pot-webdav-resource-sharing-03#section-4.4.1
  14. *
  15. * This property is used to indicate if a resource is a shared resource, and
  16. * whether the instance of the shared resource is the original instance, or
  17. * an instance belonging to a sharee.
  18. *
  19. * @copyright Copyright (C) fruux GmbH (https://fruux.com/).
  20. * @author Evert Pot (http://evertpot.com/)
  21. * @license http://sabre.io/license/ Modified BSD License
  22. */
  23. class ShareAccess implements Element
  24. {
  25. /**
  26. * Either SHARED or SHAREDOWNER.
  27. *
  28. * @var int
  29. */
  30. protected $value;
  31. /**
  32. * Creates the property.
  33. *
  34. * The constructor value must be one of the
  35. * \Sabre\DAV\Sharing\Plugin::ACCESS_ constants.
  36. *
  37. * @param int $shareAccess
  38. */
  39. public function __construct($shareAccess)
  40. {
  41. $this->value = $shareAccess;
  42. }
  43. /**
  44. * Returns the current value.
  45. *
  46. * @return int
  47. */
  48. public function getValue()
  49. {
  50. return $this->value;
  51. }
  52. /**
  53. * The xmlSerialize method is called during xml writing.
  54. *
  55. * Use the $writer argument to write its own xml serialization.
  56. *
  57. * An important note: do _not_ create a parent element. Any element
  58. * implementing XmlSerializable should only ever write what's considered
  59. * its 'inner xml'.
  60. *
  61. * The parent of the current element is responsible for writing a
  62. * containing element.
  63. *
  64. * This allows serializers to be re-used for different element names.
  65. *
  66. * If you are opening new elements, you must also close them again.
  67. */
  68. public function xmlSerialize(Writer $writer)
  69. {
  70. switch ($this->value) {
  71. case SharingPlugin::ACCESS_NOTSHARED:
  72. $writer->writeElement('{DAV:}not-shared');
  73. break;
  74. case SharingPlugin::ACCESS_SHAREDOWNER:
  75. $writer->writeElement('{DAV:}shared-owner');
  76. break;
  77. case SharingPlugin::ACCESS_READ:
  78. $writer->writeElement('{DAV:}read');
  79. break;
  80. case SharingPlugin::ACCESS_READWRITE:
  81. $writer->writeElement('{DAV:}read-write');
  82. break;
  83. case SharingPlugin::ACCESS_NOACCESS:
  84. $writer->writeElement('{DAV:}no-access');
  85. break;
  86. }
  87. }
  88. /**
  89. * The deserialize method is called during xml parsing.
  90. *
  91. * This method is called statically, this is because in theory this method
  92. * may be used as a type of constructor, or factory method.
  93. *
  94. * Often you want to return an instance of the current class, but you are
  95. * free to return other data as well.
  96. *
  97. * You are responsible for advancing the reader to the next element. Not
  98. * doing anything will result in a never-ending loop.
  99. *
  100. * If you just want to skip parsing for this element altogether, you can
  101. * just call $reader->next();
  102. *
  103. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  104. * the next element.
  105. *
  106. * @return mixed
  107. */
  108. public static function xmlDeserialize(Reader $reader)
  109. {
  110. $elems = $reader->parseInnerTree();
  111. foreach ($elems as $elem) {
  112. switch ($elem['name']) {
  113. case '{DAV:}not-shared':
  114. return new self(SharingPlugin::ACCESS_NOTSHARED);
  115. case '{DAV:}shared-owner':
  116. return new self(SharingPlugin::ACCESS_SHAREDOWNER);
  117. case '{DAV:}read':
  118. return new self(SharingPlugin::ACCESS_READ);
  119. case '{DAV:}read-write':
  120. return new self(SharingPlugin::ACCESS_READWRITE);
  121. case '{DAV:}no-access':
  122. return new self(SharingPlugin::ACCESS_NOACCESS);
  123. }
  124. }
  125. throw new BadRequest('Invalid value for {DAV:}share-access element');
  126. }
  127. }