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