2010-10-07 15:16:56 +02:00
|
|
|
<?php
|
2010-10-08 17:01:27 +02:00
|
|
|
/*
|
2011-07-18 22:43:12 +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.
|
|
|
|
*/
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2011-07-18 22:43:12 +02:00
|
|
|
namespace BeSimple\SoapBundle\Util;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
|
|
|
class Collection implements \IteratorAggregate, \Countable
|
|
|
|
{
|
|
|
|
private $elements = array();
|
2011-07-17 10:46:54 +02:00
|
|
|
private $getter;
|
|
|
|
private $class;
|
2010-10-07 15:16:56 +02:00
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
public function __construct($getter, $class = null)
|
2010-10-07 15:16:56 +02:00
|
|
|
{
|
2011-07-17 10:46:54 +02:00
|
|
|
$this->getter = $getter;
|
|
|
|
$this->class = $class;
|
2010-10-07 15:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function add($element)
|
|
|
|
{
|
2011-07-17 10:46:54 +02:00
|
|
|
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-07-17 10:46:54 +02:00
|
|
|
}
|
|
|
|
|
2011-08-11 00:59:40 +02:00
|
|
|
$this->elements[$element->{$this->getter}()] = $element;
|
2010-10-07 15:16:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function addAll($elements)
|
|
|
|
{
|
2011-07-14 17:45:03 +02:00
|
|
|
foreach ($elements as $element) {
|
2010-10-07 15:16:56 +02:00
|
|
|
$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);
|
|
|
|
}
|
|
|
|
|
2011-07-17 10:46:54 +02:00
|
|
|
public function getIterator()
|
2010-10-07 15:16:56 +02:00
|
|
|
{
|
|
|
|
return new \ArrayIterator($this->elements);
|
|
|
|
}
|
|
|
|
}
|