Completed
Push — master ( 2d084d...ce6a8c )
by ARCANEDEV
11s
created

BreadcrumbCollection::addOne()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php namespace Arcanedev\Breadcrumbs\Entities;
2
3
use Illuminate\Support\Collection;
4
5
/**
6
 * Class     BreadcrumbCollection
7
 *
8
 * @package  Arcanedev\Breadcrumbs\Entities
9
 * @author   ARCANEDEV <[email protected]>
10
 */
11
class BreadcrumbCollection extends Collection
12
{
13
    /* -----------------------------------------------------------------
14
     |  Main Methods
15
     | -----------------------------------------------------------------
16
     */
17
18
    /**
19
     * Add a breadcrumb item to collection.
20
     *
21
     * @param  string  $title
22
     * @param  string  $url
23
     * @param  array   $data
24
     *
25
     * @return self
26
     */
27 28
    public function addOne($title, $url, array $data = [])
28
    {
29 28
        return $this->addBreadcrumb(
30 28
            BreadcrumbItem::make($title, $url, $data)
31
        );
32
    }
33
34
    /**
35
     * Add a breadcrumb item to collection.
36
     *
37
     * @param  BreadcrumbItem  $breadcrumb
38
     *
39
     * @return self
40
     */
41 28
    public function addBreadcrumb(BreadcrumbItem $breadcrumb)
42
    {
43 28
        $this->push($breadcrumb);
44
45 28
        return $this->order();
46
    }
47
48
    /* -----------------------------------------------------------------
49
     |  Other Methods
50
     | -----------------------------------------------------------------
51
     */
52
53
    /**
54
     * Order all breadcrumbs items.
55
     *
56
     * @return self
57
     */
58 28
    private function order()
59
    {
60 28
        $count = $this->count();
61
62
        $this->map(function (BreadcrumbItem $crumb, $key) use ($count) {
63 28
            $crumb->resetPosition();
64
65 28
            if ($key === 0)
66 28
                $crumb->setFirst();
67
68 28
            if ($key === ($count - 1))
69 28
                $crumb->setLast();
70
71 28
            return $crumb;
72 28
        });
73
74 28
        return $this;
75
    }
76
}
77