Completed
Push — master ( b8c902...40fce1 )
by Oleg
07:28
created

Builder::minifyHtmlOutput()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 1

Importance

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