Completed
Push — master ( 7a6aee...e5c914 )
by Colin
52:42 queued 27:44
created

Xml::escape()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 10
rs 9.4285
cc 2
eloc 6
nc 2
nop 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
    public static function escape($string, $preserveEntities = false)
29
    {
30
        if ($preserveEntities) {
31
            $string = preg_replace('/[&](?![#](x[a-f0-9]{1,8}|[0-9]{1,8});|[a-z][a-z0-9]{1,31};)/i', '&amp;', $string);
32
        } else {
33
            $string = str_replace('&', '&amp;', $string);
34
        }
35
36
        return str_replace(['<', '>', '"'], ['&lt;', '&gt;', '&quot;'], $string);
37
    }
38
}
39