Breadcrumb::key()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Tadcka package.
5
 *
6
 * (c) Tadas Gliaubicas <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Tadcka\Component\Breadcrumbs;
13
14
/**
15
 * @author Tadas Gliaubicas <[email protected]>
16
 *
17
 * @since 10/30/14 11:20 PM
18
 */
19
class Breadcrumb implements \Iterator, \Countable
20
{
21
    /**
22
     * @var array
23
     */
24
    private $items = array();
25
26
    /**
27
     * {@inheritdoc}
28
     */
29 1
    public function current()
30
    {
31 1
        return current($this->items);
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37 1
    public function next()
38
    {
39 1
        next($this->items);
40 1
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45 2
    public function key()
46
    {
47 2
        return key($this->items);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53 2
    public function valid()
54
    {
55 2
        return isset($this->items[$this->key()]);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61 2
    public function rewind()
62
    {
63 2
        reset($this->items);
64 2
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    public function count()
70
    {
71
        return count($this->items);
72
    }
73
74
    /**
75
     * Add item.
76
77
     * @param string $title
78
     * @param string $url
79
     * @param bool $truncate
80
     */
81 2
    public function add($title, $url = null, $truncate = true)
82
    {
83 2
        $this->items[] = array(
84 2
            'title' => $truncate ? $this->truncate($title) : $title,
85 2
            'url' => $url,
86
        );
87 2
    }
88
89
    /**
90
     * @param int $key
91
     *
92
     * @return array|null
93
     */
94 1
    public function get($key)
95
    {
96 1
        return isset($this->items[$key]) ? $this->items[$key] : null;
97
    }
98
99
    /**
100
     * Truncate string.
101
     *
102
     * @param string $string
103
     * @param int $length
104
     * @param string $separator
105
     *
106
     * @return string
107
     */
108 2
    protected function truncate($string, $length = 30, $separator = '...')
109
    {
110 2
        if (mb_strlen($string) > $length) {
111
112
            return rtrim(mb_substr($string, 0, $length)) . $separator;
113
        }
114
115 2
        return $string;
116
    }
117
}
118