Version 1
[yaffs-website] / vendor / php-instagram-api / php-instagram-api / Instagram / Collection / CollectionAbstract.php
1 <?php
2
3 /**
4 * Instagram PHP
5 * @author Galen Grover <galenjr@gmail.com>
6 * @license http://opensource.org/licenses/mit-license.php The MIT License
7 */
8
9 namespace Instagram\Collection;
10
11 /**
12  * Abstract Collection
13  *
14  * All Collections extend this class
15  */
16 abstract class CollectionAbstract implements \IteratorAggregate, \ArrayAccess, \Countable {
17
18     /**
19      * Holds the pagination data for the collection
20      *
21      * @var StdClass
22      * @access protected
23      */
24     protected $pagination;
25
26     /**
27      * Holds the data for the collection
28      *
29      * @var array
30      * @access protected
31      */
32     protected $data = array();
33
34     /**
35      * Holds the position for the iterator
36      *
37      * @var integer
38      * @access protected
39      */
40     protected $position;
41
42     /**
43      * Constructor
44      *
45      * Sets the data and child object's proxies
46      *
47      * @param StdClass $data Data from the API
48      * @param \Instagram\Core\Proxy $proxy Proxy to pass on to teh collection's objects
49      * @access public
50      */
51     public function __construct( $raw_data = null, \Instagram\Core\Proxy $proxy = null ) {
52         if ( $raw_data ) {
53             $this->setData( $raw_data );
54         }
55         if ( $proxy ) {
56             $this->setProxies( $proxy );
57         }
58     }
59
60     /**
61      * Set the collections data
62      *
63      * @param StdClass $data Data from the API
64      * @access public
65      */
66     public abstract function setData( $raw_data );
67
68     /**
69      * Get the collection's data
70      *
71      * @return StdClass
72      * @access public
73      */
74     public function getData() {
75         return $this->data;
76     }
77
78     /**
79      * Get a collection item
80      *
81      * @param int $position Item to retrieve, starting at 0
82      * @return mixed Returns the collection item at the position
83      * @access public
84      */
85     public function getItem( $position ) {
86         return isset( $this->data[$position] ) ? $this->data[$position] : null;
87     }
88
89     /**
90      * Get a slice of the collection
91      *
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
95      * @access public
96      */
97     public function getSlice( $offset, $length ) {
98         return array_slice( $this->data, $offset, $length );
99     }
100
101     /**
102      * Add data
103      *
104      * Add data from another collection the this collection
105      *
106      * @param \Instagram\Collection\CollectionAbstract $object Object to add the data of
107      * @access public
108      */
109     public function addData( \Instagram\Collection\CollectionAbstract $object ) {
110         $this->data = array_merge( $this->data, $object->getData() );
111     }
112
113     /**
114      * Convert the collection's objects
115      *
116      * Child classes use this to turn the objects into the correct class
117      *
118      * @param string $object
119      * @access protected
120      */
121     protected function convertData( $object ) {
122         $this->data = array_map(
123             function( $c ) use( $object ) {
124                 return new $object( $c );
125             },
126             $this->data
127         );
128     }
129
130     /**
131      * Set object proxies
132      *
133      * Sets all the child object's proxies
134      *
135      * @param \Instagram\Core\Proxy $proxy
136      * @access public
137      */
138     public function setProxies( \Instagram\Core\Proxy $proxy ) {
139         foreach( $this->data as $object ) {
140             $object->setProxy( $proxy );
141         }
142     }
143
144     /**
145      * Implode the collection
146      *
147      * Implode the collection into a string
148      *
149      * Example - Get a media's tags into a comma delimited string
150      *
151      * $media->getTags()->implode(
152      *     function( $t ){ return sprintf( '<a href="?example=tag.php&tag=%1$s">#%1$s</a>', $t ); }
153      * )
154      *
155      * @param Closure $callback Function to run on the collection 
156      * @param string $sep Implode separator
157      * @access public
158      */
159     public function implode( \Closure $callback = null, $sep = ', ' ) {
160         if ( !count( $this->getData() ) ) {
161             return null;
162         }
163         if ( !$callback ) {
164             $callback = function( $i ){ return $i->__toString(); };
165         }
166         foreach( $this->getData() as $item ) {
167             $items[] = $callback( $item );
168         }
169         return implode( $sep, $items );
170     }
171
172     /**
173      * IteratorAggregate
174      *
175      * {@link http://us2.php.net/manual/en/class.iteratoraggregate.php}
176      */
177     public function getIterator(){
178         return new \ArrayIterator( $this->data );
179     }
180
181     /**
182      * ArrayAccess
183      *
184      * {@link http://us2.php.net/manual/en/class.arrayaccess.php}
185      */
186     public function offsetExists( $offset ) {
187         return isset( $this->data[$offset] );
188     }
189     public function offsetGet( $offset ) {
190         return $this->data[$offset];
191     }
192     public function offsetSet( $offset, $value ) {
193         trigger_error( "You can't set collection data");
194     }
195     public function offsetUnset( $offset ) {
196         trigger_error( "You can't unset collection data" );
197     }
198
199     /**
200      * Countable
201      * 
202      * {@link http://us2.php.net/manual/en/class.countable.php}
203      */
204     public function count() {
205         return count( $this->data );
206     }
207
208 }