Completed
Push — master ( b1115e...998ae3 )
by ignace nyamagana
22:33 queued 07:16
created

DecimalNumber::startsWith()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 7
c 0
b 0
f 0
nc 4
nop 1
dl 0
loc 14
rs 10
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\Label;
15
16
final class DecimalNumber implements LabelGenerator
17
{
18
    /**
19
     * @var int
20
     */
21
    private $int;
22
23
    /**
24
     * New instance.
25
     */
26
    public function __construct(int $int = 1)
27
    {
28
        if (0 >= $int) {
29
            $int = 1;
30
        }
31
32
        $this->int = $int;
33
    }
34
35
    /**
36
     * {@inheritdoc}
37
     */
38
    public function generate(int $nbLabels): \Iterator
39
    {
40
        if (0 >= $nbLabels) {
41
            return;
42
        }
43
44
        $count = 0;
45
        $end = $this->int + $nbLabels;
46
        $value = $this->int;
47
        while ($value < $end) {
48
            yield $count => $this->format((string) $value);
49
50
            ++$count;
51
            ++$value;
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function format(string $label): string
59
    {
60
        return $label;
61
    }
62
63
    /**
64
     * Returns the starting Letter.
65
     */
66
    public function startingAt(): int
67
    {
68
        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
    public function startsWith(int $int): self
78
    {
79
        if (0 >= $int) {
80
            $int = 1;
81
        }
82
83
        if ($int === $this->int) {
84
            return $this;
85
        }
86
87
        $clone = clone $this;
88
        $clone->int = $int;
89
90
        return $clone;
91
    }
92
}
93