Collection

Simple collection class

Methods

addItem

Add an item to the collection.

Collection::addItem($obj, $key = null)

Example

Add an item to the collection:

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection');
  $collection->getItems();
  // returns [0 => 'Item in collection']

Add an item to the collection with a specific key:

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->getItems();
  // returns ['foo' => 'Item in collection']

setItem

Update a item in the collection by key.

Collection::setItem($obj, $key = null)

Example

Update item in the collection:

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->setItem('Update to item in collection', 'foo');
  $collection->getItems();
  // returns ['foo' => 'Update to item in collection']

getItem

Get an item from the collection by key.

Collection::getItem($key)

Example

Get an item from the collection:

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->getItem('foo');
  // returns 'Item in collection'

keys

Get all the keys in the collection

Collection::keys()

Example

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->addItem('Another in collection', 'bar');
  $collection->keys();
  // returns ['foo', 'bar']

length

Get count of all the items in the collection.

Collection::length()

Example

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->addItem('Another in collection', 'bar');
  $collection->length();
  // returns 2

keyExists

Checks to see if a key exists in the collection.

Collection::keyExists($key)

Example

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->addItem('Another in collection', 'bar');
  $collection->keyExists('foo');
  // returns true
  $collection->keyExists('bazz');
  // returns false

deleteItem

Delete an item from the collection.

Collection::deleteItem($key)

Example

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->addItem('Another in collection', 'bar');
  $collection->deleteItem('foo');
  $collection->getItems();
  // returns ['bar' => 'Another in collection']

getItems

Get all items from a collection

Collection::getItems()

Example

  use \Pbc\Bandolier\Type\Collection

  $collection = new Collection();
  $collection->addItem('Item in collection', 'foo');
  $collection->addItem('Another in collection', 'bar');
  $collection->getItems();
  // returns ['foo' => 'Item in collection', 'bar' => 'Another in collection']