GetLastModified.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\DAV\Xml\Property;
  4. use DateTime;
  5. use DateTimeZone;
  6. use Sabre\HTTP;
  7. use Sabre\Xml\Element;
  8. use Sabre\Xml\Reader;
  9. use Sabre\Xml\Writer;
  10. /**
  11. * This property represents the {DAV:}getlastmodified property.
  12. *
  13. * Defined in:
  14. * http://tools.ietf.org/html/rfc4918#section-15.7
  15. *
  16. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  17. * @author Evert Pot (http://www.rooftopsolutions.nl/)
  18. * @license http://sabre.io/license/ Modified BSD License
  19. */
  20. class GetLastModified implements Element
  21. {
  22. /**
  23. * time.
  24. *
  25. * @var DateTime
  26. */
  27. public $time;
  28. /**
  29. * Constructor.
  30. *
  31. * @param int|DateTime $time
  32. */
  33. public function __construct($time)
  34. {
  35. if ($time instanceof DateTime) {
  36. $this->time = clone $time;
  37. } else {
  38. $this->time = new DateTime('@'.$time);
  39. }
  40. // Setting timezone to UTC
  41. $this->time->setTimezone(new DateTimeZone('UTC'));
  42. }
  43. /**
  44. * getTime.
  45. *
  46. * @return DateTime
  47. */
  48. public function getTime()
  49. {
  50. return $this->time;
  51. }
  52. /**
  53. * The serialize method is called during xml writing.
  54. *
  55. * It should use the $writer argument to encode this object into XML.
  56. *
  57. * Important note: it is not needed to create the parent element. The
  58. * parent element is already created, and we only have to worry about
  59. * attributes, child elements and text (if any).
  60. *
  61. * Important note 2: If you are writing any new elements, you are also
  62. * responsible for closing them.
  63. */
  64. public function xmlSerialize(Writer $writer)
  65. {
  66. $writer->write(
  67. HTTP\toDate($this->time)
  68. );
  69. }
  70. /**
  71. * The deserialize method is called during xml parsing.
  72. *
  73. * This method is called statically, this is because in theory this method
  74. * may be used as a type of constructor, or factory method.
  75. *
  76. * Often you want to return an instance of the current class, but you are
  77. * free to return other data as well.
  78. *
  79. * Important note 2: You are responsible for advancing the reader to the
  80. * next element. Not doing anything will result in a never-ending loop.
  81. *
  82. * If you just want to skip parsing for this element altogether, you can
  83. * just call $reader->next();
  84. *
  85. * $reader->parseInnerTree() will parse the entire sub-tree, and advance to
  86. * the next element.
  87. *
  88. * @return mixed
  89. */
  90. public static function xmlDeserialize(Reader $reader)
  91. {
  92. return new self(new DateTime($reader->parseInnerTree()));
  93. }
  94. }