Completed
Push — master ( bdf7f5...6ddda3 )
by Joas
50:06 queued 20:46
created

SearchResult::hasResult()   B

Complexity

Conditions 5
Paths 5

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 5
nop 2
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Arthur Schiwon <[email protected]>
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 *
7
 * @license GNU AGPL version 3 or any later version
8
 *
9
 * This program is free software: you can redistribute it and/or modify
10
 * it under the terms of the GNU Affero General Public License as
11
 * published by the Free Software Foundation, either version 3 of the
12
 * License, or (at your option) any later version.
13
 *
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU Affero General Public License for more details.
18
 *
19
 * You should have received a copy of the GNU Affero General Public License
20
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 *
22
 */
23
24
namespace OC\Collaboration\Collaborators;
25
26
27
use OCP\Collaboration\Collaborators\ISearchResult;
28
use OCP\Collaboration\Collaborators\SearchResultType;
29
30
class SearchResult implements ISearchResult {
31
32
	protected $result = [
33
		'exact' => [],
34
	];
35
36
	protected $exactIdMatches = [];
37
38
	public function addResultSet(SearchResultType $type, array $matches, array $exactMatches = null) {
39
		$type = $type->getLabel();
40
		if(!isset($this->result[$type])) {
41
			$this->result[$type] = [];
42
			$this->result['exact'][$type] = [];
43
		}
44
45
		$this->result[$type] = array_merge($this->result[$type], $matches);
46
		if(is_array($exactMatches)) {
47
			$this->result['exact'][$type] = array_merge($this->result['exact'][$type], $exactMatches);
48
		}
49
	}
50
51
	public function markExactIdMatch(SearchResultType $type) {
52
		$this->exactIdMatches[$type->getLabel()] = 1;
53
	}
54
55
	public function hasExactIdMatch(SearchResultType $type) {
56
		return isset($this->exactIdMatches[$type->getLabel()]);
57
	}
58
59
	public function hasResult(SearchResultType $type, $collaboratorId) {
60
		$type = $type->getLabel();
61
		if(!isset($this->result[$type])) {
62
			return false;
63
		}
64
65
		$resultArrays = [$this->result['exact'][$type], $this->result[$type]];
66
		foreach($resultArrays as $resultArray) {
67
			foreach ($resultArray as $result) {
68
				if ($result['value']['shareWith'] === $collaboratorId) {
69
					return true;
70
				}
71
			}
72
		}
73
74
		return false;
75
	}
76
77
	public function asArray() {
78
		return $this->result;
79
	}
80
81
	public function unsetResult(SearchResultType $type) {
82
		$type = $type->getLabel();
83
		$this->result[$type] = [];
84
		if(isset($this->result['exact'][$type])) {
85
			$this->result['exact'][$type] = [];
86
		}
87
	}
88
}
89