Guest User

Untitled

a guest
Feb 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.28 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4.  * Collection of AW_Media_Objects
  5.  */
  6. class AW_Media_Collection implements ArrayAccess, Iterator, Countable {
  7.  
  8.     /**
  9.      * @var array
  10.      */
  11.     protected $media;
  12.  
  13.     /**
  14.      * Iterator index
  15.      * @var mixed
  16.      */
  17.     private $index;
  18.  
  19.     /**
  20.      * Force this collection to only contain a single type
  21.      * @var mixed
  22.      */
  23.     private $type;
  24.  
  25.     /**
  26.      * @param  string $type - Limit the possible contents to the given type
  27.      * @return void
  28.      */
  29.     public function __construct($type = null) {
  30.         $this->index = 0;
  31.         $this->media = array();
  32.         $this->type  = $type;
  33.     }
  34.  
  35.     /**
  36.      * Fluid constructor
  37.      *
  38.      * @param  array $from
  39.      * @param  string $type
  40.      * @return AW_Media_Collection
  41.      */
  42.     public static function Create(array $from = null, $type = null) {
  43.         $coll = new self($type);
  44.         if (is_array($from)) {
  45.             foreach ($from as $item) {
  46.                 if ($item instanceof AW_Media_Object) {
  47.                     $coll->add($item);
  48.                 } else if (is_string($item) || $item instanceof AW_Path_Filesystem) {
  49.                     $coll->add(AW_Media_Object::Create($item));
  50.                 } else {
  51.                     throw new AW_Media_Exception_CollectionItemInvalid('Invalid item type:');
  52.                 }
  53.             }
  54.         }
  55.         return $coll;
  56.     }
  57.  
  58.     /**
  59.      * Add meta data to the collection
  60.      *
  61.      * @param  mixed $key
  62.      * @param  mixed $value
  63.      * @return AW_Media_Collection
  64.      */
  65.     public function setMetaData($key, $value) {
  66.         $this->_metaData[$key] = $value;
  67.         return $this;
  68.     }
  69.  
  70.     /**
  71.      * Return the meta data by key
  72.      *
  73.      * @param  mixed $key
  74.      * @return mixed
  75.      */
  76.     public function getMetaData($key) {
  77.         if (array_key_exists($key, $this->_metaData)) {
  78.             return $this->_metaData[$key];
  79.         } else {
  80.             // Try to get meta data from the first media object
  81.             if (count($this->media)) {
  82.                 $metaData = $this->media->getMetaData($key);
  83.                 return $metaData;
  84.             }
  85.         }
  86.         return null;
  87.     }
  88.  
  89.     /**
  90.      * Output a list of the filenames in the collection
  91.      * Only added for debug/testing purposes
  92.      *
  93.      * @return void
  94.      */
  95.     public function listFiles() {
  96.         echo "Collection files (".$this->count()."): \n";
  97.         foreach ($this->media as $m) {
  98.             echo "\t".(string)$m->getFile()."\n";
  99.         }
  100.     }
  101.  
  102.     /**
  103.      * Add one element to the collection
  104.      *
  105.      * @param  AW_Media_Object $item
  106.      * @return AW_Media_Collection
  107.      * @throws AW_Media_Exception_CollectionTypeMismatch
  108.      */
  109.     public function add(AW_Media_Object $item) {
  110.         if (isset($this->type) && ($item->getType() != $this->type)) {
  111.             throw new AW_Media_Exception_CollectionTypeMismatch("Cannot append a media object of a different type");
  112.         }
  113.         $this->media[] = $item;
  114.         return $this;
  115.     }
  116.  
  117.     /**
  118.      * Return the type this connection is set to
  119.      *
  120.      * @return string
  121.      */
  122.     public function getType() {
  123.         return $this->type;
  124.     }
  125.  
  126.     /**
  127.      * Append an existing collection to this collection
  128.      *
  129.      * @param array|AW_Media_Collection $collection
  130.      * @return AW_Media_Collection $this
  131.      */
  132.     public function append($collection) {
  133.         if (isset($this->type) && ($this->type !== $collection->type)) {
  134.             throw new AW_Media_Exception_CollectionTypeMismatch("Cannot append a collection of a different type");
  135.         }
  136.         foreach ($collection as $item) {
  137.             $this->add($item);
  138.         }
  139.         return $this;
  140.     }
  141.  
  142.     public function count() {
  143.         return count($this->media);
  144.     }
  145.  
  146.     // ------------------------------------------------------------------------
  147.     // ArrayAccess methods
  148.  
  149.     /**
  150.      * @see ArrayAccess::offsetExists()
  151.      * @param  mixed $offset
  152.      * @return boolean
  153.      */
  154.     public function offsetExists($offset) {
  155.         return array_key_exists($offset, $this->media);
  156.     }
  157.  
  158.     /**
  159.      * @see ArrayAccess::offsetGet()
  160.      * @param  mixed $offset
  161.      * @return AW_Media_Object
  162.      */
  163.     public function offsetGet($offset) {
  164.         return (array_key_exists($offset, $this->media) ? $this->media[$offset] : null);
  165.     }
  166.  
  167.     /**
  168.      * @see ArrayAccess::offsetSet()
  169.      * @param  mixed $offset
  170.      * @param  AW_Media_Object $value
  171.      * @return void
  172.      */
  173.     public function offsetSet($offset, $value) {
  174.         if (!($value instanceof AW_Media_Object)) {
  175.             throw new AW_Media_Exception_InvalidType("Collection cannot accept the given type");
  176.         }
  177.         if (isset($this->type)) {
  178.             if ($this->type !== $value->type) {
  179.                 throw new AW_Media_Exception_InvalidType("Created to only contain {$this->type} objects");
  180.             }
  181.         }
  182.         if (!isset($offset) || is_null($offset)) {
  183.             $this->media[] = $value;
  184.         } else {
  185.             $this->media[$offset] = $value;
  186.         }
  187.     }
  188.  
  189.     /**
  190.      * @see ArrayAccess::offsetUnset()
  191.      * @param  mixed $offset
  192.      * @return void
  193.      */
  194.     public function offsetUnset($offset) {
  195.         if (array_key_exists($offset, $this->media)) {
  196.             unset($this->media[$offset]);
  197.         }
  198.     }
  199.  
  200.     // ------------------------------------------------------------------------
  201.     // Iterator methods
  202.  
  203.     /**
  204.      * @see Iterator::current()
  205.      * @return AW_Media_Object
  206.      */
  207.     public function current() {
  208.         return (array_key_exists($this->index, $this->media) ? $this->media[$this->index] : null);
  209.     }
  210.  
  211.     /**
  212.      * @see Iterator::key()
  213.      * @return mixed
  214.      */
  215.     public function key() {
  216.         return $this->index;
  217.     }
  218.  
  219.     /**
  220.      * @see Iterator::next()
  221.      * @return void
  222.      */
  223.     public function next() {
  224.         ++$this->index;
  225.     }
  226.  
  227.     /**
  228.      * @see Iterator::rewind()
  229.      * @return void
  230.      */
  231.     public function rewind() {
  232.         $this->index = 0;
  233.     }
  234.  
  235.     /**
  236.      * @see Iterator::valid()
  237.      * @return boolean
  238.      */
  239.     public function valid() {
  240.         return (array_key_exists($this->index, $this->media));
  241.     }
  242. }
Add Comment
Please, Sign In to add comment