Completed
Push — master ( 30aab1...cadcfa )
by Asmir
07:00
created

CharacterReference   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 52
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 4
cbo 1
dl 0
loc 52
ccs 7
cts 7
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A lookupName() 0 5 2
A lookupDecimal() 0 7 1
A lookupHex() 0 4 1
1
<?php
2
namespace Masterminds\HTML5\Parser;
3
4
use Masterminds\HTML5\Entities;
5
6
/**
7
 * Manage entity references.
8
 *
9
 * This is a simple resolver for HTML5 character reference entitites.
10
 * See \Masterminds\HTML5\Entities for the list of supported entities.
11
 */
12
class CharacterReference
13
{
14
15
    protected static $numeric_mask = array(
16
        0x0,
17
        0x2FFFF,
18
        0,
19
        0xFFFF
20
    );
21
22
    /**
23
     * Given a name (e.g.
24
     * 'amp'), lookup the UTF-8 character ('&')
25
     *
26
     * @param string $name
27
     *            The name to look up.
28
     * @return string The character sequence. In UTF-8 this may be more than one byte.
29
     */
30 12
    public static function lookupName($name)
31
    {
32
        // Do we really want to return NULL here? or FFFD
33 12
        return isset(Entities::$byName[$name]) ? Entities::$byName[$name] : null;
34
    }
35
36
    /**
37
     * Given a Unicode codepoint, return the UTF-8 character.
38
     *
39
     * (NOT USED ANYWHERE)
40
     */
41
    /*
42
     * public static function lookupCode($codePoint) { return 'POINT'; }
43
     */
44
45
    /**
46
     * Given a decimal number, return the UTF-8 character.
47
     */
48 4
    public static function lookupDecimal($int)
49
    {
50 4
        $entity = '&#' . $int . ';';
51
        // UNTESTED: This may fail on some planes. Couldn't find full documentation
52
        // on the value of the mask array.
53 4
        return mb_decode_numericentity($entity, static::$numeric_mask, 'utf-8');
54
    }
55
56
    /**
57
     * Given a hexidecimal number, return the UTF-8 character.
58
     */
59 3
    public static function lookupHex($hexdec)
60
    {
61 3
        return static::lookupDecimal(hexdec($hexdec));
62
    }
63
}
64