Completed
Push — master ( 26495e...cf8e72 )
by Nathan
02:19
created

Escaper::setEscapeHtmlAttr()   A

Complexity

Conditions 1
Paths 1

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 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace NatePage\EasyHtmlElement;
4
5
use Zend\Escaper\Escaper as BaseEscaper;
6
7
class Escaper extends BaseEscaper implements EscaperInterface
8
{
9
    /** @var array The special escaping types */
10
    private $specialEscapingTypes = array('script', 'style');
11
12
    /** @var bool Determine if html is escaped or not */
13
    private $escapeHtml = true;
14
15
    /** @var bool Determine if html attributes are escaped or not */
16
    private $escapeHtmlAttr = true;
17
18
    /** @var bool Determine if javascript is escaped or not */
19
    private $escapeJs = true;
20
21
    /** @var bool Determine if css is escaped or not */
22
    private $escapeCss = true;
23
24
    /** @var bool Determine if urls are escaped or not */
25
    private $escapeUrl = true;
26
27
    /**
28
     * {@inheritdoc}
29
     */
30
    public function escapeAttributes(array $attributes)
31
    {
32
        if ($this->escapeHtmlAttr || $this->escapeUrl) {
33
            foreach ($attributes as $attr => $value) {
34
                if ('href' == $attr) {
35
                    if ($this->escapeUrl) {
36
                        $value = $this->escapeUrl($value);
37
                    }
38
                } else {
39
                    if ($this->escapeHtmlAttr) {
40
                        $value = $this->escapeHtmlAttr($value);
41
                    }
42
                }
43
44
                $attributes[$attr] = $value;
45
            }
46
        }
47
48
        return $attributes;
49
    }
50
51
    /**
52
     * {@inheritdoc}
53
     */
54
    public function escape(ElementInterface $element)
55
    {
56
        if ($this->escapeHtml && !in_array($element->getType(), $this->specialEscapingTypes)) {
57
            $element->setText($this->escapeHtml($element->getText()));
58
        }
59
60
        $element->setAttributes($this->escapeAttributes($element->getAttributes()));
61
62
        if ($this->escapeJs && 'script' == $element->getType()) {
63
            $element->setText($this->escapeJs($element->getText()));
64
        }
65
66
        if ($this->escapeCss && 'style' == $element->getType()) {
67
            $element->setText($this->escapeCss($element->getText()));
68
        }
69
70
        return $element;
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function isEscapeHtml()
77
    {
78
        return $this->escapeHtml;
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function setEscapeHtml($escapeHtml = true)
85
    {
86
        $this->escapeHtml = $escapeHtml;
87
88
        return $this;
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function isEscapeHtmlAttr()
95
    {
96
        return $this->escapeHtmlAttr;
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function setEscapeHtmlAttr($escapeHtmlAttr = true)
103
    {
104
        $this->escapeHtmlAttr = $escapeHtmlAttr;
105
106
        return $this;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function isEscapeJs()
113
    {
114
        return $this->escapeJs;
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120
    public function setEscapeJs($escapeJs = true)
121
    {
122
        $this->escapeJs = $escapeJs;
123
124
        return $this;
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function isEscapeCss()
131
    {
132
        return $this->escapeCss;
133
    }
134
135
    /**
136
     * {@inheritdoc}
137
     */
138
    public function setEscapeCss($escapeCss = true)
139
    {
140
        $this->escapeCss = $escapeCss;
141
142
        return $this;
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148
    public function isEscapeUrl()
149
    {
150
        return $this->escapeUrl;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function setEscapeUrl($escapeUrl = true)
157
    {
158
        $this->escapeUrl = $escapeUrl;
159
160
        return $this;
161
    }
162
}
163