Passed
Pull Request — master (#53)
by
unknown
02:53
created

Breadcrumbs::run()   B

Complexity

Conditions 8
Paths 21

Size

Total Lines 31
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 8

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 8
eloc 16
c 1
b 0
f 0
nc 21
nop 0
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 8
rs 8.4444
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Yiisoft\Yii\Widgets;
6
7
use InvalidArgumentException;
8
use JsonException;
9
use Yiisoft\Html\Html;
10
use Yiisoft\Widget\Widget;
11
12
use function array_key_exists;
13
use function implode;
14
use function is_array;
15
use function strtr;
16
17
/**
18
 * Breadcrumbs displays a list of items indicating the position of the current page in the whole site hierarchy.
19
 *
20
 * For example, breadcrumbs like "Home / Sample Post / Edit" means the user is viewing an edit page for the
21
 * "Sample Post". He can click on "Sample Post" to view that page, or he can click on "Home" to return to the homepage.
22
 *
23
 * To use Breadcrumbs, you need to configure its {@see Breadcrumbs::items()} method,
24
 * which specifies the items to be displayed. For example:
25
 *
26
 * ```php
27
 * // $this is the view object currently being used
28
 * echo Breadcrumbs::widget()
29
 *     -> itemTemplate() => "<li><i>{link}</i></li>\n", // template for all links
30
 *     -> items() => [
31
 *         [
32
 *             'label' => 'Post Category',
33
 *             'url' => 'post-category/view?id=10',
34
 *             'template' => "<li><b>{link}</b></li>\n", // template for this link only
35
 *         ],
36
 *         ['label' => 'Sample Post', 'url' => 'post/edit?id=1',
37
 *         'Edit',
38
 *     ];
39
 * ```
40
 *
41
 * Because breadcrumbs usually appears in nearly every page of a website, you may consider placing it in a layout view.
42
 * You can use a view common parameter (e.g. `$this->getCommonParameter('breadcrumbs')`) to configure the items in
43
 * different views. In the layout view, you assign this view parameter to the {@see Breadcrumbs::items()} method
44
 * like the following:
45
 *
46
 * ```php
47
 * // $this is the view object currently being used
48
 * echo Breadcrumbs::widget()->items($this->getCommonParameter('breadcrumbs', []));
49
 * ```
50
 */
51
final class Breadcrumbs extends Widget
52
{
53
    private string $activeItemTemplate = "<li class=\"active\">{link}</li>\n";
54
    private array $attributes = ['class' => 'breadcrumb'];
55
    private array|null $homeItem = ['label' => 'Home', 'url' => '/'];
56
    private array $items = [];
57
    private string $itemTemplate = "<li>{link}</li>\n";
58
    private string $tag = 'ul';
59
60
    /**
61
     * Returns a new instance with the specified active item template.
62
     *
63
     * @param string $value The template used to render each active item in the breadcrumbs.
64
     * The token `{link}` will be replaced with the actual HTML link for each active item.
65
     */
66 4
    public function activeItemTemplate(string $value): self
67
    {
68 4
        $new = clone $this;
69 4
        $new->activeItemTemplate = $value;
70
71 4
        return $new;
72
    }
73
74
    /**
75
     * Returns a new instance with the HTML attributes. The following special options are recognized.
76
     *
77
     * @param array $values Attribute values indexed by attribute names.
78
     */
79 3
    public function attributes(array $values): self
80
    {
81 3
        $new = clone $this;
82 3
        $new->attributes = $values;
83
84 3
        return $new;
85
    }
86
87
    /**
88
     * Returns a new instance with the specified first item in the breadcrumbs (called home link).
89
     *
90
     * If a null is specified, the home item will not be rendered.
91
     *
92
     * @param array|null $value Please refer to {@see items()} on the format.
93
     *
94
     * @throws InvalidArgumentException If an empty array is specified.
95
     */
96 8
    public function homeItem(?array $value): self
97
    {
98 8
        if ($value === []) {
99 1
            throw new InvalidArgumentException(
100
                'The home item cannot be an empty array. To disable rendering of the home item, specify null.',
101
            );
102
        }
103
104 7
        $new = clone $this;
105 7
        $new->homeItem = $value;
106
107 7
        return $new;
108
    }
109
110
    /**
111
     * Returns a new instance with the specified list of items.
112
     *
113
     * @param array $value List of items to appear in the breadcrumbs. If this property is empty, the widget will not
114
     * render anything. Each array element represents a single item in the breadcrumbs with the following structure:
115
     *
116
     * ```php
117
     * [
118
     *     'label' => 'label of the item',  // required
119
     *     'url' => 'url of the item',      // optional
120
     *     'template' => 'own template of the item', // optional, if not set $this->itemTemplate will be used
121
     * ]
122
     * ```
123
     *
124
     * If a item is active, you only need to specify its "label", and instead of writing `['label' => $label]`, you may
125
     * simply use `$label`.
126
     *
127
     * Additional array elements for each item will be treated as the HTML attributes for the hyperlink tag.
128
     * For example, the following item specification will generate a hyperlink with CSS class `external`:
129
     *
130
     * ```php
131
     * [
132
     *     'label' => 'demo',
133
     *     'url' => 'http://example.com',
134
     *     'class' => 'external',
135
     * ]
136
     * ```
137
     *
138
     * Each individual item can override global {@see encodeLabels} param like the following:
139
     *
140
     * ```php
141
     * [
142
     *     'label' => '<strong>Hello!</strong>',
143
     *     'encode' => false,
144
     * ]
145
     * ```
146
     */
147 11
    public function items(array $value): self
148
    {
149 11
        $new = clone $this;
150 11
        $new->items = $value;
151
152 11
        return $new;
153
    }
154
155
    /**
156
     * Returns a new instance with the specified item template.
157
     *
158
     * @param string $value The template used to render each inactive item in the breadcrumbs.
159
     * The token `{link}` will be replaced with the actual HTML link for each inactive item.
160
     */
161 2
    public function itemTemplate(string $value): self
162
    {
163 2
        $new = clone $this;
164 2
        $new->itemTemplate = $value;
165
166 2
        return $new;
167
    }
168
169
    /**
170
     * Returns a new instance with the specified tag.
171
     *
172
     * @param string $value The tag name.
173
     */
174 4
    public function tag(string $value): self
175
    {
176 4
        $new = clone $this;
177 4
        $new->tag = $value;
178
179 4
        return $new;
180
    }
181
182
    /**
183
     * Renders the widget.
184
     *
185
     * @throws JsonException
186
     *
187
     * @return string The result of widget execution to be outputted.
188
     */
189 11
    protected function run(): string
190
    {
191 11
        if ($this->items === []) {
192 1
            return '';
193
        }
194
195 10
        $items = [];
196
197 10
        if ($this->homeItem !== null) {
198 5
            $items[] = $this->renderItem($this->homeItem, $this->itemTemplate);
199
        }
200
201
        /** @var mixed $item */
202 10
        foreach ($this->items as $item) {
203 10
            if (!is_array($item)) {
204 6
                $item = ['label' => $item];
205
            }
206
207 10
            if ($item !== []) {
208 10
                $items[] = $this->renderItem(
209
                    $item,
210 10
                    isset($item['url']) ? $this->itemTemplate : $this->activeItemTemplate
211
                );
212
            }
213
        }
214
215 8
        $body = implode('', $items);
216
217 8
        return empty($this->tag)
218 2
            ? $body
219 8
            : Html::normalTag($this->tag, PHP_EOL . $body, $this->attributes)->encode(false)->render();
220
    }
221
222
    /**
223
     * Renders a single breadcrumb item.
224
     *
225
     * @param array $item The item to be rendered. It must contain the "label" element. The "url" element is optional.
226
     * @param string $template The template to be used to rendered the link. The token "{link}" will be replaced by the
227
     * link.
228
     *
229
     * @throws InvalidArgumentException|JsonException if `$item` does not have "label" element.
230
     *
231
     * @return string The rendering result.
232
     */
233 10
    private function renderItem(array $item, string $template): string
234
    {
235 10
        if (!array_key_exists('label', $item)) {
236 1
            throw new InvalidArgumentException('The "label" element is required for each item.');
237
        }
238
239 9
        if (!is_string($item['label'])) {
240 1
            throw new InvalidArgumentException('The "label" element must be a string.');
241
        }
242
243
        /** @var bool */
244 9
        $encodeLabel = $item['encode'] ?? true;
245 9
        $label = $encodeLabel ? Html::encode($item['label']) : $item['label'];
246
247 9
        if (isset($item['template']) && is_string($item['template'])) {
248 1
            $template = $item['template'];
249
        }
250
251 9
        if (isset($item['url']) && is_string($item['url'])) {
252 4
            $link = $item['url'];
253 4
            unset($item['template'], $item['label'], $item['url']);
254 4
            $link = Html::a($label, $link, $item);
255
        } else {
256 8
            $link = $label;
257
        }
258
259 9
        return strtr($template, ['{link}' => $link]);
260
    }
261
}
262