Completed
Pull Request — master (#4)
by James Ekow Abaka
03:06 queued 36s
created

MenuHelper::__toString()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 31
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 17
nc 3
nop 0
dl 0
loc 31
ccs 16
cts 16
cp 1
crap 4
rs 9.7
c 0
b 0
f 0
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\honam\TemplateRenderer;
39
use ntentan\utils\Input;
40
41
/**
42
 * Standard menu widget which ships with the menu widget.
43
 */
44
class MenuHelper extends Helper
45
{
46
    private $items = array();
47
    private $cssClasses = [
48
        'menu' => [],
49
        'item' => [],
50
        'selected' => [],
51
        'matched' => []
52
    ];
53
    private $currentUrl;
54
    private $alias;
55
    private $hasLinks = true;
56
    
57
    const MENU = 'menu';
58
    const ITEM = 'item';
59
    const SELECTED_ITEM = 'selected';
60
    const MATCHED_ITEM = 'matched';
61
    
62 4
    public function __construct(TemplateRenderer $templateRenderer)
63
    {
64 4
        parent::__construct($templateRenderer);
65 4
        $this->setCurrentUrl(Input::server('REQUEST_URI'));
66 4
    }    
67
    
68 4
    public function help($items = null)
69
    {
70 4
        $this->items = $items;
71 4
        return $this;
72
    }
73
    
74 1
    public function addCssClass($class, $section = self::MENU)
75
    {
76 1
        $this->cssClasses[$section][] = $class;
77 1
        return $this;
78
    }
79
    
80 1
    public function setAlias($alias)
81
    {
82 1
        $this->alias = $alias;
83 1
        return $this;
84
    }
85
    
86 4
    public function setCurrentUrl($currentUrl)
87
    {
88 4
        $this->currentUrl = $currentUrl;
89 4
        return $this;
90
    }
91
    
92 1
    public function setHasLinks($hasLinks)
93
    {
94 1
        $this->hasLinks = $hasLinks;
95 1
        return $this;
96
    }
97
    
98 4
    public function __toString()
99
    {
100 4
        $menuItems = array();
101
        
102 4
        foreach($this->items as $index => $item)
103
        {
104 4
            if(is_string($item) || is_numeric($item))
105
            {
106
                $item = array(
107 4
                    'label' => $item,
108 4
                    'url' => $this->makeFullUrl(strtolower(str_replace(' ', '_', $item))),
109
                    'default' => null
110
                );
111
            }
112
                        
113 4
            $item['selected'] = (
114 4
                $item['url'] == substr($this->currentUrl, 0, strlen($item['url']))
115
            );
116
            
117 4
            $item['fully_matched'] = $item['url'] == $this->currentUrl;
118
            
119 4
            $menuItems[$index] = $item;
120
        }
121
        
122 4
        return $this->templateRenderer->render(
123 4
            "{$this->alias}_menu.tpl.php",
124
            [
125 4
                'items' => $menuItems,
126 4
                'css_classes' => $this->cssClasses,
127 4
                'has_links' => $this->hasLinks,
128 4
                'alias' => $this->alias 
129
            ]
130
        );
131
    }
132
}
133