Completed
Pull Request — 2.x (#50)
by jake
04:13
created

Scripts::attr()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 2

Importance

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