Completed
Push — master ( 723848...f6a9f3 )
by Yaro
05:45
created

Breadcrumbs::valid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace Yaro\Jarboe\ViewComponents\Breadcrumbs;
4
5
class Breadcrumbs implements BreadcrumbsInterface
6
{
7
    private $position = 0;
8
    private $crumbs = [];
9
10
    public function add(Crumb $crumb): BreadcrumbsInterface
11
    {
12
        array_push($this->crumbs, $crumb);
13
14
        return $this;
15
    }
16
17
    public function isEmptyForListPage(): bool
18
    {
19
        /** @var Crumb $crumb */
20
        foreach ($this->crumbs as $crumb) {
21
            if ($crumb->shouldBeShownOnListPage()) {
22
                return false;
23
            }
24
        }
25
26
        return true;
27
    }
28
29
    public function isEmptyForCreatePage(): bool
30
    {
31
        /** @var Crumb $crumb */
32
        foreach ($this->crumbs as $crumb) {
33
            if ($crumb->shouldBeShownOnCreatePage()) {
34
                return false;
35
            }
36
        }
37
38
        return true;
39
    }
40
41
    public function isEmptyForEditPage(): bool
42
    {
43
        /** @var Crumb $crumb */
44
        foreach ($this->crumbs as $crumb) {
45
            if ($crumb->shouldBeShownOnEditPage()) {
46
                return false;
47
            }
48
        }
49
50
        return true;
51
    }
52
53
    /**
54
     * Return the current element
55
     * @link https://php.net/manual/en/iterator.current.php
56
     * @return mixed Can return any type.
57
     * @since 5.0.0
58
     */
59
    public function current()
60
    {
61
        return $this->crumbs[$this->key()];
62
    }
63
64
    /**
65
     * Move forward to next element
66
     * @link https://php.net/manual/en/iterator.next.php
67
     * @return void Any returned value is ignored.
68
     * @since 5.0.0
69
     */
70
    public function next()
71
    {
72
        $this->position++;
73
    }
74
75
    /**
76
     * Return the key of the current element
77
     * @link https://php.net/manual/en/iterator.key.php
78
     * @return mixed scalar on success, or null on failure.
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use integer.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
79
     * @since 5.0.0
80
     */
81
    public function key()
82
    {
83
        return $this->position;
84
    }
85
86
    /**
87
     * Checks if current position is valid
88
     * @link https://php.net/manual/en/iterator.valid.php
89
     * @return bool The return value will be casted to boolean and then evaluated.
90
     * Returns true on success or false on failure.
91
     * @since 5.0.0
92
     */
93
    public function valid()
94
    {
95
        return isset($this->crumbs[$this->key()]);
96
    }
97
98
    /**
99
     * Rewind the Iterator to the first element
100
     * @link https://php.net/manual/en/iterator.rewind.php
101
     * @return void Any returned value is ignored.
102
     * @since 5.0.0
103
     */
104
    public function rewind()
105
    {
106
        $this->position = 0;
107
    }
108
}
109