Passed
Push — master ( 3a35da...a738f6 )
by Nikolaos
02:35
created

Breadcrumbs::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
1
<?php
2
3
/**
4
 * This file is part of the Phalcon Framework.
5
 *
6
 * (c) Phalcon Team <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE.txt
9
 * file that was distributed with this source code.
10
 */
11
12
declare(strict_types=1);
13
14
namespace Phalcon\Html;
15
16
use function array_keys;
17
use function end;
18
use function implode;
19
use function str_replace;
20
21
/**
22
 * Phalcon\Html\Breadcrumbs
23
 *
24
 * This component offers an easy way to create breadcrumbs for your application.
25
 * The resulting HTML when calling `render()` will have each breadcrumb enclosed
26
 * in `<dt>` tags, while the whole string is enclosed in `<dl>` tags.
27
 *
28
 * @property array  $elements
29
 * @property string $separator
30
 * @property string $template
31
 */
32
class Breadcrumbs
33
{
34
    /**
35
     * Keeps all the breadcrumbs
36
     *
37
     * @var array
38
     */
39
    private $elements = [];
40
41
    /**
42
     * Crumb separator
43
     *
44
     * @var string
45
     */
46
    private $separator = " / ";
47
48
    /**
49
     * The HTML template to use to render the breadcrumbs.
50
     *
51
     * @var string
52
     */
53
    private $template = "<dt><a href=\"{link}\">{label}</a></dt>";
54
55
    /**
56
     * Adds a new crumb.
57
     *
58
     * ```php
59
     * // Adding a crumb with a link
60
     * $breadcrumbs->add("Home", "/");
61
     *
62
     * // Adding a crumb without a link (normally the last one)
63
     * $breadcrumbs->add("Users");
64
     * ```
65
     *
66
     * @param string $label
67
     * @param string $link
68
     *
69
     * @return Breadcrumbs
70
     */
71 5
    public function add(string $label, string $link = ""): Breadcrumbs
72
    {
73 5
        $this->elements[$link] = $label;
74
75 5
        return $this;
76
    }
77
78
    /**
79
     * Clears the crumbs
80
     *
81
     * ```php
82
     * $breadcrumbs->clear()
83
     * ```
84
     */
85 1
    public function clear(): void
86
    {
87 1
        $this->elements = [];
88 1
    }
89
90
    /**
91
     * Returns the separator
92
     *
93
     * @return string
94
     */
95 1
    public function getSeparator(): string
96
    {
97 1
        return $this->separator;
98
    }
99
100
    /**
101
     * Removes crumb by url.
102
     *
103
     * ```php
104
     * $breadcrumbs->remove("/admin/user/create");
105
     *
106
     * // remove a crumb without an url (last link)
107
     * $breadcrumbs->remove();
108
     * ```
109
     *
110
     * @param string $link
111
     */
112 1
    public function remove(string $link): void
113
    {
114 1
        $elements = $this->elements;
115
116 1
        unset($elements[$link]);
117
118 1
        $this->elements = $elements;
119 1
    }
120
121
    /**
122
     * Renders and outputs breadcrumbs based on previously set template.
123
     *
124
     * ```php
125
     * echo $breadcrumbs->render();
126
     * ```
127
     */
128 1
    public function render(): string
129
    {
130 1
        $output    = [];
131 1
        $elements  = $this->elements;
132 1
        $urls      = array_keys($elements);
133 1
        $lastUrl   = end($urls);
134 1
        $lastLabel = $elements[$lastUrl];
135
136 1
        unset($elements[$lastUrl]);
137
138 1
        foreach ($elements as $url => $element) {
139 1
            $output[] = $this->getLink($element, $url);
140
        }
141
142
        /**
143
         * Check if this is the "Home" element i.e. count() = 0
144
         */
145 1
        if (0 !== count($elements)) {
146 1
            $output[] = "<dt>" . $lastLabel . "</dt>";
147
        } else {
148
            $output[] = $this->getLink($lastLabel, $lastUrl);
149
        }
150
151
        return "<dl>"
152 1
            . implode("<dt>" . $this->separator . "</dt>", $output)
153 1
            . "</dl>";
154
    }
155
156
    /**
157
     * Set the separator
158
     *
159
     * @param string $separator
160
     *
161
     * @return Breadcrumbs
162
     */
163 1
    public function setSeparator(string $separator): Breadcrumbs
164
    {
165 1
        $this->separator = $separator;
166
167 1
        return $this;
168
    }
169
170
    /**
171
     * Returns the internal breadcrumbs array
172
     */
173 4
    public function toArray(): array
174
    {
175 4
        return $this->elements;
176
    }
177
178
    /**
179
     * @param string $label
180
     * @param string $url
181
     *
182
     * @return string
183
     */
184 1
    private function getLink(string $label, string $url): string
185
    {
186 1
        return str_replace(
187
            [
188 1
                "{label}",
189
                "{link}"
190
            ],
191
            [
192 1
                $label,
193 1
                $url
194
            ],
195 1
            $this->template
196
        );
197
    }
198
}
199