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\Model\Circle;
35
36/**
37 * Class CircleProbe
38 *
39 * @package OCA\Circles\Model\Probes
40 */
41class CircleProbe extends MemberProbe {
42
43
44	/** @var int */
45	private $include = 0;
46
47	/** @var int */
48	private $filter = Circle::CFG_SINGLE;
49
50	/** @var bool */
51	private $includeNonVisible = false;
52
53
54	/**
55	 * CircleProbe constructor.
56	 */
57	public function __construct() {
58	}
59
60
61	/**
62	 * @param bool $include
63	 *
64	 * @return $this
65	 */
66	public function includePersonalCircles(bool $include = true): self {
67		$this->include |= Circle::CFG_PERSONAL;
68		if (!$include) {
69			$this->include -= Circle::CFG_PERSONAL;
70		}
71
72		return $this;
73	}
74
75	/**
76	 * @param bool $include
77	 *
78	 * @return $this
79	 */
80	public function includeSingleCircles(bool $include = true): self {
81		$this->include |= Circle::CFG_SINGLE;
82		if (!$include) {
83			$this->include -= Circle::CFG_SINGLE;
84		}
85
86		return $this;
87	}
88
89	/**
90	 * @param bool $include
91	 *
92	 * @return $this
93	 */
94	public function includeSystemCircles(bool $include = true): self {
95		$this->include |= Circle::CFG_SYSTEM;
96		if (!$include) {
97			$this->include -= Circle::CFG_SYSTEM;
98		}
99
100		return $this;
101	}
102
103	/**
104	 * @param bool $include
105	 *
106	 * @return $this
107	 */
108	public function includeHiddenCircles(bool $include = true): self {
109		$this->include |= Circle::CFG_HIDDEN;
110		if (!$include) {
111			$this->include -= Circle::CFG_HIDDEN;
112		}
113
114		return $this;
115	}
116
117	/**
118	 * @param bool $include
119	 *
120	 * @return $this
121	 */
122	public function includeBackendCircles(bool $include = true): self {
123		$this->include |= Circle::CFG_BACKEND;
124		if (!$include) {
125			$this->include -= Circle::CFG_BACKEND;
126		}
127
128		return $this;
129	}
130
131	/**
132	 * @param bool $include
133	 *
134	 * @return $this
135	 */
136	public function includeNonVisibleCircles(bool $include = true): self {
137		$this->includeNonVisible = $include;
138
139		return $this;
140	}
141
142	/**
143	 * @return bool
144	 */
145	public function nonVisibleCirclesIncluded(): bool {
146		return $this->includeNonVisible;
147	}
148
149
150	/**
151	 * @return int
152	 */
153	public function included(): int {
154		return $this->include;
155	}
156
157	/**
158	 * @param int $config
159	 *
160	 * @return bool
161	 */
162	public function isIncluded(int $config): bool {
163		return (($this->included() & $config) !== 0);
164	}
165
166
167	/**
168	 * @param bool $filter
169	 *
170	 * @return $this
171	 */
172	public function filterPersonalCircles(bool $filter = true): self {
173		$this->filter |= Circle::CFG_PERSONAL;
174		if (!$filter) {
175			$this->filter -= Circle::CFG_PERSONAL;
176		}
177
178		return $this;
179	}
180
181	/**
182	 * @param bool $filter
183	 *
184	 * @return $this
185	 */
186	public function filterSingleCircles(bool $filter = true): self {
187		$this->filter |= Circle::CFG_SINGLE;
188		if (!$filter) {
189			$this->filter -= Circle::CFG_SINGLE;
190		}
191
192		return $this;
193	}
194
195	/**
196	 * @param bool $filter
197	 *
198	 * @return $this
199	 */
200	public function filterSystemCircles(bool $filter = true): self {
201		$this->filter |= Circle::CFG_SYSTEM;
202		if (!$filter) {
203			$this->filter -= Circle::CFG_SYSTEM;
204		}
205
206		return $this;
207	}
208
209	/**
210	 * @param bool $filter
211	 *
212	 * @return $this
213	 */
214	public function filterHiddenCircles(bool $filter = true): self {
215		$this->filter |= Circle::CFG_HIDDEN;
216		if (!$filter) {
217			$this->filter -= Circle::CFG_HIDDEN;
218		}
219
220		return $this;
221	}
222
223	/**
224	 * @param bool $filter
225	 *
226	 * @return $this
227	 */
228	public function filterBackendCircles(bool $filter = true): self {
229		$this->filter |= Circle::CFG_BACKEND;
230		if (!$filter) {
231			$this->filter -= Circle::CFG_BACKEND;
232		}
233
234		return $this;
235	}
236
237
238	/**
239	 * @param int $config
240	 * @param bool $filter
241	 *
242	 * @return $this
243	 */
244	public function filterConfig(int $config, bool $filter = true): self {
245		$this->filter |= $config;
246		if (!$filter) {
247			$this->filter -= $config;
248		}
249
250		return $this;
251	}
252
253
254	/**
255	 * @return int
256	 */
257	public function filtered(): int {
258		return $this->filter;
259	}
260
261	/**
262	 * @param int $config
263	 *
264	 * @return bool
265	 */
266	public function isFiltered(int $config): bool {
267		return (($this->filtered() & $config) !== 0);
268	}
269
270
271	/**
272	 * @return array
273	 */
274	public function getAsOptions(): array {
275		return array_merge(
276			[
277				'included' => $this->included(),
278				'includeHiddenCircles' => $this->isIncluded(Circle::CFG_HIDDEN),
279				'includeSingleCircles' => $this->isIncluded(Circle::CFG_SINGLE),
280				'includeBackendCircles' => $this->isIncluded(Circle::CFG_BACKEND),
281				'includeSystemCircles' => $this->isIncluded(Circle::CFG_SYSTEM),
282				'includePersonalCircles' => $this->isIncluded(Circle::CFG_PERSONAL),
283				'includeNonVisibleCircles' => $this->nonVisibleCirclesIncluded(),
284				'filtered' => $this->included(),
285				'filterHiddenCircles' => $this->isIncluded(Circle::CFG_HIDDEN),
286				'filterSingleCircles' => $this->isIncluded(Circle::CFG_SINGLE),
287				'filterBackendCircles' => $this->isIncluded(Circle::CFG_BACKEND),
288				'filterSystemCircles' => $this->isIncluded(Circle::CFG_SYSTEM),
289				'filterPersonalCircles' => $this->isIncluded(Circle::CFG_PERSONAL),
290			],
291			parent::getAsOptions()
292		);
293	}
294
295
296	/**
297	 * @return array
298	 */
299	public function JsonSerialize(): array {
300		return $this->getAsOptions();
301	}
302}
303