generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 14
/
SanitizerFactory.php
47 lines (40 loc) · 1.32 KB
/
SanitizerFactory.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
namespace Workbench\App\Html;
use Symfony\Component\HtmlSanitizer\HtmlSanitizer;
use Symfony\Component\HtmlSanitizer\HtmlSanitizerConfig;
class SanitizerFactory
{
private static $cache = [];
public static function make($config = null): Sanitizer
{
return new Sanitizer(new HtmlSanitizer(static::configFor($config)));
}
private static function configFor($config = null): HtmlSanitizerConfig
{
return static::$cache[$config ?? 'default'] ??= match ($config) {
'minimal' => static::minimalConfig(),
default => static::defaultConfig(),
};
}
private static function defaultConfig(): HtmlSanitizerConfig
{
return (new HtmlSanitizerConfig)
->allowSafeElements()
->allowAttribute('class', '*');
}
private static function minimalConfig(): HtmlSanitizerConfig
{
return (new HtmlSanitizerConfig)
->allowElement('br')
->allowElement('div')
->allowElement('p')
->allowElement('span')
->allowElement('img', ['class', 'src', 'alt'])
->allowElement('a', ['href'])
->allowElement('strong')
->allowElement('b')
->allowElement('em')
->allowElement('i')
->allowAttribute('class', '*');
}
}