. * */ 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 \Sabre\VObject\Property\Text { /** * 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)); } }