Completed
Push — master ( 7a0a03...73b89e )
by Colin
02:28
created

HtmlFilter   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 33
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
lcom 0
cbo 0
dl 0
loc 33
ccs 8
cts 8
cp 1
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A filter() 0 13 4
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace League\CommonMark\Util;
13
14
final class HtmlFilter
15
{
16
    // Return the entire string as-is
17
    public const ALLOW = 'allow';
18
    // Escape the entire string so any HTML/JS won't be interpreted as such
19
    public const ESCAPE = 'escape';
20
    // Return an empty string
21
    public const STRIP = 'strip';
22
23
    /**
24
     * Runs the given HTML through the given filter
25
     *
26
     * @param string $html   HTML input to be filtered
27
     * @param string $filter One of the HtmlFilter constants
28
     *
29
     * @return string Filtered HTML
30
     *
31
     * @throws \InvalidArgumentException when an invalid $filter is given
32
     */
33 285
    public static function filter(string $html, string $filter): string
34
    {
35
        switch ($filter) {
36 285
            case self::STRIP:
37 12
                return '';
38 273
            case self::ESCAPE:
39 12
                return \htmlspecialchars($html, \ENT_NOQUOTES);
40 261
            case self::ALLOW:
41 258
                return $html;
42
            default:
43 3
                throw new \InvalidArgumentException(sprintf('Invalid filter provided: "%s"', $filter));
44
        }
45
    }
46
}
47