Blame view
sources/apps/contacts/lib/abstractpimcollection.php
3.86 KB
|
d1bafeea1
|
1 2 3 4 5 |
<?php /** * ownCloud - Collection class for PIM object * * @author Thomas Tanghus |
|
6d9380f96
|
6 |
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net) |
|
d1bafeea1
|
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
* * 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; /** |
|
6d9380f96
|
26 |
* Subclass this for PIM collections. |
|
d1bafeea1
|
27 |
*/ |
|
d1bafeea1
|
28 29 30 31 32 |
abstract class AbstractPIMCollection extends AbstractPIMObject implements \Iterator, \Countable, \ArrayAccess {
// Iterator properties
protected $objects = array();
|
|
6d9380f96
|
33 |
protected $current; |
|
d1bafeea1
|
34 35 36 |
/** * Returns a specific child node, referenced by its id |
|
d1bafeea1
|
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 |
* * @param string $id * @return IPIMObject */ public abstract function getChild($id); /** * Returns an array with all the child nodes * * @return IPIMObject[] */ public abstract function getChildren($limit = null, $offset = null); /** * Checks if a child-node with the specified id exists * * @param string $id * @return bool */ public abstract function childExists($id); /** * Add a child to the collection * * It's up to the implementations to "keep track" of the children. * * @param mixed $data * @return string ID of the newly added child */ public abstract function addChild($data); /** * Delete a child from the collection * * @param string $id * @return bool */ public abstract function deleteChild($id, $options = array()); |
|
6d9380f96
|
75 |
// Iterator methods |
|
d1bafeea1
|
76 |
|
|
6d9380f96
|
77 |
// NOTE: This method is reliant on sub class implementing count() |
|
d1bafeea1
|
78 |
public function rewind() {
|
|
6d9380f96
|
79 80 81 82 83 84 |
// Assure any internal counter's initialized
self::count();
if (count($this->objects) === 0) {
$this->getChildren();
}
$this->current = reset($this->objects);
|
|
d1bafeea1
|
85 86 87 |
}
public function next() {
|
|
6d9380f96
|
88 |
$this->current = next($this->objects); |
|
d1bafeea1
|
89 90 91 |
}
public function valid() {
|
|
6d9380f96
|
92 |
return $this->current ? array_key_exists($this->current->getId(), $this->objects) : false; |
|
d1bafeea1
|
93 94 95 |
}
public function current() {
|
|
6d9380f96
|
96 |
return $this->objects[$this->current->getId()]; |
|
d1bafeea1
|
97 98 99 100 101 102 103 |
}
/** Implementations can choose to return the current objects ID/UUID
* to be able to iterate over the collection with ID => Object pairs:
* foreach($collection as $id => $object) {}
*/
public function key() {
|
|
6d9380f96
|
104 |
return $this->current->getId(); |
|
d1bafeea1
|
105 106 107 108 109 110 111 |
} // Countable method. /** * For implementations using a backend where fetching all object at once * would give too much overhead, they can maintain an internal count value |
|
6d9380f96
|
112 |
* and fetch objects progressively. |
|
d1bafeea1
|
113 114 115 116 117 118 119 120 121 |
*/
public function count() {
return count($this->objects);
}
// ArrayAccess methods
public function offsetSet($offset, $value) {
if (is_null($offset)) {
|
|
6d9380f96
|
122 |
throw new \RunTimeException('You cannot add objects using array access');
|
|
d1bafeea1
|
123 |
} else {
|
|
6d9380f96
|
124 |
// TODO: Check if offset is set and update element. |
|
d1bafeea1
|
125 126 127 128 129 |
$this->objects[$offset] = $value;
}
}
public function offsetExists($offset) {
|
|
6d9380f96
|
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
if (isset($this->objects[$offset])) {
return true;
}
try {
$child = $this->getChild($offset);
} catch (\Exception $e) {
return false;
}
if ($child) {
$this->objects[$offset] = $child;
return true;
}
return false;
|
|
d1bafeea1
|
146 147 148 |
}
public function offsetUnset($offset) {
|
|
6d9380f96
|
149 |
$this->deleteChild($offset); |
|
d1bafeea1
|
150 151 152 153 |
unset($this->objects[$offset]);
}
public function offsetGet($offset) {
|
|
6d9380f96
|
154 155 156 |
if (isset($this->objects[$offset])) {
return $this->objects[$offset];
}
|
|
d1bafeea1
|
157 |
|
|
6d9380f96
|
158 |
$child = $this->getChild($offset); |
|
d1bafeea1
|
159 |
|
|
6d9380f96
|
160 161 162 163 |
if ($child) {
$this->objects[$offset] = $child;
return $child;
}
|
|
d1bafeea1
|
164 |
|
|
d1bafeea1
|
165 |
} |
|
d1bafeea1
|
166 167 |
} |