Completed
Pull Request — master (#491)
by Richard
14:12 queued 03:48
created

DropDownButton::render()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 23
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 17
c 1
b 0
f 0
nc 16
nop 1
dl 0
loc 23
rs 8.5906
1
<?php
2
/*
3
 You may not change or alter any portion of this comment or credits
4
 of supporting developers from this source code or any supporting source code
5
 which is considered copyrighted (c) material of the original comment or credit authors.
6
7
 This program is distributed in the hope that it will be useful,
8
 but WITHOUT ANY WARRANTY; without even the implied warranty of
9
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
10
 */
11
12
namespace Xoops\Html\Menu\Render;
13
14
use Xoops\Html\Menu\Header;
15
use Xoops\Html\Menu\Item;
16
use Xoops\Html\Menu\ItemList;
17
18
/**
19
 * DropDownButton - render a button dropdown menu
20
 *
21
 * @category  Xoops\Html\Menu\Render
22
 * @package   DropDownButton
23
 * @author    Richard Griffith <[email protected]>
24
 * @copyright 2016 XOOPS Project (http://xoops.org)
25
 * @license   GNU GPL 2 or later (http://www.gnu.org/licenses/gpl-2.0.html)
26
 * @link      http://xoops.org
27
 */
28
class DropDownButton extends RenderAbstract
29
{
30
    /**
31
     * render menu from ItemList
32
     *
33
     * @param ItemList $menu menu items
34
     *
35
     * @return string rendered HTML for menu
36
     */
37
    public function render(ItemList $menu)
38
    {
39
        $dropdown = $menu->get('dropdown', 'dropdown');
40
        $renderedMenu = "<div class=\"{$dropdown}\">\n";
41
        $class = $menu->get('class', 'btn btn-default dropdown-toggle');
42
        $id = ($menu->has('id')) ? ' id="' . $menu->get('id') . '"' : '';
43
        $labeledId = ($menu->has('id')) ? ' aria-labelledby="' . $menu->get('id') . '"' : '';
44
        $caption = $menu->get('caption', '');
45
        $icon = $menu->has('icon') ? '<span class="' . $menu->get('icon') . '" aria-hidden="true"></span> ' : '';
46
        $renderedMenu .= <<<EOT
47
<button class="{$class}" type="button"{$id} data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
48
{$icon}{$caption} <span class="caret"></span>
49
</button>
50
  <ul class="dropdown-menu"{$labeledId}">
51
EOT;
52
53
        foreach ($menu['items'] as $item) {
54
            $renderedMenu .= $this->renderItem($item);
55
        }
56
57
        $renderedMenu .= "  </ul>\n</div>\n";
58
        return $renderedMenu;
59
    }
60
61
    /**
62
     * render items, call recursively to handle ItemList, skip unsupported types
63
     *
64
     * @param Item $item Item to render
65
     *
66
     * @return string
67
     */
68 View Code Duplication
    protected function renderItem(Item $item) {
69
        $renderedItems = '';
70
        $type = $item->get('type', 'error');
71
        switch ($type) {
72
            case Item::TYPE_LINK;
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
73
                $anchorStart = '';
74
                $anchorEnd = '';
75
                $liClass = ' class="active"';
76
                if ($item->has('link')) {
77
                    $anchorStart = '<a href="' . $this->xoops->url($item->get('link')) . '">';
78
                    $anchorEnd = '</a>';
79
                    $liClass = '';
80
                }
81
                $caption = $item->get('caption', '');
82
                $icon = $item->has('icon') ? '<span class="' . $item->get('icon') . '" aria-hidden="true"></span> ' : '';
83
                $renderedItems .= "<li{$liClass}>{$anchorStart}{$icon}{$caption}{$anchorEnd}</li>";
84
                break;
85
            case Item::TYPE_LIST;
0 ignored issues
show
Coding Style introduced by
CASE statements must be defined using a colon

As per the PSR-2 coding standard, case statements should not be wrapped in curly braces. There is no need for braces, since each case is terminated by the next break.

switch ($expr) {
    case "A": { //wrong
        doSomething();
        break;
    }
    case "B": //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
86
                foreach ($item['items'] as $listItem) {
87
                    $renderedItems .= $this->renderItem($listItem);
88
                }
89
                break;
90
            case Item::TYPE_DIVIDER:
91
                $renderedItems .= '<li role="separator" class="divider"></li>';
92
                break;
93
            default;
0 ignored issues
show
Coding Style introduced by
DEFAULT statements must be defined using a colon

As per the PSR-2 coding standard, default statements should not be wrapped in curly braces.

switch ($expr) {
    default: { //wrong
        doSomething();
        break;
    }
}

switch ($expr) {
    default: //right
        doSomething();
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
94
                break;
95
        }
96
        return $renderedItems;
97
    }
98
}
99
/*
100
<div class="dropdown">
101
  <button class="btn btn-default dropdown-toggle" type="button" id="dropdownMenu1" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
102
    Dropdown
103
    <span class="caret"></span>
104
  </button>
105
  <ul class="dropdown-menu" aria-labelledby="dropdownMenu1">
106
    <li><a href="#">Action</a></li>
107
    <li><a href="#">Another action</a></li>
108
    <li><a href="#">Something else here</a></li>
109
    <li role="separator" class="divider"></li>
110
    <li><a href="#">Separated link</a></li>
111
  </ul>
112
</div>
113
*/
114