BreadcrumbItem::extra()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Arcanedev\Breadcrumbs\Entities;
6
7
use Illuminate\Support\{Arr, Fluent};
8
9
/**
10
 * Class     BreadcrumbItem
11
 *
12
 * @author   ARCANEDEV <[email protected]>
13
 *
14
 * @property  string  title
15
 * @property  string  url
16
 * @property  bool    first
17
 * @property  bool    last
18
 */
19
class BreadcrumbItem extends Fluent
20
{
21
    /* -----------------------------------------------------------------
22
     |  Constructor
23
     | -----------------------------------------------------------------
24
     */
25
26
    /**
27
     * Create a breadcrumb item instance.
28
     *
29
     * @param  array|object  $attributes
30
     */
31 66
    public function __construct(array $attributes = [])
32
    {
33 66
        $keys = ['title', 'url', 'icon'];
34
35 66
        parent::__construct(Arr::only($attributes, $keys) + [
36 66
            'extra' => Arr::except($attributes, $keys)
37
        ]);
38
39 66
        $this->resetPosition();
40 66
    }
41
42
    /* -----------------------------------------------------------------
43
     |  Getters & Setters
44
     | -----------------------------------------------------------------
45
     */
46
47
    /**
48
     * Get the title.
49
     *
50
     * @return string|null
51
     */
52 12
    public function getTitle()
53
    {
54 12
        return $this->get('title');
55
    }
56
57
    /**
58
     * Get the url.
59
     *
60
     * @return string|null
61
     */
62 12
    public function getUrl()
63
    {
64 12
        return $this->get('url');
65
    }
66
67
    /**
68
     * Set as first item.
69
     *
70
     * @return $this
71
     */
72 42
    public function setFirst()
73
    {
74 42
        $this->attributes['first'] = true;
75
76 42
        return $this;
77
    }
78
79
    /**
80
     * Set as last item.
81
     *
82
     * @return $this
83
     */
84 42
    public function setLast()
85
    {
86 42
        $this->attributes['last'] = true;
87
88 42
        return $this;
89
    }
90
91
    /**
92
     * Get the extra attribute.
93
     *
94
     * @param  string  $key
95
     * @param  mixed   $default
96
     *
97
     * @return mixed
98
     */
99 6
    public function extra($key, $default = null)
100
    {
101 6
        return Arr::get($this->attributes['extra'], $key, $default);
102
    }
103
104
    /* -----------------------------------------------------------------
105
     |  Main Methods
106
     | -----------------------------------------------------------------
107
     */
108
109
    /**
110
     * Make a breadcrumb item instance.
111
     *
112
     * @param  string  $title
113
     * @param  string  $url
114
     * @param  array   $data
115
     *
116
     * @return $this
117
     */
118 48
    public static function make($title, $url, array $data = [])
119
    {
120 48
        return new self($data + compact('title', 'url'));
121
    }
122
123
    /**
124
     * Reset position.
125
     *
126
     * @return $this
127
     */
128 66
    public function resetPosition()
129
    {
130 66
        $this->attributes['first'] = false;
131 66
        $this->attributes['last']  = false;
132
133 66
        return $this;
134
    }
135
136
    /* -----------------------------------------------------------------
137
     |  Check Methods
138
     | -----------------------------------------------------------------
139
     */
140
141
    /**
142
     * Check is first item.
143
     *
144
     * @return bool
145
     */
146 18
    public function isFirst(): bool
147
    {
148 18
        return $this->get('first', false);
149
    }
150
151
    /**
152
     * Check is last item.
153
     *
154
     * @return bool
155
     */
156 18
    public function isLast(): bool
157
    {
158 18
        return $this->get('last', false);
159
    }
160
}
161