BeSimpleSoap/Util/Collection.php

66 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2010-10-08 17:01:27 +02:00
/*
* This file is part of the WebServiceBundle.
*
* (c) Christian Kerl <christian-kerl@web.de>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Bundle\WebServiceBundle\Util;
class Collection implements \IteratorAggregate, \Countable
{
private $elements = array();
private $getter;
private $class;
public function __construct($getter, $class = null)
{
$this->getter = $getter;
$this->class = $class;
}
public function add($element)
{
if ($this->class && !$element instanceof $this->class) {
throw new \InvalidArgument(sprintf('Cannot add class "%s" because it is not an instance of "%s"', get_class($element), $class));
}
$getter = $this->getter;
$this->elements[$element->$getter()] = $element;
}
public function addAll($elements)
{
2011-07-14 17:45:03 +02:00
foreach ($elements as $element) {
$this->add($element);
}
}
public function has($key)
{
return isset($this->elements[$key]);
}
public function get($key)
{
return $this->has($key) ? $this->elements[$key] : null;
}
public function clear()
{
$this->elements = array();
}
public function count()
{
return count($this->elements);
}
public function getIterator()
{
return new \ArrayIterator($this->elements);
}
}