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