Blame view

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