Escape::__construct()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 6
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 3
nc 2
nop 1
1
<?php
2
namespace Tuum\Form\Data;
3
4
class Escape
5
{
6
    /**
7
     * @var callable
8
     */
9
    private $escape = ['Tuum\Form\Data\Escape', 'htmlSafe'];
10
11
    /**
12
     * @param null|callable $escape
13
     */
14
    public function __construct($escape = null)
15
    {
16
        if (is_callable($escape)) {
17
            $this->escape = $escape;
18
        }
19
    }
20
21
    /**
22
     * escape for html output.
23
     *
24
     * @param string $string
25
     * @return string
26
     */
27
    public static function htmlSafe($string)
28
    {
29
        return is_string($string) ? htmlspecialchars($string, ENT_QUOTES, 'UTF-8') : $string;
30
    }
31
32
    /**
33
     * @param string $string
34
     * @return mixed
35
     */
36
    public function __invoke($string)
37
    {
38
        return $this->escape($string);
39
    }
40
41
    /**
42
     * escapes a string using $this->escape.
43
     *
44
     * @param string $string
45
     * @return mixed
46
     */
47
    public function escape($string)
48
    {
49
        if (is_string($string)) {
50
            $func = $this->escape;
51
            return $func($string);
52
        }
53
        return $string;
54
    }
55
56
    /**
57
     * @return callable
58
     */
59
    public function getEscape()
60
    {
61
        return function ($value) {
62
            return $this->escape($value);
63
        };
64
    }
65
66
    /**
67
     * @param callable $escape
68
     * @return Escape
69
     */
70
    public function setEscape($escape)
71
    {
72
        $this->escape = $escape;
73
        return $this;
74
    }
75
76
}