1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SpringboardVR\LaravelAblyBroadcaster; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Str; |
6
|
|
|
|
7
|
|
|
trait UseAblyChannelConventions |
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* Return true if channel is protected by authentication. |
11
|
|
|
* |
12
|
|
|
* @param string $channel |
13
|
|
|
* @return bool |
14
|
|
|
*/ |
15
|
|
|
public function isGuardedChannel($channel) |
16
|
|
|
{ |
17
|
|
|
return Str::startsWith($channel, ['private-', 'presence-']); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Remove prefix from channel name. |
22
|
|
|
* |
23
|
|
|
* @param string $channel |
24
|
|
|
* @return string |
25
|
|
|
*/ |
26
|
|
|
public function normalizeChannelName($channel) |
27
|
|
|
{ |
28
|
|
|
if ($this->isGuardedChannel($channel)) { |
29
|
|
|
return Str::startsWith($channel, 'private-') |
30
|
|
|
? Str::replaceFirst('private-', '', $channel) |
31
|
|
|
: Str::replaceFirst('presence-', '', $channel); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $channel; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Format the channel array into an array of strings. |
39
|
|
|
* |
40
|
|
|
* @param array $channels |
41
|
|
|
* @return array |
42
|
|
|
*/ |
43
|
|
|
protected function formatChannels(array $channels) |
44
|
|
|
{ |
45
|
|
|
return array_map(function ($channel) { |
46
|
|
|
$channel = (string) $channel; |
47
|
|
|
if (Str::startsWith($channel, ['private-', 'presence-'])) { |
48
|
|
|
return Str::startsWith($channel, 'private-') |
49
|
|
|
? Str::replaceFirst('private-', 'private:', $channel) |
50
|
|
|
: Str::replaceFirst('presence-', 'presence:', $channel); |
51
|
|
|
} |
52
|
|
|
return 'public:' . $channel; |
53
|
|
|
}, $channels); |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|