Completed
Push — master ( 15f051...29343b )
by Oleg
05:33
created

Builder::group()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 22
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 22
ccs 13
cts 14
cp 0.9286
rs 9.2
cc 3
eloc 13
nc 2
nop 3
crap 3.0032
1
<?php
2
3
namespace Malezha\Menu\Entity;
4
5
use Illuminate\Contracts\Container\Container;
6
use Illuminate\Support\Collection;
7
use Illuminate\Contracts\View\Factory as ViewFactory;
8
use Malezha\Menu\Contracts\Builder as BuilderContract;
9
use Malezha\Menu\Traits\HasAttributes;
10
11
class Builder implements BuilderContract
12
{
13
    use HasAttributes;
14
15
    /**
16
     * @var Container
17
     */
18
    protected $container;
19
20
    /**
21
     * @var Collection
22
     */
23
    protected $items;
24
25
    /**
26
     * @var string
27
     */
28
    protected $name;
29
30
    /**
31
     * @var string
32
     */
33
    protected $type;
34
35
    /**
36
     * @var array
37
     */
38
    protected $activeAttributes;
39
40
    /**
41
     * @param Container $container
42
     * @param string $name
43
     * @param string $type
44
     * @param array $attributes
45
     * @param array $activeAttributes
46
     */
47 14
    function __construct(Container $container, $name, $type = self::UL, $attributes = [], $activeAttributes = [])
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
48
    {
49 14
        $this->container = $container;
50 14
        $this->name = $name;
51 14
        $this->type = $type;
52 14
        $this->attributes = new Attributes($attributes);
53 14
        $this->items = new Collection();
54 14
        $this->activeAttributes = new Attributes($activeAttributes);
0 ignored issues
show
Documentation Bug introduced by
It seems like new \Malezha\Menu\Entity...utes($activeAttributes) of type object<Malezha\Menu\Entity\Attributes> is incompatible with the declared type array of property $activeAttributes.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
55 14
    }
56
57
    /**
58
     * @param string $name
59
     * @param callable $itemCallable
60
     * @param callable $menuCallable
61
     * @return Group
62
     */
63 2
    public function group($name, $itemCallable, $menuCallable)
64
    {
65 2
        if (is_callable($itemCallable) && is_callable($menuCallable)) {
66 2
            $item = new Item($this, $name);
67 2
            call_user_func($itemCallable, $item);
68
69 2
            $menu = $this->container->make(BuilderContract::class, [
70 2
                'container' => $this->container, 
71 2
                'name' => $name,
72 2
                'activeAttributes' => $this->activeAttributes()->all(),
73 2
            ]);
74 2
            call_user_func($menuCallable, $menu);
75
76 2
            $group = new Group($menu, $item);
77
78 2
            $this->items->put($name, $group);
79
80 2
            return $group;
81
        }
82
83
        throw new \InvalidArgumentException('Arguments must be callable');
84
    }
85
86
    /**
87
     * @param string $name
88
     * @param string $title
89
     * @param string $url
90
     * @param array $attributes
91
     * @param array $linkAttributes
92
     * @param callable|null $callback
93
     * @return Item
94
     */
95 6
    public function add($name, $title, $url, $attributes = [], $linkAttributes = [], $callback = null)
96
    {
97 6
        $item = new Item($this, $name, $attributes, $title, $url, $linkAttributes);
98
99 6
        if (is_callable($callback)) {
100 2
            call_user_func($callback, $item);
101 2
        }
102
103 6
        $this->items->put($name, $item);
104
105 6
        return $item;
106
    }
107
108
    /**
109
     * @param string $name
110
     * @return bool
111
     */
112 2
    public function has($name)
113
    {
114 2
        return $this->items->has($name);
115
    }
116
117
    /**
118
     * @param string $name
119
     * @return Item|Group
120
     */
121 2
    public function get($name)
122
    {
123 2
        return $this->items->get($name);
124
    }
125
126
    /**
127
     * @return array
128
     */
129 3
    public function all()
130
    {
131 3
        return $this->items->all();
132
    }
133
134
    /**
135
     * @param string $name
136
     */
137 1
    public function forget($name)
138
    {
139 1
        $this->items->forget($name);
140 1
    }
141
142
    /**
143
     * @return string
144
     */
145 3
    public function getType()
146
    {
147 3
        return $this->type;
148
    }
149
150
    /**
151
     * @param string $type
152
     */
153 1
    public function setType($type)
154
    {
155 1
        $this->type = (string)$type;
156 1
    }
157
158
    /**
159
     * @param string|null $view
160
     * @return string
161
     */
162 2
    public function render($view = null)
163
    {
164 2
        $view = (empty($view)) ? config('menu.view') : $view;
165 2
        $minify = config('menu.minify', false);
166
        
167
        /* @var ViewFactory $viewFactory */
168 2
        $viewFactory = $this->container->make(ViewFactory::class);
169
170 2
        $rendered = $viewFactory->make($view, [
171 2
            'menu' => $this,
172 2
            'renderView' => $view,
173 2
        ])->render();
174
        
175 2
        if ($minify) {
176 2
            $rendered = $this->minifyHtmlOutput($rendered);
177 2
        }
178
        
179 2
        return $rendered;
180
    }
181
182
    /**
183
     * @param callable|null $callback
184
     * @return Attributes|mixed
185
     */
186 5
    public function activeAttributes($callback = null)
187
    {
188 5
        if (is_callable($callback)) {
189 1
            return call_user_func($callback, $this->activeAttributes);
190
        }
191
192 5
        return $this->activeAttributes;
193
    }
194
195 2
    protected function minifyHtmlOutput($html)
196
    {
197
        $search = array(
198 2
            '/\>[^\S]+/s',  // strip whitespaces after tags, except space
199 2
            '/[^\S]+\</s',  // strip whitespaces before tags, except space
200
            '/(\s)+/s'       // shorten multiple whitespace sequences
201 2
        );
202
203
        $replace = array(
204 2
            '>',
205 2
            '<',
206
            '\\1'
207 2
        );
208
209 2
        return preg_replace($search, $replace, $html);
210
    }
211
}