Escape   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 0
dl 0
loc 73
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A htmlSafe() 0 4 2
A __invoke() 0 4 1
A escape() 0 8 2
A getEscape() 0 6 1
A setEscape() 0 5 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
}