Breadcrumbs::offsetSet()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
c 0
b 0
f 0
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
1
<?php
2
namespace BreadcrumbsBundle\Model;
3
4
/**
5
 * Class Breadcrumbs
6
 * @package BreadcrumbsBundle\Model
7
 */
8
class Breadcrumbs implements \Iterator, \ArrayAccess, \Countable
9
{
10
    /**
11
     * @var Item[]
12
     */
13
    protected $items = [];
14
15
    /**
16
     * @param string $text
17
     * @param string|null $url
18
     * @return Breadcrumbs
19
     */
20
    public function addItem(string $text, string $url = null): Breadcrumbs
21
    {
22
        $item = (new Item())
23
            ->setText($text)
24
            ->setUrl($url);
25
26
        $this->items[] = $item;
27
28
        return $this;
29
    }
30
31
    /**
32
     * @param mixed $offset
33
     * @return bool
34
     */
35
    public function offsetExists($offset): bool
36
    {
37
        return isset($this->items[$offset]);
38
    }
39
40
    /**
41
     * @param mixed $offset
42
     * @return mixed
43
     */
44
    public function offsetGet($offset)
45
    {
46
        return $this->offsetExists($offset) ? $this->items[$offset] : null;
47
    }
48
49
    /**
50
     * @param mixed $offset
51
     * @param mixed $value
52
     */
53
    public function offsetSet($offset, $value): void
54
    {
55
        if (!$value instanceof Item) {
56
            throw new \LogicException('Invalid value type');
57
        }
58
59
        $this->items[$offset] = $value;
60
    }
61
62
    /**
63
     * @param mixed $offset
64
     */
65
    public function offsetUnset($offset): void
66
    {
67
        unset($this->items[$offset]);
68
    }
69
70
    /**
71
     * @return int
72
     */
73
    public function count(): int
74
    {
75
        return \count($this->items);
76
    }
77
78
    /**
79
     * @return Item
80
     */
81
    public function current(): Item
82
    {
83
        return current($this->items);
84
    }
85
86
    /**
87
     * @return void
88
     */
89
    public function next(): void
90
    {
91
        next($this->items);
92
    }
93
94
    /**
95
     * @return int|null
96
     */
97
    public function key(): ?int
98
    {
99
        return key($this->items);
100
    }
101
102
    /**
103
     * @return bool
104
     */
105
    public function valid(): bool
106
    {
107
        return $this->key() !== null;
108
    }
109
110
    /**
111
     * @return void
112
     */
113
    public function rewind(): void
114
    {
115
        reset($this->items);
116
    }
117
}