PageRenderTime 39ms CodeModel.GetById 14ms app.highlight 16ms RepoModel.GetById 1ms app.codeStats 0ms

/Instagram/Media.php

http://github.com/galen/PHP-Instagram-API
PHP | 328 lines | 110 code | 31 blank | 187 comment | 11 complexity | 3f9a9083b53989e78259724cad3eda6d MD5 | raw file
  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
  9namespace Instagram;
 10
 11use \Instagram\Comment;
 12use \Instagram\User;
 13use \Instagram\Location;
 14use \Instagram\Collection\CommentCollection;
 15use \Instagram\Collection\TagCollection;
 16use \Instagram\Collection\UserCollection;
 17
 18/**
 19 * Media class
 20 *
 21 * @see \Instagram\Instagram->getLocation()
 22 * {@link https://github.com/galen/PHP-Instagram-API/blob/master/Examples/media.php}
 23 * {@link http://galengrover.com/projects/instagram/?example=media.php}
 24 */
 25class Media extends \Instagram\Core\BaseObjectAbstract {
 26
 27    /**
 28     * User cache
 29     * 
 30     * @var \Instagram\User
 31     */
 32    protected $user = null;
 33
 34    /**
 35     * Comments cache
 36     *
 37     * @var \Instagram\Collection\CommentCollection
 38     */
 39    protected $comments = null;
 40
 41    /**
 42     * Location cache
 43     *
 44     * @var \Instagram\Location
 45     */
 46    protected $location = null;
 47
 48    /**
 49     * Tags cache
 50     *
 51     * @var \Instagram\Collection\TagCollection
 52     */
 53    protected $tags = null;
 54
 55    /**
 56     * Get the ID
 57     * Get the media type
 58     *
 59     * @return string
 60     * @access public
 61     */
 62    public function getId() {
 63        return $this->data->id;
 64     }
 65
 66    /**
 67     * Get the media type
 68     *
 69     * @return string
 70     * @access public
 71     */
 72    public function getType() {
 73        return $this->data->type;
 74    }
 75
 76    /**
 77     * Get the thumbnail
 78     *
 79     * @return string
 80     * @access public
 81     */
 82    public function getThumbnail() {
 83        return $this->data->images->thumbnail;
 84    }
 85
 86    /**
 87     * Get the standard resolution video
 88     *
 89     * @return string
 90     * @access public
 91     */
 92    public function getStandardResVideo() {
 93        return $this->data->videos->standard_resolution;
 94    }
 95
 96    /**
 97     * Get the low resolution video
 98     *
 99     * @return string
100     * @access public
101     */
102    public function getLowResVideo() {
103        return $this->data->videos->low_resolution;
104    }
105
106    /**
107     * Get the standard resolution image
108     *
109     * @return string
110     * @access public
111     */
112    public function getStandardResImage() {
113        return $this->data->images->standard_resolution;
114    }
115
116    /**
117     * Get the low resolution image
118     *
119     * @return string
120     * @access public
121     */
122    public function getLowResImage() {
123        return $this->data->images->low_resolution;
124    }
125
126    /**
127     * Get the standard resolution image
128     *
129     * Alias for getStandardResImage since Instagram added videos
130     * 
131     * @return string
132     * @access public
133     */
134    public function getStandardRes() {
135        return $this->getStandardResImage();
136    }
137
138    /**
139     * Get the low resolution image
140     *
141     * Alias for getLowResImage since Instagram added videos
142     *
143     * @return string
144     * @access public
145     */
146    public function getLowRes() {
147        return $this->getLowResImage();
148    }
149
150    /**
151     * Get the media caption
152     *
153     * @return string
154     * @access public
155     */
156    public function getCaption() {
157        if ( $this->data->caption ) {
158            return new Comment( $this->data->caption );
159        }
160        return null;
161    }
162
163    /**
164     * Get the created time
165     *
166     * @param string $format {@link http://php.net/manual/en/function.date.php}
167     * @return string
168     * @access public
169     */
170    public function getCreatedTime( $format = null ) {
171        if ( $format ) {
172            $date = date( $format, $this->data->created_time );
173        }
174        else {
175            $date = $this->data->created_time;
176        }
177        return $date;
178    }
179
180    /**
181     * Get the user that posted the media
182     *
183     * @return \Instagram\User
184     * @access public
185     */
186    public function getUser() {
187        if ( !$this->user ) {
188            $this->user = new User( $this->data->user, $this->proxy );
189        }
190        return $this->user;
191    }
192
193    /**
194     * Get media comments
195     *
196     * Return all the comments associated with a media
197     *
198     * @return \Instagram\CommentCollection
199     * @access public
200     */
201    public function getComments() {
202        if ( !$this->comments ) {
203            $this->comments = new CommentCollection( $this->proxy->getMediaComments( $this->getApiId() ), $this->proxy );
204        }
205        return $this->comments;
206    }
207
208    /**
209     * Get the media filter
210     *
211     * @return string
212     * @access public
213     */
214    public function getFilter() {
215        return $this->data->filter;
216    }
217
218    /**
219     * Get the media's tags
220     *
221     * @return \Instagram\Collection\TagCollection
222     * @access public
223     */
224    public function getTags() {
225        if ( !$this->tags ) {
226            $this->tags = new TagCollection( $this->data->tags, $this->proxy );
227        }
228        return $this->tags;
229    }
230
231    /**
232     * Get the media's link
233     *
234     * @return string
235     * @access public
236     */
237    public function getLink() {
238        return $this->data->link;
239    }
240
241    /**
242     * Get the media's likes count
243     *
244     * @return int
245     * @access public
246     */
247    public function getLikesCount() {
248        return (int)$this->data->likes->count;
249    }
250
251    /**
252     * Get media likes
253     *
254     * Media objects contain the first 10 likes. You can get these likes by passing `false`
255     * to this method. Using the internal likes of a media object cause issues when liking/disliking media.
256     *
257     * @param bool $fetch_from_api Query the API or use internal
258     * @return \Instagram\UserCollection
259     * @access public
260     */
261    public function getLikes( $fetch_from_api = true ) {
262        if ( !$fetch_from_api ) {
263            return new UserCollection( $this->data->likes );
264        }
265        $user_collection = new UserCollection( $this->proxy->getMediaLikes( $this->getApiId() ), $this->proxy );
266        $user_collection->setProxies( $this->proxy );
267        $this->likes = $user_collection;
268        return $this->likes;
269    }
270
271    /**
272     * Get location status
273     *
274     * Will return true if any location data is associated with the media
275     *
276     * @return bool
277     * @access public
278     */
279    public function hasLocation() {
280        return isset( $this->data->location->latitude ) && isset( $this->data->location->longitude );
281    }
282
283    /**
284     * Get location status
285     *
286     * Will return true if the media has a named location attached to it
287     *
288     * Some media only has lat/lng data
289     *
290     * @return bool
291     * @access public
292     */
293    public function hasNamedLocation() {
294        return isset( $this->data->location->id );
295    }
296
297    /**
298     * Get the location
299     *
300     * Returns the location associated with the media or null if no location data is available
301     *
302     * @param bool $force_fetch Don't use the cache
303     * @return \Instagram\Location|null
304     * @access public
305     */
306    public function getLocation( $force_fetch = false ) {
307        if ( !$this->hasLocation() ) {
308            return null;
309        }
310        if ( !$this->location || (bool)$force_fetch ) {
311            $this->location = new Location( $this->data->location, isset( $this->data->location->id ) ? $this->proxy : null );
312        }
313        return $this->location;
314    }
315
316    /**
317     * Magic toString method
318     *
319     * Returns the media's thumbnail url
320     *
321     * @return string
322     * @access public
323     */
324    public function __toString() {
325        return $this->getThumbnail()->url;
326    }
327
328}