Completed
Pull Request — 2.x (#43)
by jake
02:49 queued 50s
created

Scripts::endInternal()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 9
nc 2
nop 0
crap 2
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 1
        $attr = $this->escaper->attr(array(
36
            'src' => $src,
37 1
            'type' => 'text/javascript',
38
        ));
39 1
        $tag = "<script $attr></script>";
40
        $this->addElement($pos, $tag);
41
42 1
        return $this;
43 2
    }
44
45
    /**
46
     *
47
     * Adds a conditional `<!--[if ...]><script><![endif] -->` tag to the
48
     * stack.
49
     *
50
     * @param string $cond The conditional expression for the script.
51
     *
52
     * @param string $src The source href for the script.
53
     *
54
     * @param string $pos The script position in the stack.
55
     *
56
     * @return null
57
     *
58
     */
59 1
    public function addCond($cond, $src, $pos = 100)
60
    {
61
        $cond = $this->escaper->html($cond);
62 1
        $attr = $this->escaper->attr(array(
63
            'src' => $src,
64 1
            'type' => 'text/javascript',
65
        ));
66 1
        $tag = "<!--[if $cond]><script $attr></script><![endif]-->";
67
        $this->addElement($pos, $tag);
68
69 1
        return $this;
70 1
    }
71
72
    /**
73
     * Adds internal script
74
     *
75
     * @param mixed $script The script
76
     * @param int   $pos    The script position in the stack.
77
     *
78
     * @return Scripts
79
     *
80
     * @access public
81
     */
82 1
    public function addInternal($script, $pos = 100)
83
    {
84
        $attr = $this->escaper->attr(array(
85
            'type' => 'text/javascript'
86
        ));
87
        $tag = "<script $attr>$script</script>";
88
        $this->addElement($pos, $tag);
89
        return $this;
90 1
    }
91
92
    /**
93
     * addCondInternal
94
     *
95
     * @param mixed $cond   The conditional expression for the script.
96
     * @param mixed $script The script
97
     * @param int   $pos    The script position in the stack.
98
     *
99
     * @return mixed
100
     * @throws exceptionclass [description]
101
     *
102
     * @access public
103
     */
104 1
    public function addCondInternal($cond, $script, $pos = 100)
105
    {
106
        $cond = $this->escaper->html($cond);
107
        $attr = $this->escaper->attr(array(
108
            'type' => 'text/javascript',
109
        ));
110
        $tag = "<!--[if $cond]><script $attr>$script</script><![endif]-->";
111
        $this->addElement($pos, $tag);
112
113
        return $this;
114 1
    }
115
116
    /**
117
     * Adds internal script
118
     *
119
     * @param int $pos The script position in the stack.
120
     *
121
     * @return Scripts
122
     *
123
     * @access public
124
     */
125 1
    public function beginInternal($pos = 100)
126
    {
127 1
        $this->capture[] = $pos;
128
         ob_start();
129
    }
130
131
    /**
132
     * beginInternalCond
133
     *
134
     * @param mixed $cond DESCRIPTION
135
     * @param int   $pos  DESCRIPTION
136
     *
137
     * @return mixed
138
     *
139
     * @access public
140
     */
141 1
    public function beginCondInternal($cond, $pos = 100)
142
    {
143 1
        $this->capture[] = array($cond, $pos);
144
        ob_start();
145
    }
146
147
    /**
148
     * endInternal
149
     *
150
     * @return mixed
151
     *
152
     * @access public
153
     */
154 1
    public function endInternal()
155
    {
156
        $script = ob_get_clean();
157
        $params = array_pop($this->capture);
158
        if (is_array($params)) {
159 1
            return $this->addCondInternal(
160 1
                $params[0],
161
                $script,
162 1
                $params[1]
163
            );
164
        }
165
        return $this->addInternal($script, $params);
166
    }
167
}
168