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