1 | <?php |
||
10 | class SoundCloudProvider extends AbstractOembedProvider implements ProviderInterface |
||
11 | { |
||
12 | const URL_OEMBED = 'https://soundcloud.com/oembed?format=json&url=https://soundcloud.com/%s'; |
||
13 | const URL = 'https://soundcloud.com/%s'; |
||
14 | |||
15 | /** |
||
16 | * @param FormMapper $formMapper |
||
17 | */ |
||
18 | 1 | public function buildProviderEditFormBefore(FormMapper $formMapper) |
|
19 | { |
||
20 | 1 | $formMapper->add('providerReference', TextType::class, ['label' => $this->getReferenceLabel()]); |
|
21 | 1 | } |
|
22 | |||
23 | /** |
||
24 | * @param FormMapper $formMapper |
||
25 | */ |
||
26 | 1 | public function buildProviderEditFormAfter(FormMapper $formMapper) |
|
27 | { |
||
28 | $formMapper |
||
29 | 1 | ->tab('form.embed_options') |
|
30 | 1 | ->add( |
|
31 | 1 | 'providerMetaData', |
|
32 | 1 | ImmutableArrayType::class, |
|
33 | [ |
||
34 | 'keys' => [ |
||
35 | 1 | ['autoPlay', CheckboxType::class, ['label' => 'form.auto_play', 'required' => false]], |
|
36 | 1 | ['hideRelated', CheckboxType::class, ['label' => 'form.hide_related', 'required' => false]], |
|
37 | 1 | ['showComments', CheckboxType::class, ['label' => 'form.show_comments', 'required' => false]], |
|
38 | 1 | ['showUser', CheckboxType::class, ['label' => 'form.show_user', 'required' => false]], |
|
39 | 1 | ['showReposts', CheckboxType::class, ['label' => 'form.show_reposts', 'required' => false]], |
|
40 | 1 | ['showVisual', CheckboxType::class, ['label' => 'form.show_visual', 'required' => false]], |
|
41 | 1 | ], |
|
42 | 1 | 'label' => 'form.embed_options', |
|
43 | 'required' => false |
||
44 | 1 | ] |
|
45 | 1 | )->end() |
|
46 | ; |
||
47 | 1 | } |
|
48 | |||
49 | /** |
||
50 | * @param string $id |
||
51 | * @return string |
||
52 | */ |
||
53 | 1 | public function getOembedUrl($id) |
|
54 | { |
||
55 | 1 | return sprintf(self::URL_OEMBED, $id); |
|
56 | } |
||
57 | |||
58 | /** |
||
59 | * @param $value |
||
60 | * @return string |
||
61 | * @throws \Exception |
||
62 | */ |
||
63 | 1 | public function parseProviderReference($value) |
|
64 | { |
||
65 | 1 | if (strpos($value, 'soundcloud.com')) { |
|
66 | 1 | $url = parse_url($value); |
|
67 | |||
68 | 1 | return trim($url['path'], '/'); |
|
69 | } |
||
70 | |||
71 | 1 | return $value; |
|
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param $id |
||
76 | * @return array |
||
77 | */ |
||
78 | 1 | protected function getOembedDataCache($id) |
|
79 | { |
||
80 | 1 | $data = parent::getOembedDataCache($id); |
|
81 | 1 | $data['embedUrl'] = $this->extractEmbedUrl($data); |
|
82 | |||
83 | 1 | return $data; |
|
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param array $data |
||
88 | */ |
||
89 | 1 | protected function extractEmbedUrl(array $data) |
|
90 | { |
||
91 | 1 | preg_match('/src="(.*)"/', $data['html'], $matches); |
|
92 | 1 | $url = $matches[1]; |
|
93 | |||
94 | 1 | $data = parse_url($url); |
|
95 | 1 | parse_str($data['query'], $data); |
|
96 | |||
97 | 1 | return $data['url']; |
|
98 | } |
||
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | 5 | public function getIcon() |
|
104 | { |
||
105 | 5 | return 'fa fa-soundcloud'; |
|
106 | } |
||
107 | |||
108 | /** |
||
109 | * @return string |
||
110 | */ |
||
111 | 5 | public function getName() |
|
115 | |||
116 | /** |
||
117 | * @return string |
||
118 | */ |
||
119 | 1 | public function getType() |
|
120 | { |
||
121 | 1 | return AbstractProvider::TYPE_AUDIO; |
|
122 | } |
||
123 | } |
||
124 |