Completed
Push — master ( dbc7b1...93957e )
by Mihail
02:52
created

Listing::buildLink()   C

Complexity

Conditions 7
Paths 10

Size

Total Lines 34
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 34
rs 6.7272
cc 7
eloc 16
nc 10
nop 3
1
<?php
2
3
namespace Ffcms\Core\Helper\HTML;
4
5
use Ffcms\Core\Helper\HTML\System\Dom;
6
use Ffcms\Core\Helper\HTML\System\NativeGenerator;
7
use Ffcms\Core\Helper\Type\Arr;
8
use Ffcms\Core\Helper\Type\Obj;
9
use Ffcms\Core\Helper\Type\Str;
10
11
/**
12
 * Class Listing - build html listing carcase
13
 * @package Ffcms\Core\Helper\HTML
14
 */
15
class Listing extends NativeGenerator
16
{
17
18
    /**
19
     * Construct listing elements,property's and future's
20
     * @param array $elements
21
     * @return string
22
     */
23
    public static function display($elements)
24
    {
25
        // check input elements
26
        if (!Arr::in($elements['type'], ['ul', 'ol']) || count($elements['items']) < 1) {
27
            return null;
28
        }
29
30
        // initialize new DOM model
31
        $dom = new Dom();
32
        // return DOM-HTML, build based on closures!
33
        return $dom->{$elements['type']}(function () use ($dom, $elements) {
34
            // prepare output avg variable
35
            $itemHTML = null;
36
            // get active order level
37
            $orderActiveLink = false;
38
            if (isset($elements['activeOrder'])) {
39
                $orderActiveLink = $elements['activeOrder'];
40
                unset($elements['activeOrder']);
41
            }
42
43
            foreach ($elements['items'] as $item) {
44
                // sounds like dropdown array
45
                if (isset($item['dropdown']) && isset($item['items'])) {
46
                    $itemHTML .= self::buildDropdown($dom, $item);
47
                } elseif (isset($item['link'])) { // looks like link item
48
                    $itemHTML .= self::buildLink($dom, $item, $orderActiveLink);
49
                } else { // just text item
50
                    $itemHTML .= self::buildText($dom, $item);
0 ignored issues
show
Documentation introduced by
$dom is of type object<Ffcms\Core\Helper\HTML\System\Dom>, but the function expects a object<Ffcms\Core\Helper\HTML\unknown>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
51
                }
52
            }
53
            return $itemHTML;
54
        }, $elements['property']);
55
    }
56
57
    /**
58
     * Build dropdown dom element
59
     * @param Dom $dom
60
     * @param array $item
61
     */
62
    private static function buildDropdown($dom, $item)
63
    {
64
        if (!Obj::isArray($item['items'])) {
65
            return null;
66
        }
67
68
        return $dom->li(function() use($dom, $item){
69
            $dropdownLink = $dom->a(function() use ($dom, $item){
70
                return self::applyEscape($item['text'], $item['html'], $item['!secure']) . ' ' . $dom->span(function(){}, ['class' => 'caret']);
71
            }, $item['dropdown']);
72
73
            $dropdownElements = $dom->ul(function() use ($dom, $item){
74
                $resp = null;
75
                foreach ($item['items'] as $obj) {
76
                    $resp .= self::buildLink($dom, $obj, false);
77
                }
78
                return $resp;
79
            }, ['class' => 'dropdown-menu']);
80
81
            return $dropdownLink . $dropdownElements;
82
        }, $item['property']);
83
84
    }
85
86
    /**
87
     * Build text item
88
     * @param unknown $dom
89
     * @param unknown $item
90
     */
91
    private static function buildText($dom, $item)
92
    {
93
        $text = self::applyEscape($item['text'], $item['html'], $item['!secure']);
94
        return $dom->li(function() use ($text){
95
            return $text;
96
        }, $item['property']);
97
    }
98
99
    /**
100
     * Build link with active definition in listing
101
     * @param Dom $dom
102
     * @param array $item
103
     * @param string $orderActiveLink
104
     */
105
    private static function buildLink($dom, $item, $orderActiveLink = false)
106
    {
107
        // set default link data - text and properties
108
        $text = self::applyEscape($item['text'], $item['html'], $item['!secure']);
109
        $properties = $item['property'];
110
111
        // try to parse link format for controller/action definition (must be array: 'main/index' to ['main/index'])
112
        if (!Obj::isArray($item['link']) && !Str::startsWith('http', $item['link']) && !Str::startsWith('#', $item['link'])) {
113
            $item['link'] = [$item['link']];
114
        }
115
116
        // if its a controller/action definition try to work with active class
117
        if (Obj::isArray($item['link'])) {
118
            // check if css class for active item is defined
119
            if (!isset($item['activeClass'])) {
120
                $item['activeClass'] = 'active';
121
            }
122
123
            // check if element is active on current URI
124
            if (self::isCurrentLink($item['link'], $item['activeOn'], $orderActiveLink) === true) {
0 ignored issues
show
Bug introduced by
It seems like $orderActiveLink defined by parameter $orderActiveLink on line 105 can also be of type string; however, Ffcms\Core\Helper\HTML\S...erator::isCurrentLink() does only seem to accept boolean, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
125
                $properties['class'] = Str::concat(' ', $item['activeClass'], $properties['class']);
126
            }
127
        }
128
129
        // set href source for link
130
        $item['linkProperty']['href'] = self::convertLink($item['link']);
131
132
        // build output <li@params><a @params>@text</li>
133
        return $dom->li(function () use ($dom, $text, $item) {
134
            return $dom->a(function() use ($text) {
135
                return $text;
136
            }, $item['linkProperty']);
137
        }, $properties);
138
    }
139
}