aboutsummaryrefslogtreecommitdiffstats
path: root/lib/public/Http/WellKnown/JrdResponse.php
blob: 077b58dc1d9dbad439b1179b65fb8fa8001fe1b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php

declare(strict_types=1);

/**
 * @copyright 2020 Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @author Christoph Wurst <christoph@winzerhof-wurst.at>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 *
 */
namespace OCP\Http\WellKnown;

use OCP\AppFramework\Http\JSONResponse;
use OCP\AppFramework\Http\Response;
use function array_filter;

/**
 * A JSON Document Format (JDF) response to a well-known request
 *
 * @ref https://tools.ietf.org/html/rfc6415#appendix-A
 * @ref https://tools.ietf.org/html/rfc7033#section-4.4
 *
 * @since 21.0.0
 */
final class JrdResponse implements IResponse {
	/** @var string */
	private $subject;

	/** @var string|null */
	private $expires;

	/** @var string[] */
	private $aliases = [];

	/** @var (string|null)[] */
	private $properties = [];

	/** @var mixed[] */
	private $links;

	/**
	 * @param string $subject https://tools.ietf.org/html/rfc7033#section-4.4.1
	 *
	 * @since 21.0.0
	 */
	public function __construct(string $subject) {
		$this->subject = $subject;
	}

	/**
	 * @param string $expires
	 *
	 * @return $this
	 *
	 * @since 21.0.0
	 */
	public function setExpires(string $expires): self {
		$this->expires = $expires;

		return $this;
	}

	/**
	 * Add an alias
	 *
	 * @ref https://tools.ietf.org/html/rfc7033#section-4.4.2
	 *
	 * @param string $alias
	 *
	 * @return $this
	 *
	 * @since 21.0.0
	 */
	public function addAlias(string $alias): self {
		$this->aliases[] = $alias;

		return $this;
	}

	/**
	 * Add a property
	 *
	 * @ref https://tools.ietf.org/html/rfc7033#section-4.4.3
	 *
	 * @param string $property
	 * @param string|null $value
	 *
	 * @return $this
	 *
	 * @since 21.0.0
	 */
	public function addProperty(string $property, ?string $value): self {
		$this->properties[$property] = $value;

		return $this;
	}

	/**
	 * Add a link
	 *
	 * @ref https://tools.ietf.org/html/rfc7033#section-8.4
	 *
	 * @param string $rel https://tools.ietf.org/html/rfc7033#section-4.4.4.1
	 * @param string|null $type https://tools.ietf.org/html/rfc7033#section-4.4.4.2
	 * @param string|null $href https://tools.ietf.org/html/rfc7033#section-4.4.4.3
	 * @param string[]|null $titles https://tools.ietf.org/html/rfc7033#section-4.4.4.4
	 * @param string|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
	 *
	 * @psalm-param array<string,(string|null)>|null $properties https://tools.ietf.org/html/rfc7033#section-4.4.4.5
	 *
	 * @return JrdResponse
	 * @since 21.0.0
	 */
	public function addLink(string $rel,
		?string $type,
		?string $href,
		?array $titles = [],
		?array $properties = []): self {
		$this->links[] = array_filter([
			'rel' => $rel,
			'type' => $type,
			'href' => $href,
			'titles' => $titles,
			'properties' => $properties,
		]);

		return $this;
	}

	/**
	 * @since 21.0.0
	 */
	public function toHttpResponse(): Response {
		return new JSONResponse(array_filter([
			'subject' => $this->subject,
			'expires' => $this->expires,
			'aliases' => $this->aliases,
			'properties' => $this->properties,
			'links' => $this->links,
		]));
	}

	/**
	 * Does this response have any data attached to it?
	 *
	 * @since 21.0.0
	 */
	public function isEmpty(): bool {
		return $this->expires === null
			&& empty($this->aliases)
			&& empty($this->properties)
			&& empty($this->links);
	}
}