1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22: 23: 24: 25: 26: 27: 28: 29: 30: 31: 32: 33: 34: 35: 36: 37: 38: 39: 40: 41: 42: 43: 44: 45: 46: 47: 48: 49: 50: 51: 52: 53: 54: 55: 56: 57: 58: 59: 60: 61: 62: 63: 64: 65: 66: 67: 68: 69: 70: 71: 72: 73: 74: 75: 76: 77: 78: 79: 80: 81: 82: 83: 84: 85: 86: 87: 88: 89: 90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
<?php
/**
* Cart Item Bag
*/
namespace Omnipay\Common;
/**
* Cart Item Bag
*
* This class defines a bag (multi element set or array) of single cart items
* in the Omnipay system.
*
* @see Item
*/
class ItemBag implements \IteratorAggregate, \Countable
{
/**
* Item storage
*
* @see Item
*
* @var array
*/
protected $items;
/**
* Constructor
*
* @param array $items An array of items
*/
public function __construct(array $items = array())
{
$this->replace($items);
}
/**
* Return all the items
*
* @see Item
*
* @return array An array of items
*/
public function all()
{
return $this->items;
}
/**
* Replace the contents of this bag with the specified items
*
* @see Item
*
* @param array $items An array of items
*/
public function replace(array $items = array())
{
$this->items = array();
foreach ($items as $item) {
$this->add($item);
}
}
/**
* Add an item to the bag
*
* @see Item
*
* @param ItemInterface|array $item An existing item, or associative array of item parameters
*/
public function add($item)
{
if ($item instanceof ItemInterface) {
$this->items[] = $item;
} else {
$this->items[] = new Item($item);
}
}
/**
* Returns an iterator for items
*
* @return \ArrayIterator An \ArrayIterator instance
*/
public function getIterator()
{
return new \ArrayIterator($this->items);
}
/**
* Returns the number of items
*
* @return int The number of items
*/
public function count()
{
return count($this->items);
}
}