1
0

JrdResponse.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\Http\WellKnown;
  25. use OCP\AppFramework\Http\JSONResponse;
  26. use OCP\AppFramework\Http\Response;
  27. use function array_filter;
  28. /**
  29. * A JSON Document Format (JDF) response to a well-known request
  30. *
  31. * @ref https://tools.ietf.org/html/rfc6415#appendix-A
  32. * @ref https://tools.ietf.org/html/rfc7033#section-4.4
  33. *
  34. * @since 21.0.0
  35. */
  36. final class JrdResponse implements IResponse {
  37. /** @var string */
  38. private $subject;
  39. /** @var string|null */
  40. private $expires;
  41. /** @var string[] */
  42. private $aliases = [];
  43. /** @var (string|null)[] */
  44. private $properties = [];
  45. /** @var mixed[] */
  46. private $links;
  47. /**
  48. * @param string $subject https://tools.ietf.org/html/rfc7033#section-4.4.1
  49. *
  50. * @since 21.0.0
  51. */
  52. public function __construct(string $subject) {
  53. $this->subject = $subject;
  54. }
  55. /**
  56. * @param string $expires
  57. *
  58. * @return $this
  59. *
  60. * @since 21.0.0
  61. */
  62. public function setExpires(string $expires): self {
  63. $this->expires = $expires;
  64. return $this;
  65. }
  66. /**
  67. * Add an alias
  68. *
  69. * @ref https://tools.ietf.org/html/rfc7033#section-4.4.2
  70. *
  71. * @param string $alias
  72. *
  73. * @return $this
  74. *
  75. * @since 21.0.0
  76. */
  77. public function addAlias(string $alias): self {
  78. $this->aliases[] = $alias;
  79. return $this;
  80. }
  81. /**
  82. * Add a property
  83. *
  84. * @ref https://tools.ietf.org/html/rfc7033#section-4.4.3
  85. *
  86. * @param string $property
  87. * @param string|null $value
  88. *
  89. * @return $this
  90. *
  91. * @since 21.0.0
  92. */
  93. public function addProperty(string $property, ?string $value): self {
  94. $this->properties[$property] = $value;
  95. return $this;
  96. }
  97. /**
  98. * Add a link
  99. *
  100. * @ref https://tools.ietf.org/html/rfc7033#section-8.4
  101. *
  102. * @param string $rel https://tools.ietf.org/html/rfc7033#section-4.4.4.1
  103. * @param string|null $type https://tools.ietf.org/html/rfc7033#section-4.4.4.2
  104. * @param string|null $href https://tools.ietf.org/html/rfc7033#section-4.4.4.3
  105. * @param string[]|null $titles https://tools.ietf.org/html/rfc7033#section-4.4.4.4
  106. * @param string|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
  107. *
  108. * @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
  109. *
  110. * @return JrdResponse
  111. * @since 21.0.0
  112. */
  113. public function addLink(string $rel,
  114. ?string $type,
  115. ?string $href,
  116. ?array $titles = [],
  117. ?array $properties = []): self {
  118. $this->links[] = array_filter([
  119. 'rel' => $rel,
  120. 'type' => $type,
  121. 'href' => $href,
  122. 'titles' => $titles,
  123. 'properties' => $properties,
  124. ]);
  125. return $this;
  126. }
  127. /**
  128. * @since 21.0.0
  129. */
  130. public function toHttpResponse(): Response {
  131. return new JSONResponse(array_filter([
  132. 'subject' => $this->subject,
  133. 'expires' => $this->expires,
  134. 'aliases' => $this->aliases,
  135. 'properties' => $this->properties,
  136. 'links' => $this->links,
  137. ]));
  138. }
  139. /**
  140. * Does this response have any data attached to it?
  141. *
  142. * @since 21.0.0
  143. */
  144. public function isEmpty(): bool {
  145. return $this->expires === null
  146. && empty($this->aliases)
  147. && empty($this->properties)
  148. && empty($this->links);
  149. }
  150. }