Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
Escape
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
2 / 2
4
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 filter
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2
3namespace Verja\Filter;
4
5use Verja\Filter;
6
7class Escape extends Filter
8{
9    /** @var bool */
10    protected $doubleEncode = false;
11
12    /** @var bool */
13    protected $specialChars = true;
14
15    /**
16     * Escape constructor.
17     *
18     * @param bool $doubleEncode
19     * @param bool $specialChars
20     */
21    public function __construct(bool $doubleEncode = false, bool $specialChars = true)
22    {
23        $this->doubleEncode = $doubleEncode;
24        $this->specialChars = $specialChars;
25    }
26
27    /**
28     * Filter $value
29     *
30     * @param mixed $value
31     * @param array $context
32     * @return mixed
33     */
34    public function filter($value, array $context = [])
35    {
36        if (!is_string($value)) {
37            return $value;
38        }
39
40        return $this->specialChars ?
41            htmlspecialchars($value, ENT_COMPAT, ini_get('default_charset'), $this->doubleEncode) :
42            htmlentities($value, ENT_COMPAT, ini_get('default_charset'), $this->doubleEncode);
43    }
44}