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