Propel: BaseObject Collection

May 28, 2008 at 11:33 pm Leave a comment

Here the code snippet I promised in my last blog entry. Two very simple pieces of code — 1 class and 1 interface — that use the existing PHP array functionality with an OO face-lift. Since Propel methods such as doSelect() return a regular PHP array, all you need to do is pass that array to the constructor of the BaseObjectCollection in order to have an OO interface to your Propel collections. the BaseObjectCollection can (and should) be extended to allow for a type-hinted add method and allow for additional methods that are collection specific.

The first piece of code required is the interface:

interface BaseObjectCollectionInterface extends IteratorAggregate
{
	public function add(BaseObject $o);
	public function addAll(BaseObjectCollectionInterface $c);
	public function clear();
	public function contains(BaseObject $o);
	public function containsAll(BaseObjectCollectionInterface $c);
	public function indexOf(BaseObject $o);
	public function isEmpty();
	public function remove(BaseObject $o);
	public function removeAll(BaseObjectCollectionInterface $c);
	public function size();
	public function toArray();
}

You’ll see that this extends PHP’s native IteratorAggregate interface which will allow the collection to have an iterator traversed in foreach loops and other related functionality. This interface is also loosely based on the Java API Collection interface. It defines the minimum functionality that is expected of a BaseObjectCollection.

The next piece of code is the actual Collection class:

class BaseObjectCollection extends ArrayObject implements BaseObjectCollectionInterface
{

	public function __construct(array $array = array())
	{
		parent::__construct($array);
	}

	public function add(BaseObject $object)
	{
		$this->append($object);
	}

	public function addAll(BaseObjectCollectionInterface $collection)
	{

		foreach($collection as $element){
			$this->add($element);
		}

	}

	public function clear()
	{
		$this->exchangeArray(array());
	}

	public function contains(BaseObject $object)
	{
		return (boolean)in_array($object, (array)$this, true);
	}

	public function containsAll(BaseObjectCollectionInterface $collection)
	{

		$containsAll = true;

		foreach($collection as $element){

			$containsAll = $containsAll && $this->contains($element);

			if (!$containsAll){
				return false;
			}

		}

		return true;

	}

	public function indexOf(BaseObject $object)
	{
		return array_search($object, (array)$this, true);
	}

	public function isEmpty()
	{
		return ((int)$this->count() == 0);
	}

	public function remove(BaseObject $object)
	{

		$index = $this->indexOf($object);

		if ($index !== false){
			$array = (array)$this;
			array_splice($array, $index, 1);
			$this->exchangeArray($array);
			return true;
		}

		return false;

	}

	public function removeAll(BaseObjectCollectionInterface $collection)
	{

		foreach($collection as $element){
			$this->remove($element);
		}

	}

	public function size()
	{
		return $this->count();
	}

	public function toArray()
	{
		return (array)$this;
	}

}

You should notice that this collection implements the BaseObjectCollectionInterface defined previously as well as extends the PHP ArrayObject class. The majority of the functionality required for this collection is implemented in the ArrayObject class, and by extending this class the BaseObjectCollection can utilize much the built in PHP functionality such as ArrayAccess and can be passed to PHP array functions such as count().

The code itself is quite simple. But then again, in most cases the simplest solution is the best.

Advertisement

Entry filed under: Uncategorized. Tags: .

PHP Object/Relational Mapper Favour Composition over Inheritance

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Trackback this post  |  Subscribe to the comments via RSS Feed


Calendar

May 2008
S M T W T F S
« Mar   Jun »
 123
45678910
11121314151617
18192021222324
25262728293031

Most Recent Posts


Follow

Get every new post delivered to your Inbox.