Bootstrap3Navbar   A
last analyzed

Complexity

Total Complexity 16

Size/Duplication

Total Lines 159
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 16
c 1
b 0
f 0
lcom 1
cbo 1
dl 0
loc 159
ccs 52
cts 52
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
C open() 0 37 7
A menu() 0 7 2
A button() 0 10 2
A search() 0 8 2
A text() 0 6 2
A close() 0 4 1
1
<?php
2
3
namespace BootPress\Bootstrap;
4
5
class Bootstrap3Navbar extends Bootstrap3
6
{
7
    
8
    /**
9
     * This will open a new navbar.
10
     * 
11
     * @param mixed  $brand    The name of your website. If this is a string then it will automatically link to your ``$page->url['base']``. If you want to override that, then make this an ``array($brand, $link)``.
12
     * @param string $align    Either '**top**', '**bottom**', or '**static**' if you want to fix the alignment. If you are just trying to get to the next arg then you can declare '**inverse**' here and we will know what you are talking about.
13
     * @param mixed  $inverse  If this is anything but false (eg. '****inverse**') then we will display the inverted or alternate navbar.
14
     * 
15
     * @return string
16
     * 
17
     * ```php
18
     * echo $bp->navbar->open($ci->blog->get('name'), 'top', 'inverse');
19
     * ```
20
     */
21 1
    public function open($brand, $align = '', $inverse = false)
22
    {
23 1
        if (is_array($brand)) {
24 1
            list($brand, $link) = each($brand);
25 1
        } else {
26 1
            $link = $this->page->url['base'];
27
        }
28 1
        $id = $this->page->id('navbar');
29 1
        $class = 'navbar';
30
        switch ($align) {
31 1
            case 'top':
32 1
            case 'bottom':
33 1
                $class .= ' navbar-fixed-'.$align;
34 1
                break;
35 1
            case 'static':
36 1
                $class .= ' navbar-static-top';
37 1
                break;
38 1
            case 'inverse':
39 1
                $inverse = 'inverse';
40 1
                break;
41
        }
42 1
        $class .= ($inverse !== false) ? ' navbar-inverse' : ' navbar-default';
43 1
        $html = '<nav class="'.$class.'">';
44 1
        $html .= '<div class="container-fluid">';
45 1
        $html .= '<div class="navbar-header">';
46 1
        $html .= $this->page->tag('button', array(
47 1
                        'type' => 'button',
48 1
                        'class' => 'navbar-toggle collapsed',
49 1
                        'data-toggle' => 'collapse',
50 1
                        'data-target' => '#'.$id,
51 1
                    ), '<span class="sr-only">Toggle navigation</span>'.str_repeat('<span class="icon-bar"></span>', 3));
52 1
        $html .= "\n\t".$this->page->tag('a', array('class' => 'navbar-brand', 'href' => $link), $brand);
53 1
        $html .= '</div>';
54 1
        $html .= '<div class="collapse navbar-collapse" id="'.$id.'">';
55
56 1
        return "\n".$html;
57
    }
58
59
    /**
60
     * This will create a menu of links across your navbar.
61
     * 
62
     * @param array $links    An ``array($name => $href, ...)`` of links. If ``$href`` is an array unto itself, then it will be turned into a dropdown menu with the same header and divider rules applied as with buttons.
63
     * @param array $options  The options available here are:
64
     * 
65
     * - '**active**' => $name, $href, 'url', 'urlquery', or number (starting from 1)
66
     * - '**disabled**' => $name or $href or number (starting from 1)
67
     * - '**pull**' => 'left' (default) or 'right'
68
     * 
69
     * @return string
70
     * 
71
     * ```php
72
     * echo $bp->navbar->menu(array(
73
     *     'Home' => '#',
74
     *     'Work' => '#',
75
     *     'Dropdown' => array(
76
     *         'Action' => '#',
77
     *         'More' => '#',
78
     *     ),
79
     * ), array('active'=>'Home'));
80
     * ```
81
     */
82 1
    public function menu(array $links, array $options = array()) // array('active'=>'name or url', 'pull'=>'left or right')
83
    {
84 1
        $align = (isset($options['pull'])) ? ' navbar-'.$options['pull'] : '';
85 1
        unset($options['pull']);
86
87 1
        return "\n\t".'<ul class="nav navbar-nav'.$align.'">'.$this->links('li', $links, $options).'</ul>';
88
    }
89
90
    /**
91
     * This is used in exactly the same manner as $bp->button(...), except that it will be added to the navbar, and receive the navbar-btn class.
92
     * 
93
     * @param string $class 
94
     * @param string $name 
95
     * @param array  $options  
96
     * 
97
     * @return string
98
     */
99 1
    public function button($class, $name, array $options = array())
100
    {
101 1
        $class .= ' navbar-btn';
102 1
        if (isset($options['pull'])) {
103 1
            $class .= ' navbar-'.$options['pull'];
104 1
        }
105 1
        unset($options['pull']);
106
107 1
        return "\n\t".parent::button($class, $name, $options);
108
    }
109
110
    /**
111
     * This is used in exactly the same manner as ``$bp->search(...)``, but modified slightly for use in a navbar.
112
     * 
113
     * @param string $url   The same as it's parent.
114
     * @param array  $form  The same as it's parent, except for the default '**class**' which is '**navbar-form navbar-right**'.
115
     * 
116
     * @return string
117
     * 
118
     * ```php
119
     * echo $bp->navbar->search($page->url());
120
     * ```
121
     */
122 1
    public function search($url, array $form = array())
123
    {
124 1
        if (!isset($form['class'])) {
125 1
            $form['class'] = 'navbar-form navbar-right';
126 1
        }
127
128 1
        return "\n\t".parent::search($url, $form);
129
    }
130
131
    /**
132
     * This adds a string of text to the navbar. It will be wrapped in a ``<p>`` tag, and any ``<a>``'s it finds will also receive their special class treatment.
133
     * 
134
     * @param string $string  The message you would like to get across.
135
     * @param string $pull    Either '**left**' or '**right**'.
136
     * 
137
     * @return string
138
     * 
139
     * ```php
140
     * echo $bp->navbar->text('You <a href="#">link</a> me');
141
     * ```
142
     */
143 1
    public function text($string, $pull = false)
144
    {
145 1
        $align = (in_array($pull, array('left', 'right'))) ? ' navbar-'.$pull : '';
146
147 1
        return "\n\t".'<p class="navbar-text'.$align.'">'.$this->addClass($string, array('a' => 'navbar-link')).'</p>';
148
    }
149
150
    /**
151
     * This method will add the final ``<div>``'s to your navbar.
152
     * 
153
     * @return string
154
     * 
155
     * ```php
156
     * echo $bp->navbar->close();
157
     * ```
158
     */
159 1
    public function close()
160
    {
161 1
        return "</div></div>\n</nav>";
162
    }
163
}
164