Completed
Push — dev ( 72cd7e...c5a23f )
by James Ekow Abaka
05:21
created

MenuHelper   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 91
Duplicated Lines 0 %

Coupling/Cohesion

Components 2
Dependencies 2

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 2
dl 0
loc 91
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A help() 0 5 1
A addCssClass() 0 5 1
A setAlias() 0 5 1
A setCurrentUrl() 0 5 1
A setHasLinks() 0 5 1
A __toString() 0 34 4
1
<?php
2
/**
3
 * Source file for the menu widget
4
 * 
5
 * Ntentan Framework
6
 * Copyright (c) 2010-2012 James Ekow Abaka Ainooson
7
 * 
8
 * Permission is hereby granted, free of charge, to any person obtaining
9
 * a copy of this software and associated documentation files (the
10
 * "Software"), to deal in the Software without restriction, including
11
 * without limitation the rights to use, copy, modify, merge, publish,
12
 * distribute, sublicense, and/or sell copies of the Software, and to
13
 * permit persons to whom the Software is furnished to do so, subject to
14
 * the following conditions:
15
 * 
16
 * The above copyright notice and this permission notice shall be
17
 * included in all copies or substantial portions of the Software.
18
 * 
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 
26
 * 
27
 * @category Widgets
28
 * @author James Ainooson <[email protected]>
29
 * @copyright 2010-2012 James Ainooson
30
 * @license MIT
31
 */
32
33
34
namespace ntentan\honam\engines\php\helpers;
35
36
use ntentan\honam\engines\php\Helper;
37
38
use ntentan\utils\Input;
39
40
/**
41
 * Standard menu widget which ships with the menu widget.
42
 */
43
class MenuHelper extends Helper
44
{
45
    private $items = array();
46
    private $cssClasses = [
47
        'menu' => [],
48
        'item' => [],
49
        'selected' => [],
50
        'matched' => []
51
    ];
52
    private $currentUrl;
53
    private $alias;
54
    private $hasLinks = true;
55
    
56
    const MENU = 'menu';
57
    const ITEM = 'item';
58
    const SELECTED_ITEM = 'selected';
59
    const MATCHED_ITEM = 'matched';
60
    
61
    public function __construct()
62
    {
63
        \ntentan\honam\TemplateEngine::appendPath(
64
            __DIR__ . "/../../templates/menu"
65
        );
66
        $this->setCurrentUrl(Input::server('REQUEST_URI'));
67
    }    
68
    
69
    public function help($items = null)
70
    {
71
        $this->items = $items;
72
        return $this;
73
    }
74
    
75
    public function addCssClass($class, $section = self::MENU)
76
    {
77
        $this->cssClasses[$section][] = $class;
78
        return $this;
79
    }
80
    
81
    public function setAlias($alias)
82
    {
83
        $this->alias = $alias;
84
        return $this;
85
    }
86
    
87
    public function setCurrentUrl($currentUrl)
88
    {
89
        $this->currentUrl = $currentUrl;
90
        return $this;
91
    }
92
    
93
    public function setHasLinks($hasLinks)
94
    {
95
        $this->hasLinks = $hasLinks;
96
        return $this;
97
    }
98
    
99
    public function __toString()
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
100
    {
101
        $menuItems = array();
102
        
103
        foreach($this->items as $index => $item)
104
        {
105
            if(is_string($item) || is_numeric($item))
106
            {
107
                $item = array(
108
                    'label' => $item,
109
                    'url' => $this->makeFullUrl(strtolower(str_replace(' ', '_', $item))),
110
                    'default' => null
111
                );
112
            }
113
                        
114
            $item['selected'] = (
115
                $item['url'] == substr($this->currentUrl, 0, strlen($item['url']))
116
            );
117
            
118
            $item['fully_matched'] = $item['url'] == $this->currentUrl;
119
            
120
            $menuItems[$index] = $item;
121
        }
122
        
123
        return \ntentan\honam\TemplateEngine::render(
124
            "{$this->alias}_menu.tpl.php",
125
            [
126
                'items' => $menuItems,
127
                'css_classes' => $this->cssClasses,
128
                'has_links' => $this->hasLinks,
129
                'alias' => $this->alias 
130
            ]
131
        );
132
    }
133
}
134