2013-03-10 12:40:11 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* ownCloud - Collection class for PIM object
|
|
|
|
*
|
|
|
|
* @author Thomas Tanghus
|
2014-01-26 00:40:22 +01:00
|
|
|
* @copyright 2012-2014 Thomas Tanghus (thomas@tanghus.net)
|
2013-03-10 12:40:11 +01:00
|
|
|
*
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
2014-03-08 16:22:51 +01:00
|
|
|
* Subclass this for PIM collections.
|
2013-03-10 12:40:11 +01:00
|
|
|
*/
|
2013-04-25 01:03:05 +02:00
|
|
|
abstract class AbstractPIMCollection extends AbstractPIMObject implements \Iterator, \Countable, \ArrayAccess {
|
2013-03-10 12:40:11 +01:00
|
|
|
|
|
|
|
// Iterator properties
|
|
|
|
|
|
|
|
protected $objects = array();
|
|
|
|
|
2014-03-10 15:14:36 +01:00
|
|
|
protected $current;
|
2013-03-10 12:40:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a specific child node, referenced by its id
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return IPIMObject
|
|
|
|
*/
|
2013-05-30 20:52:12 +02:00
|
|
|
public abstract function getChild($id);
|
2013-03-10 12:40:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns an array with all the child nodes
|
|
|
|
*
|
|
|
|
* @return IPIMObject[]
|
|
|
|
*/
|
2013-05-30 20:52:12 +02:00
|
|
|
public abstract function getChildren($limit = null, $offset = null);
|
2013-03-10 12:40:11 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if a child-node with the specified id exists
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-05-30 20:52:12 +02:00
|
|
|
public abstract function childExists($id);
|
2013-03-10 12:40:11 +01:00
|
|
|
|
2013-03-20 11:22:53 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2013-05-30 20:52:12 +02:00
|
|
|
public abstract function addChild($data);
|
2013-03-20 11:22:53 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a child from the collection
|
|
|
|
*
|
|
|
|
* @param string $id
|
|
|
|
* @return bool
|
|
|
|
*/
|
2013-09-03 19:23:11 +02:00
|
|
|
public abstract function deleteChild($id, $options = array());
|
2013-03-20 11:22:53 +01:00
|
|
|
|
2014-03-08 19:09:17 +01:00
|
|
|
// Iterator methods
|
2013-03-10 12:40:11 +01:00
|
|
|
|
2014-03-10 15:14:36 +01:00
|
|
|
// NOTE: This method is reliant on sub class implementing count()
|
2013-03-10 12:40:11 +01:00
|
|
|
public function rewind() {
|
2014-03-10 15:14:36 +01:00
|
|
|
// Assure any internal counter's initialized
|
|
|
|
self::count();
|
|
|
|
if (count($this->objects) === 0) {
|
|
|
|
$this->getChildren();
|
|
|
|
}
|
|
|
|
$this->current = reset($this->objects);
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function next() {
|
2014-03-10 15:14:36 +01:00
|
|
|
$this->current = next($this->objects);
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function valid() {
|
2014-03-10 15:14:36 +01:00
|
|
|
return $this->current ? array_key_exists($this->current->getId(), $this->objects) : false;
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function current() {
|
2014-03-10 15:14:36 +01:00
|
|
|
return $this->objects[$this->current->getId()];
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/** 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() {
|
2014-03-10 15:14:36 +01:00
|
|
|
return $this->current->getId();
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
2014-03-10 15:14:36 +01:00
|
|
|
* and fetch objects progressively.
|
2013-03-10 12:40:11 +01:00
|
|
|
*/
|
|
|
|
public function count() {
|
|
|
|
return count($this->objects);
|
|
|
|
}
|
|
|
|
|
|
|
|
// ArrayAccess methods
|
|
|
|
|
|
|
|
public function offsetSet($offset, $value) {
|
|
|
|
if (is_null($offset)) {
|
2014-03-10 15:14:36 +01:00
|
|
|
throw new \RunTimeException('You cannot add objects using array access');
|
2013-03-10 12:40:11 +01:00
|
|
|
} else {
|
2014-03-10 15:14:36 +01:00
|
|
|
// TODO: Check if offset is set and update element.
|
2013-03-10 12:40:11 +01:00
|
|
|
$this->objects[$offset] = $value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetExists($offset) {
|
2014-03-10 15:14:36 +01:00
|
|
|
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;
|
2013-03-10 12:40:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetUnset($offset) {
|
2014-03-10 15:14:36 +01:00
|
|
|
$this->deleteChild($offset);
|
2013-03-10 12:40:11 +01:00
|
|
|
unset($this->objects[$offset]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function offsetGet($offset) {
|
2014-03-10 15:14:36 +01:00
|
|
|
if (isset($this->objects[$offset])) {
|
|
|
|
return $this->objects[$offset];
|
|
|
|
}
|
2013-03-10 12:40:11 +01:00
|
|
|
|
2014-03-10 15:14:36 +01:00
|
|
|
$child = $this->getChild($offset);
|
2013-03-10 12:40:11 +01:00
|
|
|
|
2014-03-10 15:14:36 +01:00
|
|
|
if ($child) {
|
|
|
|
$this->objects[$offset] = $child;
|
|
|
|
return $child;
|
|
|
|
}
|
2013-03-10 12:40:11 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2013-03-17 21:32:53 +01:00
|
|
|
}
|