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

AbstractList::buildItem()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3.4578

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 9
ccs 2
cts 7
cp 0.2857
rs 9.6666
cc 2
eloc 6
nc 2
nop 1
crap 3.4578
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 `<ul>` tag with `<li>` items.
14
 *
15
 * @package Aura.Html
16
 *
17
 */
18
abstract class AbstractList extends AbstractHelper
19
{
20
    /**
21
     *
22
     * Attributes for the ul tag.
23
     *
24
     * @var array
25
     *
26
     */
27
    protected $attr = array();
28
29
    /**
30
     *
31
     * The stack of HTML elements.
32
     *
33
     * @var array
34
     *
35
     */
36
    protected $stack = array();
37
38
    /**
39
     *
40
     * The generated HTML.
41
     *
42
     * @var string
43
     *
44
     */
45
    protected $html = '';
46
47
    /**
48
     *
49
     * Initializes and returns the UL object.
50
     *
51
     * @param array $attr Attributes for the UL tag.
52
     *
53
     * @return self
54
     *
55
     */
56 2
    public function __invoke(array $attr = null)
57
    {
58
        if ($attr !== null) {
59 2
            $this->attr = $attr;
60
        }
61
        return $this;
62
    }
63
64
    /**
65
     *
66
     * Generates and returns the HTML for the list.
67
     *
68
     * @return string
69
     *
70
     */
71 4
    public function __toString()
72
    {
73
        // if there is no stack of items, **do not** return an empty
74
        // <ul></ul> tag set.
75
        if (! $this->stack) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->stack of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
76 4
            return '';
77
        }
78
79
        $tag = $this->getTag();
80
        $attr = $this->escaper->attr($this->attr);
81 4
        if ($attr) {
82
            $this->html = $this->indent(0, "<{$tag} {$attr}>");
83
        } else {
84
            $this->html = $this->indent(0, "<{$tag}>");
85 2
        }
86
87 4
        foreach ($this->stack as $item) {
88
            $this->buildItem($item);
89
        }
90
91
        $html = $this->html . $this->indent(0, "</{$tag}>");
92
93 4
        $this->attr  = array();
94 4
        $this->stack = array();
95 4
        $this->html  = '';
96
97 4
        return $html;
98
    }
99
100
    /**
101
     *
102
     * Adds a single item to the stack; the text will be escaped.
103
     *
104
     * @param string $text The list item text.
105
     *
106
     * @param array $attr Attributes for the list item tag.
107
     *
108
     * @return self
109
     *
110
     */
111
    public function item($text, array $attr = array())
112
    {
113
        $this->stack[] = array(
114
            $this->escaper->html($text),
115
            $this->escaper->attr($attr),
116
        );
117
        return $this;
118
    }
119
120
    /**
121
     *
122
     * Adds multiple items to the stack; the text will be escaped.
123
     *
124
     * @param array $items An array of text, or text => attribs, for the list
125
     * items.
126
     *
127
     * @return self
128
     *
129
     */
130 2
    public function items(array $items)
131
    {
132
        foreach ($items as $key => $val) {
133
            if (is_int($key)) {
134
                $this->item($val);
135
            } else {
136
                $this->item($key, $val);
137
            }
138
        }
139 2
        return $this;
140 2
    }
141
142
    /**
143
     *
144
     * Adds a single raw item to the stack; the text will **not** be escaped.
145
     *
146
     * @param string $text The list item text.
147
     *
148
     * @param array $attr Attributes for the list item tag.
149
     *
150
     * @return self
151
     *
152
     */
153
    public function rawItem($text, array $attr = array())
154
    {
155
        $this->stack[] = array(
156
            $text,
157
            $this->escaper->attr($attr),
158
        );
159
        return $this;
160
    }
161
162
    /**
163
     *
164
     * Adds multiple raw items to the stack; the text will **not** be escaped.
165
     *
166
     * @param array $items An array of text, or text => attribs, for the list
167
     * items.
168
     *
169
     * @return self
170
     *
171
     */
172 2
    public function rawItems(array $items)
173
    {
174
        foreach ($items as $key => $val) {
175
            if (is_int($key)) {
176
                $this->rawItem($val);
177
            } else {
178
                $this->rawItem($key, $val);
179
            }
180
        }
181 2
        return $this;
182 2
    }
183
184
    /**
185
     *
186
     * Builds the HTML for a single list item.
187
     *
188
     * @param string $item The item HTML.
189
     *
190
     * @return null
191
     *
192
     */
193 4
    protected function buildItem($item)
194
    {
195
        list($html, $attr) = $item;
196
        if ($attr) {
197
            $this->html .= $this->indent(1, "<li {$attr}>$html</li>");
198
        } else {
199
            $this->html .= $this->indent(1, "<li>$html</li>");
200 4
        }
201
    }
202
203
    /**
204
     *
205
     * Returns the tag name.
206
     *
207
     * @return string
208
     *
209
     */
210
    abstract protected function getTag();
211
}
212