1 | <?php |
||
23 | class HtmlEscaper extends AbstractEscaper |
||
24 | { |
||
25 | /** |
||
26 | * |
||
27 | * Flags for `htmlspecialchars()`. |
||
28 | * |
||
29 | * @var mixed |
||
30 | * |
||
31 | */ |
||
32 | protected $flags = ENT_QUOTES; |
||
33 | |||
34 | /** |
||
35 | * |
||
36 | * Constructor. |
||
37 | * |
||
38 | * @param int $flags Flags for `htmlspecialchars()`. |
||
39 | * |
||
40 | * @param string $encoding The encoding to use for raw and escaped strings. |
||
41 | * |
||
42 | */ |
||
43 | 114 | public function __construct($flags = null, $encoding = null) |
|
44 | { |
||
45 | 114 | if ($flags !== null) { |
|
46 | // use custom flags only |
||
47 | 1 | $this->setFlags($flags); |
|
48 | 114 | } elseif (defined('ENT_SUBSTITUTE')) { |
|
49 | // add ENT_SUBSTITUTE if available (PHP 5.4) |
||
50 | $this->setFlags(ENT_QUOTES | ENT_SUBSTITUTE); |
||
51 | } |
||
52 | |||
53 | 114 | parent::__construct($encoding); |
|
54 | 114 | } |
|
55 | |||
56 | /** |
||
57 | * |
||
58 | * Escapes an HTML value. |
||
59 | * |
||
60 | * @param mixed $raw The value to be escaped. |
||
61 | * |
||
62 | * @return mixed The escaped value. |
||
63 | * |
||
64 | */ |
||
65 | 78 | public function __invoke($raw) |
|
66 | { |
||
67 | // pre-empt escaping |
||
68 | 78 | if ($raw === '' || ctype_digit($raw)) { |
|
69 | 2 | return $raw; |
|
70 | } |
||
71 | |||
72 | // return the escaped string |
||
73 | 78 | return htmlspecialchars( |
|
74 | 78 | $raw, |
|
75 | 78 | $this->flags, |
|
76 | 78 | $this->encoding |
|
77 | 78 | ); |
|
78 | } |
||
79 | |||
80 | /** |
||
81 | * |
||
82 | * Sets the flags for `htmlspecialchars()`. |
||
83 | * |
||
84 | * @param int $flags The flags for `htmlspecialchars()`. |
||
85 | * |
||
86 | * @return null |
||
87 | * |
||
88 | */ |
||
89 | 3 | public function setFlags($flags) |
|
93 | |||
94 | /** |
||
95 | * |
||
96 | * Gets the flags for `htmlspecialchars()`. |
||
97 | * |
||
98 | * @return int |
||
99 | * |
||
100 | */ |
||
101 | 3 | public function getFlags() |
|
105 | } |
||
106 |