|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Adapter to provide information from soundcloud.com api |
| 4 | + */ |
| 5 | +namespace Embed\Adapters; |
| 6 | + |
| 7 | +use Embed\Url; |
| 8 | +use Embed\Providers\Provider; |
| 9 | + |
| 10 | +class Soundcloud extends Webpage implements AdapterInterface { |
| 11 | + public $Api; |
| 12 | + |
| 13 | + static public function check (Url $Url) { |
| 14 | + return $Url->match(array( |
| 15 | + 'https?://soundcloud.com/*' |
| 16 | + )); |
| 17 | + } |
| 18 | + |
| 19 | + protected function initProviders (Url $Url) { |
| 20 | + parent::initProviders($Url); |
| 21 | + |
| 22 | + $this->Api = new Provider(); |
| 23 | + |
| 24 | + $UrlApi = new Url('http://api.soundcloud.com/resolve.json'); |
| 25 | + $UrlApi->setParameter('client_id', isset($this->options['soundcloudClientId']) ? $this->options['soundcloudClientId'] : 'YOUR_CLIENT_ID'); |
| 26 | + $UrlApi->setParameter('url', $Url->getUrl()); |
| 27 | + |
| 28 | + if ($json = $UrlApi->getJsonContent()) { |
| 29 | + $this->Api->set($json); |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public function getTitle () { |
| 34 | + return $this->Api->get('title') ?: parent::getTitle(); |
| 35 | + } |
| 36 | + |
| 37 | + public function getDescription () { |
| 38 | + return $this->Api->get('description') ?: parent::getDescription(); |
| 39 | + } |
| 40 | + |
| 41 | + public function getUrl () { |
| 42 | + return $this->Api->get('permalink_url') ?: parent::getUrl(); |
| 43 | + } |
| 44 | + |
| 45 | + public function getImages () { |
| 46 | + $images = parent::getImages(); |
| 47 | + |
| 48 | + if (!$this->Api->get('artwork_url') && ($img = $this->Api->get('user', 'avatar_url'))) { |
| 49 | + array_unshift($images, str_replace('-large.jpg', '-t500x500.jpg', $img)); |
| 50 | + } |
| 51 | + |
| 52 | + return array_unique($images); |
| 53 | + } |
| 54 | + |
| 55 | + public function getAuthorName () { |
| 56 | + return $this->Api->get('user', 'username') ?: parent::getAuthorName(); |
| 57 | + } |
| 58 | + |
| 59 | + public function getAuthorUrl () { |
| 60 | + return $this->Api->get('user', 'permalink_url') ?: parent::getAuthorUrl(); |
| 61 | + } |
| 62 | +} |
0 commit comments