JrdResponse.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCP\Http\WellKnown;
  8. use OCP\AppFramework\Http\JSONResponse;
  9. use OCP\AppFramework\Http\Response;
  10. use function array_filter;
  11. /**
  12. * A JSON Document Format (JDF) response to a well-known request
  13. *
  14. * @ref https://tools.ietf.org/html/rfc6415#appendix-A
  15. * @ref https://tools.ietf.org/html/rfc7033#section-4.4
  16. *
  17. * @since 21.0.0
  18. */
  19. final class JrdResponse implements IResponse {
  20. /** @var string */
  21. private $subject;
  22. /** @var string|null */
  23. private $expires;
  24. /** @var string[] */
  25. private $aliases = [];
  26. /** @var (string|null)[] */
  27. private $properties = [];
  28. /** @var mixed[] */
  29. private $links;
  30. /**
  31. * @param string $subject https://tools.ietf.org/html/rfc7033#section-4.4.1
  32. *
  33. * @since 21.0.0
  34. */
  35. public function __construct(string $subject) {
  36. $this->subject = $subject;
  37. }
  38. /**
  39. * @param string $expires
  40. *
  41. * @return $this
  42. *
  43. * @since 21.0.0
  44. */
  45. public function setExpires(string $expires): self {
  46. $this->expires = $expires;
  47. return $this;
  48. }
  49. /**
  50. * Add an alias
  51. *
  52. * @ref https://tools.ietf.org/html/rfc7033#section-4.4.2
  53. *
  54. * @param string $alias
  55. *
  56. * @return $this
  57. *
  58. * @since 21.0.0
  59. */
  60. public function addAlias(string $alias): self {
  61. $this->aliases[] = $alias;
  62. return $this;
  63. }
  64. /**
  65. * Add a property
  66. *
  67. * @ref https://tools.ietf.org/html/rfc7033#section-4.4.3
  68. *
  69. * @param string $property
  70. * @param string|null $value
  71. *
  72. * @return $this
  73. *
  74. * @since 21.0.0
  75. */
  76. public function addProperty(string $property, ?string $value): self {
  77. $this->properties[$property] = $value;
  78. return $this;
  79. }
  80. /**
  81. * Add a link
  82. *
  83. * @ref https://tools.ietf.org/html/rfc7033#section-8.4
  84. *
  85. * @param string $rel https://tools.ietf.org/html/rfc7033#section-4.4.4.1
  86. * @param string|null $type https://tools.ietf.org/html/rfc7033#section-4.4.4.2
  87. * @param string|null $href https://tools.ietf.org/html/rfc7033#section-4.4.4.3
  88. * @param string[]|null $titles https://tools.ietf.org/html/rfc7033#section-4.4.4.4
  89. * @param string|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
  90. *
  91. * @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
  92. *
  93. * @return JrdResponse
  94. * @since 21.0.0
  95. */
  96. public function addLink(string $rel,
  97. ?string $type,
  98. ?string $href,
  99. ?array $titles = [],
  100. ?array $properties = []): self {
  101. $this->links[] = array_filter([
  102. 'rel' => $rel,
  103. 'type' => $type,
  104. 'href' => $href,
  105. 'titles' => $titles,
  106. 'properties' => $properties,
  107. ]);
  108. return $this;
  109. }
  110. /**
  111. * @since 21.0.0
  112. */
  113. public function toHttpResponse(): Response {
  114. return new JSONResponse(array_filter([
  115. 'subject' => $this->subject,
  116. 'expires' => $this->expires,
  117. 'aliases' => $this->aliases,
  118. 'properties' => $this->properties,
  119. 'links' => $this->links,
  120. ]));
  121. }
  122. /**
  123. * Does this response have any data attached to it?
  124. *
  125. * @since 21.0.0
  126. */
  127. public function isEmpty(): bool {
  128. return $this->expires === null
  129. && empty($this->aliases)
  130. && empty($this->properties)
  131. && empty($this->links);
  132. }
  133. }