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