Completed
Push — escape-helper ( da2c52 )
by Colin
04:02
created

Xml::escape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 10
ccs 6
cts 6
cp 1
rs 9.4285
cc 2
eloc 6
nc 2
nop 2
crap 2
1
<?php
2
3
/*
4
 * This file is part of the league/commonmark package.
5
 *
6
 * (c) Colin O'Dell <[email protected]>
7
 *
8
 * Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js)
9
 *  - (c) John MacFarlane
10
 *
11
 * For the full copyright and license information, please view the LICENSE
12
 * file that was distributed with this source code.
13
 */
14
15
namespace League\CommonMark\Util;
16
17
/**
18
 * Utility class for handling/generating XML and HTML
19
 */
20
final class Xml
21
{
22
    /**
23
     * @param string $string
24
     * @param bool   $preserveEntities
25
     *
26
     * @return string
27
     */
28 1782
    public static function escape($string, $preserveEntities = false)
29
    {
30 1782
        if ($preserveEntities) {
31 399
            $string = preg_replace('/[&](?![#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)/i', '&amp;', $string);
32 399
        } else {
33 1779
            $string = str_replace('&', '&amp;', $string);
34
        }
35
36 1782
        return str_replace(['<', '>', '"'], ['&lt;', '&gt;', '&quot;'], $string);
37
    }
38
}
39