BeSimpleSoap/Util/Collection.php

65 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2010-10-08 17:01:27 +02:00
/*
* This file is part of the BeSimpleSoapBundle.
2010-10-08 17:01:27 +02:00
*
* (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 BeSimple\SoapBundle\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) {
2011-07-24 14:40:52 +02:00
throw new \InvalidArgumentException(sprintf('Cannot add class "%s" because it is not an instance of "%s"', get_class($element), $this->class));
}
2011-08-11 00:59:40 +02:00
$this->elements[$element->{$this->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);
}
}