Completed
Pull Request — 2.x (#50)
by jake
01:58
created

Scripts::addAttr()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 19
ccs 11
cts 11
cp 1
rs 9.4285
cc 1
eloc 11
nc 1
nop 3
crap 1
1
<?php
2
/**
3
 *
4
 * This file is part of Aura for PHP.
5
 *
6
 * @license http://opensource.org/licenses/bsd-license.php BSD
7
 *
8
 */
9
namespace Aura\Html\Helper;
10
11
/**
12
 *
13
 * Helper for a stack of <script> tags.
14
 *
15
 * @package Aura.Html
16
 *
17
 */
18
class Scripts extends AbstractSeries
19
{
20
    private $capture;
21
22
    /**
23
     *
24
     * Adds a <script> tag to the stack.
25
     *
26
     * @param string $src The source href for the script.
27
     *
28
     * @param int $pos The script position in the stack.
29
     *
30
     * @return null
31
     *
32
     */
33 2
    public function add($src, $pos = 100)
34
    {
35 2
        $attr = $this->escaper->attr(array(
36 2
            'src' => $src,
37 2
            'type' => 'text/javascript',
38 2
        ));
39 2
        $tag = "<script $attr></script>";
40 2
        $this->addElement($pos, $tag);
41
42 2
        return $this;
43
    }
44
45
    /**
46
     *
47
     * Adds a <script> tag to the stack with additional attributes
48
     *
49
     * @param string $src The source href for the script.
50
     *
51
     * @param string $attr The additional attributes
52
     *
53
     * @param int $pos The script position in the stack.
54
     *
55
     * @return null
56
     *
57
     */
58 1
    public function addAttr($src, array $attr = null, $pos = 100)
59
    {
60 1
        $attr = (array) $attr;
61
62
        $base = array(
63 1
            'src' => $src,
64 1
            'type' => 'text/javascript',
65 1
        );
66
67 1
        unset($attr['src']);
68 1
        unset($attr['type']);
69
70 1
        $attr = $this->escaper->attr(array_merge($base, $attr));
71
72 1
        $tag = "<script $attr></script>";
73 1
        $this->addElement($pos, $tag);
74
75 1
        return $this;
76
    }
77
78
    /**
79
     *
80
     * Adds a conditional `<!--[if ...]><script><![endif] -->` tag to the
81
     * stack.
82
     *
83
     * @param string $cond The conditional expression for the script.
84
     *
85
     * @param string $src The source href for the script.
86
     *
87
     * @param string $pos The script position in the stack.
88
     *
89
     * @return null
90
     *
91
     */
92 1
    public function addCond($cond, $src, $pos = 100)
93
    {
94 1
        $cond = $this->escaper->html($cond);
95 1
        $attr = $this->escaper->attr(array(
96 1
            'src' => $src,
97 1
            'type' => 'text/javascript',
98 1
        ));
99 1
        $tag = "<!--[if $cond]><script $attr></script><![endif]-->";
100 1
        $this->addElement($pos, $tag);
101
102 1
        return $this;
103
    }
104
105
    /**
106
     * Adds internal script
107
     *
108
     * @param mixed $script The script
109
     * @param int   $pos    The script position in the stack.
110
     *
111
     * @return Scripts
112
     *
113
     * @access public
114
     */
115 1
    public function addInternal($script, $pos = 100)
116
    {
117 1
        $attr = $this->escaper->attr(array(
118
            'type' => 'text/javascript'
119 1
        ));
120 1
        $tag = "<script $attr>$script</script>";
121 1
        $this->addElement($pos, $tag);
122 1
        return $this;
123
    }
124
125
    /**
126
     * addCondInternal
127
     *
128
     * @param mixed $cond   The conditional expression for the script.
129
     * @param mixed $script The script
130
     * @param int   $pos    The script position in the stack.
131
     *
132
     * @return mixed
133
     * @throws exceptionclass [description]
134
     *
135
     * @access public
136
     */
137 1
    public function addCondInternal($cond, $script, $pos = 100)
138
    {
139 1
        $cond = $this->escaper->html($cond);
140 1
        $attr = $this->escaper->attr(array(
141 1
            'type' => 'text/javascript',
142 1
        ));
143 1
        $tag = "<!--[if $cond]><script $attr>$script</script><![endif]-->";
144 1
        $this->addElement($pos, $tag);
145
146 1
        return $this;
147
    }
148
149
    /**
150
     * Adds internal script
151
     *
152
     * @param int $pos The script position in the stack.
153
     *
154
     * @return Scripts
155
     *
156
     * @access public
157
     */
158 1
    public function beginInternal($pos = 100)
159
    {
160 1
        $this->capture[] = $pos;
161 1
         ob_start();
162 1
    }
163
164
    /**
165
     * beginInternalCond
166
     *
167
     * @param mixed $cond DESCRIPTION
168
     * @param int   $pos  DESCRIPTION
169
     *
170
     * @return mixed
171
     *
172
     * @access public
173
     */
174 1
    public function beginCondInternal($cond, $pos = 100)
175
    {
176 1
        $this->capture[] = array($cond, $pos);
177 1
        ob_start();
178 1
    }
179
180
    /**
181
     * endInternal
182
     *
183
     * @return mixed
184
     *
185
     * @access public
186
     */
187 1
    public function endInternal()
188
    {
189 1
        $script = ob_get_clean();
190 1
        $params = array_pop($this->capture);
191 1
        if (is_array($params)) {
192 1
            return $this->addCondInternal(
193 1
                $params[0],
194 1
                $script,
195 1
                $params[1]
196 1
            );
197
        }
198 1
        return $this->addInternal($script, $params);
199
    }
200
}
201