1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* ownCloud - VObject Group Property |
4
|
|
|
* |
5
|
|
|
* @author Thomas Tanghus |
6
|
|
|
* @copyright 2013-2014 Thomas Tanghus ([email protected]) |
7
|
|
|
* |
8
|
|
|
* This library is free software; you can redistribute it and/or |
9
|
|
|
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE |
10
|
|
|
* License as published by the Free Software Foundation; either |
11
|
|
|
* version 3 of the License, or any later version. |
12
|
|
|
* |
13
|
|
|
* This library is distributed in the hope that it will be useful, |
14
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
15
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
16
|
|
|
* GNU AFFERO GENERAL PUBLIC LICENSE for more details. |
17
|
|
|
* |
18
|
|
|
* You should have received a copy of the GNU Affero General Public |
19
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>. |
20
|
|
|
* |
21
|
|
|
*/ |
22
|
|
|
|
23
|
|
|
namespace OCA\Contacts\VObject; |
24
|
|
|
|
25
|
|
|
use OC\VObject\CompoundProperty; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* This class adds convenience methods for the CATEGORIES property. |
29
|
|
|
* |
30
|
|
|
* NOTE: Group names are case-insensitive. |
31
|
|
|
*/ |
32
|
|
|
class GroupProperty extends \Sabre\VObject\Property\Text { |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Add a group. |
36
|
|
|
* |
37
|
|
|
* NOTE: We cannot just use add() as that method name is used in |
38
|
|
|
* \Sabre\VObject\Property |
39
|
|
|
* |
40
|
|
|
* @param string $name |
41
|
|
|
* @return bool |
42
|
|
|
*/ |
43
|
1 |
View Code Duplication |
public function addGroup($name) { |
|
|
|
|
44
|
1 |
|
$name = trim($name); |
45
|
1 |
|
if($this->hasGroup($name)) { |
46
|
|
|
return false; |
47
|
|
|
} |
48
|
1 |
|
$groups = $this->getParts(); |
49
|
|
|
// Remove empty elements |
50
|
1 |
|
$groups = array_filter($groups, 'strlen'); |
51
|
1 |
|
$groups[] = $name; |
52
|
1 |
|
$this->setParts($groups); |
53
|
1 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Remove an existing group. |
58
|
|
|
* |
59
|
|
|
* @param string $name |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
1 |
View Code Duplication |
public function removeGroup($name) { |
|
|
|
|
63
|
1 |
|
$name = trim($name); |
64
|
1 |
|
if(!$this->hasGroup($name)) { |
65
|
|
|
return false; |
66
|
|
|
} |
67
|
1 |
|
$groups = $this->getParts(); |
68
|
1 |
|
$groups = array_map('trim', $groups); |
69
|
1 |
|
array_splice($groups, $this->array_searchi($name, $groups), 1); |
70
|
1 |
|
$this->setParts($groups); |
71
|
1 |
|
return true; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Test it a group by that name exists. |
76
|
|
|
* |
77
|
|
|
* @param string $name |
78
|
|
|
* @return bool |
79
|
|
|
*/ |
80
|
1 |
|
public function hasGroup($name) { |
81
|
1 |
|
$name = trim($name); |
82
|
1 |
|
$groups = $this->getParts(); |
83
|
1 |
|
$groups = array_map('trim', $groups); |
84
|
1 |
|
return $this->in_arrayi($name, $groups); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Rename an existing group. |
89
|
|
|
* |
90
|
|
|
* @param string $from |
91
|
|
|
* @param string $to |
92
|
|
|
*/ |
93
|
1 |
|
public function renameGroup($from, $to) { |
94
|
1 |
|
$from = trim($from); |
95
|
1 |
|
$to = trim($to); |
96
|
1 |
|
if(!$this->hasGroup($from)) { |
97
|
|
|
return; |
98
|
|
|
} |
99
|
1 |
|
$groups = $this->getParts(); |
100
|
1 |
|
$groups = array_map('trim', $groups); |
101
|
1 |
|
$groups[$this->array_searchi($from, $groups)] = $to; |
102
|
1 |
|
$this->setParts($groups); |
103
|
1 |
|
} |
104
|
|
|
|
105
|
|
|
// case-insensitive in_array |
106
|
1 |
|
protected function in_arrayi($needle, $haystack) { |
|
|
|
|
107
|
1 |
|
if(!is_array($haystack)) { |
108
|
|
|
return false; |
109
|
|
|
} |
110
|
1 |
|
return in_array(strtolower($needle), array_map('strtolower', $haystack)); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
// case-insensitive array_search |
114
|
1 |
|
protected function array_searchi($needle, $haystack) { |
|
|
|
|
115
|
1 |
|
if(!is_array($haystack)) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
1 |
|
return array_search(strtolower($needle), array_map('strtolower', $haystack)); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.