DecimalNumber::startingAt()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
/**
4
 * League.Period (https://period.thephpleague.com)
5
 *
6
 * (c) Ignace Nyamagana Butera <[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
declare(strict_types=1);
13
14
namespace League\Period\Chart;
15
16
final class DecimalNumber implements LabelGenerator
17
{
18
    /**
19
     * @var int
20
     */
21
    private $int;
22
23
    /**
24
     * New instance.
25
     */
26 57
    public function __construct(int $int = 1)
27
    {
28 57
        if (0 >= $int) {
29 18
            $int = 1;
30
        }
31
32 57
        $this->int = $int;
33 57
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38 36
    public function generate(int $nbLabels): \Iterator
39
    {
40 36
        if (0 >= $nbLabels) {
41 9
            return;
42
        }
43
44 27
        $count = 0;
45 27
        $end = $this->int + $nbLabels;
46 27
        $value = $this->int;
47 27
        while ($value < $end) {
48 27
            yield $count => $this->format((string) $value);
49
50 27
            ++$count;
51 27
            ++$value;
52
        }
53 27
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58 30
    public function format(string $label): string
59
    {
60 30
        return $label;
61
    }
62
63
    /**
64
     * Returns the starting Letter.
65
     */
66 6
    public function startingAt(): int
67
    {
68 6
        return $this->int;
69
    }
70
71
    /**
72
     * Return an instance with the starting Letter.
73
     *
74
     * This method MUST retain the state of the current instance, and return
75
     * an instance that contains the starting Letter.
76
     */
77 6
    public function startsWith(int $int): self
78
    {
79 6
        if (0 >= $int) {
80 6
            $int = 1;
81
        }
82
83 6
        if ($int === $this->int) {
84 6
            return $this;
85
        }
86
87 6
        $clone = clone $this;
88 6
        $clone->int = $int;
89
90 6
        return $clone;
91
    }
92
}
93