Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Collection of AW_Media_Objects
- */
- class AW_Media_Collection implements ArrayAccess, Iterator, Countable {
- /**
- * @var array
- */
- protected $media;
- /**
- * Iterator index
- * @var mixed
- */
- private $index;
- /**
- * Force this collection to only contain a single type
- * @var mixed
- */
- private $type;
- /**
- * @param string $type - Limit the possible contents to the given type
- * @return void
- */
- public function __construct($type = null) {
- $this->index = 0;
- $this->media = array();
- $this->type = $type;
- }
- /**
- * Fluid constructor
- *
- * @param array $from
- * @param string $type
- * @return AW_Media_Collection
- */
- public static function Create(array $from = null, $type = null) {
- $coll = new self($type);
- if (is_array($from)) {
- foreach ($from as $item) {
- if ($item instanceof AW_Media_Object) {
- $coll->add($item);
- } else if (is_string($item) || $item instanceof AW_Path_Filesystem) {
- $coll->add(AW_Media_Object::Create($item));
- } else {
- throw new AW_Media_Exception_CollectionItemInvalid('Invalid item type:');
- }
- }
- }
- return $coll;
- }
- /**
- * Add meta data to the collection
- *
- * @param mixed $key
- * @param mixed $value
- * @return AW_Media_Collection
- */
- public function setMetaData($key, $value) {
- $this->_metaData[$key] = $value;
- return $this;
- }
- /**
- * Return the meta data by key
- *
- * @param mixed $key
- * @return mixed
- */
- public function getMetaData($key) {
- if (array_key_exists($key, $this->_metaData)) {
- return $this->_metaData[$key];
- } else {
- // Try to get meta data from the first media object
- if (count($this->media)) {
- $metaData = $this->media->getMetaData($key);
- return $metaData;
- }
- }
- return null;
- }
- /**
- * Output a list of the filenames in the collection
- * Only added for debug/testing purposes
- *
- * @return void
- */
- public function listFiles() {
- echo "Collection files (".$this->count()."): \n";
- foreach ($this->media as $m) {
- echo "\t".(string)$m->getFile()."\n";
- }
- }
- /**
- * Add one element to the collection
- *
- * @param AW_Media_Object $item
- * @return AW_Media_Collection
- * @throws AW_Media_Exception_CollectionTypeMismatch
- */
- public function add(AW_Media_Object $item) {
- if (isset($this->type) && ($item->getType() != $this->type)) {
- throw new AW_Media_Exception_CollectionTypeMismatch("Cannot append a media object of a different type");
- }
- $this->media[] = $item;
- return $this;
- }
- /**
- * Return the type this connection is set to
- *
- * @return string
- */
- public function getType() {
- return $this->type;
- }
- /**
- * Append an existing collection to this collection
- *
- * @param array|AW_Media_Collection $collection
- * @return AW_Media_Collection $this
- */
- public function append($collection) {
- if (isset($this->type) && ($this->type !== $collection->type)) {
- throw new AW_Media_Exception_CollectionTypeMismatch("Cannot append a collection of a different type");
- }
- foreach ($collection as $item) {
- $this->add($item);
- }
- return $this;
- }
- public function count() {
- return count($this->media);
- }
- // ------------------------------------------------------------------------
- // ArrayAccess methods
- /**
- * @see ArrayAccess::offsetExists()
- * @param mixed $offset
- * @return boolean
- */
- public function offsetExists($offset) {
- return array_key_exists($offset, $this->media);
- }
- /**
- * @see ArrayAccess::offsetGet()
- * @param mixed $offset
- * @return AW_Media_Object
- */
- public function offsetGet($offset) {
- return (array_key_exists($offset, $this->media) ? $this->media[$offset] : null);
- }
- /**
- * @see ArrayAccess::offsetSet()
- * @param mixed $offset
- * @param AW_Media_Object $value
- * @return void
- */
- public function offsetSet($offset, $value) {
- if (!($value instanceof AW_Media_Object)) {
- throw new AW_Media_Exception_InvalidType("Collection cannot accept the given type");
- }
- if (isset($this->type)) {
- if ($this->type !== $value->type) {
- throw new AW_Media_Exception_InvalidType("Created to only contain {$this->type} objects");
- }
- }
- if (!isset($offset) || is_null($offset)) {
- $this->media[] = $value;
- } else {
- $this->media[$offset] = $value;
- }
- }
- /**
- * @see ArrayAccess::offsetUnset()
- * @param mixed $offset
- * @return void
- */
- public function offsetUnset($offset) {
- if (array_key_exists($offset, $this->media)) {
- unset($this->media[$offset]);
- }
- }
- // ------------------------------------------------------------------------
- // Iterator methods
- /**
- * @see Iterator::current()
- * @return AW_Media_Object
- */
- public function current() {
- return (array_key_exists($this->index, $this->media) ? $this->media[$this->index] : null);
- }
- /**
- * @see Iterator::key()
- * @return mixed
- */
- public function key() {
- return $this->index;
- }
- /**
- * @see Iterator::next()
- * @return void
- */
- public function next() {
- ++$this->index;
- }
- /**
- * @see Iterator::rewind()
- * @return void
- */
- public function rewind() {
- $this->index = 0;
- }
- /**
- * @see Iterator::valid()
- * @return boolean
- */
- public function valid() {
- return (array_key_exists($this->index, $this->media));
- }
- }
Add Comment
Please, Sign In to add comment