1<?php
2
3declare(strict_types=1);
4
5
6/**
7 * Circles - Bring cloud-users closer together.
8 *
9 * This file is licensed under the Affero General Public License version 3 or
10 * later. See the COPYING file.
11 *
12 * @author Maxence Lange <maxence@artificial-owl.com>
13 * @copyright 2021
14 * @license GNU AGPL version 3 or any later version
15 *
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU Affero General Public License as
18 * published by the Free Software Foundation, either version 3 of the
19 * License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU Affero General Public License for more details.
25 *
26 * You should have received a copy of the GNU Affero General Public License
27 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28 *
29 */
30
31
32namespace OCA\Circles\Model\Probes;
33
34use OCA\Circles\IQueryProbe;
35use OCA\Circles\Model\Circle;
36use OCA\Circles\Model\Federated\RemoteInstance;
37use OCA\Circles\Model\Member;
38
39/**
40 * Class BasicProbe
41 *
42 * @package OCA\Circles\Model\Probes
43 */
44class BasicProbe implements IQueryProbe {
45	public const DETAILS_NONE = 0;
46	public const DETAILS_ALL = 64;
47
48
49	/** @var int */
50	private $itemsOffset = 0;
51
52	/** @var int */
53	private $itemsLimit = -1;
54
55	/** @var int */
56	private $details = 0;
57
58	/** @var Circle */
59	private $filterCircle;
60
61	/** @var Member */
62	private $filterMember;
63
64	/** @var RemoteInstance */
65	private $filterRemoteInstance;
66
67	/** @var array */
68	private $options = [];
69
70
71	/**
72	 * @param int $itemsOffset
73	 *
74	 * @return BasicProbe
75	 */
76	public function setItemsOffset(int $itemsOffset): self {
77		$this->itemsOffset = $itemsOffset;
78
79		return $this;
80	}
81
82	/**
83	 * @return int
84	 */
85	public function getItemsOffset(): int {
86		return $this->itemsOffset;
87	}
88
89
90	/**
91	 * @param int $itemsLimit
92	 *
93	 * @return BasicProbe
94	 */
95	public function setItemsLimit(int $itemsLimit): self {
96		$this->itemsLimit = $itemsLimit;
97
98		return $this;
99	}
100
101	/**
102	 * @return int
103	 */
104	public function getItemsLimit(): int {
105		return $this->itemsLimit;
106	}
107
108
109	/**
110	 * @param int $details
111	 *
112	 * @return $this
113	 */
114	public function setDetails(int $details): self {
115		$this->details = $details;
116	}
117
118	/**
119	 * @return int
120	 */
121	public function getDetails(): int {
122		return $this->details;
123	}
124
125
126	/**
127	 * @param int $details
128	 *
129	 * @return bool
130	 */
131	public function showDetails(int $details): bool {
132		return (($this->getDetails() & $details) !== 0);
133	}
134
135
136	/**
137	 * @param Circle $filterCircle
138	 *
139	 * @return CircleProbe
140	 */
141	public function setFilterCircle(Circle $filterCircle): self {
142		$this->filterCircle = $filterCircle;
143
144		return $this;
145	}
146
147	/**
148	 * @return Circle
149	 */
150	public function getFilterCircle(): Circle {
151		return $this->filterCircle;
152	}
153
154	/**
155	 * @return bool
156	 */
157	public function hasFilterCircle(): bool {
158		return !is_null($this->filterCircle);
159	}
160
161
162	/**
163	 * @param Member $filterMember
164	 *
165	 * @return CircleProbe
166	 */
167	public function setFilterMember(Member $filterMember): self {
168		$this->filterMember = $filterMember;
169
170		return $this;
171	}
172
173	/**
174	 * @return Member
175	 */
176	public function getFilterMember(): Member {
177		return $this->filterMember;
178	}
179
180	/**
181	 * @return bool
182	 */
183	public function hasFilterMember(): bool {
184		return !is_null($this->filterMember);
185	}
186
187
188	/**
189	 * @param RemoteInstance $filterRemoteInstance
190	 *
191	 * @return CircleProbe
192	 */
193	public function setFilterRemoteInstance(RemoteInstance $filterRemoteInstance): self {
194		$this->filterRemoteInstance = $filterRemoteInstance;
195
196		return $this;
197	}
198
199	/**
200	 * @return RemoteInstance
201	 */
202	public function getFilterRemoteInstance(): RemoteInstance {
203		return $this->filterRemoteInstance;
204	}
205
206	/**
207	 * @return bool
208	 */
209	public function hasFilterRemoteInstance(): bool {
210		return !is_null($this->filterRemoteInstance);
211	}
212
213
214	/**
215	 * @param string $key
216	 * @param string $value
217	 *
218	 * @return $this
219	 */
220	public function addOption(string $key, string $value): self {
221		$this->options[$key] = $value;
222
223		return $this;
224	}
225
226	/**
227	 * @param string $key
228	 * @param int $value
229	 *
230	 * @return $this
231	 */
232	public function addOptionInt(string $key, int $value): self {
233		$this->options[$key] = $value;
234
235		return $this;
236	}
237
238	/**
239	 * @param string $key
240	 * @param bool $value
241	 *
242	 * @return $this
243	 */
244	public function addOptionBool(string $key, bool $value): self {
245		$this->options[$key] = $value;
246
247		return $this;
248	}
249
250	/**
251	 * @return array
252	 */
253	public function getAsOptions(): array {
254		return array_merge(
255			$this->options,
256			[
257				'offset' => $this->getItemsOffset(),
258				'limit' => $this->getItemsLimit(),
259				'details' => $this->getDetails(),
260				'detailsAll' => ($this->getDetails() === self::DETAILS_ALL)
261			]
262		);
263	}
264
265
266	/**
267	 * @return array
268	 */
269	public function JsonSerialize(): array {
270		return $this->getAsOptions();
271	}
272}
273