1<?php
2declare(strict_types=1);
3
4
5/**
6 * Some tools for myself.
7 *
8 * This file is licensed under the Affero General Public License version 3 or
9 * later. See the COPYING file.
10 *
11 * @author Maxence Lange <maxence@artificial-owl.com>
12 * @copyright 2020, Maxence Lange <maxence@artificial-owl.com>
13 * @license GNU AGPL version 3 or any later version
14 *
15 * This program is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Affero General Public License as
17 * published by the Free Software Foundation, either version 3 of the
18 * License, or (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23 * GNU Affero General Public License for more details.
24 *
25 * You should have received a copy of the GNU Affero General Public License
26 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
27 *
28 */
29
30
31namespace ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc21;
32
33
34use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
35use JsonSerializable;
36
37
38/**
39 * Class NC21Webfinger
40 *
41 * @package ArtificialOwl\MySmallPhpTools\Model\Nextcloud\nc21
42 */
43class NC21Webfinger implements JsonSerializable {
44
45
46	use TArrayTools;
47
48
49	/** @var string */
50	private $subject = '';
51
52	/** @var array */
53	private $aliases = [];
54
55	/** @var array */
56	private $properties = [];
57
58	/** @var NC21WellKnownLink[] */
59	private $links = [];
60
61
62	/**
63	 * NC21Webfinger constructor.
64	 *
65	 * @param array $json
66	 */
67	public function __construct(array $json = []) {
68		$this->setSubject($this->get('subject', $json));
69		$this->setAliases($this->getArray('subject', $json));
70		$this->setProperties($this->getArray('properties', $json));
71
72		foreach ($this->getArray('links', $json) as $link) {
73			$this->addLink(new NC21WellKnownLink($link));
74		}
75	}
76
77
78	/**
79	 * @return string
80	 */
81	public function getSubject(): string {
82		return $this->subject;
83	}
84
85	/**
86	 * @param string $subject
87	 *
88	 * @return self
89	 */
90	public function setSubject(string $subject): self {
91		$this->subject = $subject;
92
93		return $this;
94	}
95
96
97	/**
98	 * @return array
99	 */
100	public function getAliases(): array {
101		return $this->aliases;
102	}
103
104	/**
105	 * @param array $aliases
106	 *
107	 * @return self
108	 */
109	public function setAliases(array $aliases): self {
110		$this->aliases = $aliases;
111
112		return $this;
113	}
114
115
116	/**
117	 * @return array
118	 */
119	public function getProperties(): array {
120		return $this->properties;
121	}
122
123	/**
124	 * @param array $properties
125	 *
126	 * @return self
127	 */
128	public function setProperties(array $properties): self {
129		$this->properties = $properties;
130
131		return $this;
132	}
133
134	/**
135	 * @param string $key
136	 *
137	 * @return string
138	 */
139	public function getProperty(string $key): string {
140		return $this->get($key, $this->properties);
141	}
142
143
144	/**
145	 * @return NC21WellKnownLink[]
146	 */
147	public function getLinks(): array {
148		return $this->links;
149	}
150
151	/**
152	 * @param NC21WellKnownLink[] $links
153	 *
154	 * @return self
155	 */
156	public function setLinks(array $links): self {
157		$this->links = $links;
158
159		return $this;
160	}
161
162	public function addLink(NC21WellKnownLink $link): self {
163		$this->links[] = $link;
164
165		return $this;
166	}
167
168	/**
169	 * @return array
170	 */
171	public function jsonSerialize(): array {
172		return array_filter(
173			[
174				'subject'    => $this->getSubject(),
175				'aliases'    => $this->getAliases(),
176				'properties' => $this->getProperties(),
177				'links'      => $this->getLinks()
178			]
179		);
180	}
181
182}
183
184