| Total Complexity | 15 |
| Total Lines | 78 |
| Duplicated Lines | 0 % |
| Coverage | 100% |
| Changes | 0 | ||
| 1 | <?php |
||
| 17 | class Escape |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * Sanitize. |
||
| 21 | * |
||
| 22 | * @param mixed $value |
||
| 23 | */ |
||
| 24 | 7 | public function sanitize(&$value) |
|
| 25 | { |
||
| 26 | 7 | $value = $this->htmlEscape($value); |
|
| 27 | 7 | } |
|
| 28 | |||
| 29 | /** |
||
| 30 | * Return numerical part of the HTML encoding of the Unicode character. |
||
| 31 | * |
||
| 32 | * @param string $char |
||
| 33 | * @return int |
||
| 34 | */ |
||
| 35 | 7 | private function ordutf8(string $char) : int |
|
| 36 | { |
||
| 37 | 7 | $code = ord(substr($char, 0, 1)); |
|
|
|
|||
| 38 | |||
| 39 | 7 | if ($code > 127) { |
|
| 40 | |||
| 41 | //110xxxxx |
||
| 42 | 3 | $bytes = 2; |
|
| 43 | 3 | $count = 0; |
|
| 44 | |||
| 45 | 3 | if ($code > 223) { |
|
| 46 | //1110xxxx |
||
| 47 | 2 | $bytes = 3; |
|
| 48 | 2 | $count = 32; |
|
| 49 | } |
||
| 50 | |||
| 51 | 3 | if ($code > 239) { |
|
| 52 | //11110xxx |
||
| 53 | 1 | $bytes = 4; |
|
| 54 | 1 | $count = 48; |
|
| 55 | } |
||
| 56 | |||
| 57 | 3 | $temp = $code - 192 - $count; |
|
| 58 | |||
| 59 | 3 | for ($i = 1; $i < $bytes; $i++) { |
|
| 60 | 3 | $code = $temp = $temp * 64 + ord(substr($char, $i, 1)) - 128; |
|
| 61 | } |
||
| 62 | } |
||
| 63 | |||
| 64 | 7 | return $code; |
|
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Convert char to html entities. |
||
| 69 | * |
||
| 70 | * @param string $string |
||
| 71 | * @return string |
||
| 72 | */ |
||
| 73 | 7 | private function htmlEscape(string $string) : string |
|
| 95 | } |
||
| 96 | } |
||
| 97 |