5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
9 namespace Instagram\Collection;
14 * All Collections extend this class
16 abstract class CollectionAbstract implements \IteratorAggregate, \ArrayAccess, \Countable {
19 * Holds the pagination data for the collection
24 protected $pagination;
27 * Holds the data for the collection
32 protected $data = array();
35 * Holds the position for the iterator
45 * Sets the data and child object's proxies
47 * @param StdClass $data Data from the API
48 * @param \Instagram\Core\Proxy $proxy Proxy to pass on to teh collection's objects
51 public function __construct( $raw_data = null, \Instagram\Core\Proxy $proxy = null ) {
53 $this->setData( $raw_data );
56 $this->setProxies( $proxy );
61 * Set the collections data
63 * @param StdClass $data Data from the API
66 public abstract function setData( $raw_data );
69 * Get the collection's data
74 public function getData() {
79 * Get a collection item
81 * @param int $position Item to retrieve, starting at 0
82 * @return mixed Returns the collection item at the position
85 public function getItem( $position ) {
86 return isset( $this->data[$position] ) ? $this->data[$position] : null;
90 * Get a slice of the collection
92 * @param int $offset Where to start the slice
93 * @param int $length Length of the slice
94 * @return array Returns a slice of the array
97 public function getSlice( $offset, $length ) {
98 return array_slice( $this->data, $offset, $length );
104 * Add data from another collection the this collection
106 * @param \Instagram\Collection\CollectionAbstract $object Object to add the data of
109 public function addData( \Instagram\Collection\CollectionAbstract $object ) {
110 $this->data = array_merge( $this->data, $object->getData() );
114 * Convert the collection's objects
116 * Child classes use this to turn the objects into the correct class
118 * @param string $object
121 protected function convertData( $object ) {
122 $this->data = array_map(
123 function( $c ) use( $object ) {
124 return new $object( $c );
133 * Sets all the child object's proxies
135 * @param \Instagram\Core\Proxy $proxy
138 public function setProxies( \Instagram\Core\Proxy $proxy ) {
139 foreach( $this->data as $object ) {
140 $object->setProxy( $proxy );
145 * Implode the collection
147 * Implode the collection into a string
149 * Example - Get a media's tags into a comma delimited string
151 * $media->getTags()->implode(
152 * function( $t ){ return sprintf( '<a href="?example=tag.php&tag=%1$s">#%1$s</a>', $t ); }
155 * @param Closure $callback Function to run on the collection
156 * @param string $sep Implode separator
159 public function implode( \Closure $callback = null, $sep = ', ' ) {
160 if ( !count( $this->getData() ) ) {
164 $callback = function( $i ){ return $i->__toString(); };
166 foreach( $this->getData() as $item ) {
167 $items[] = $callback( $item );
169 return implode( $sep, $items );
175 * {@link http://us2.php.net/manual/en/class.iteratoraggregate.php}
177 public function getIterator(){
178 return new \ArrayIterator( $this->data );
184 * {@link http://us2.php.net/manual/en/class.arrayaccess.php}
186 public function offsetExists( $offset ) {
187 return isset( $this->data[$offset] );
189 public function offsetGet( $offset ) {
190 return $this->data[$offset];
192 public function offsetSet( $offset, $value ) {
193 trigger_error( "You can't set collection data");
195 public function offsetUnset( $offset ) {
196 trigger_error( "You can't unset collection data" );
202 * {@link http://us2.php.net/manual/en/class.countable.php}
204 public function count() {
205 return count( $this->data );