LatinLetter   A
last analyzed

Complexity

Total Complexity 11

Size/Duplication

Total Lines 80
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 11
eloc 23
c 1
b 0
f 0
dl 0
loc 80
ccs 30
cts 30
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A generate() 0 12 3
A startingAt() 0 3 1
A format() 0 3 1
A startsWith() 0 11 2
A filterLetter() 0 12 3
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
use function preg_match;
17
use function trim;
18
19
final class LatinLetter implements LabelGenerator
20
{
21
    /**
22
     * @var string
23
     */
24
    private $str;
25
26
    /**
27
     * New instance.
28
     */
29 60
    public function __construct(string $str = 'A')
30
    {
31 60
        $this->str = $this->filterLetter($str);
32 60
    }
33
34 60
    public function filterLetter(string $str): string
35
    {
36 60
        $str = trim($str);
37 60
        if ('' === $str) {
38 12
            return '0';
39
        }
40
41 48
        if (1 !== preg_match('/^[A-Za-z]+$/', $str)) {
42 6
            return 'A';
43
        }
44
45 42
        return $str;
46
    }
47
48
    /**
49
     * {@inheritdoc}
50
     */
51 6
    public function format(string $label): string
52
    {
53 6
        return $label;
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 51
    public function generate(int $nbLabels): \Iterator
60
    {
61 51
        if (0 >= $nbLabels) {
62 12
            $nbLabels = 0;
63
        }
64
65 51
        $count = 0;
66 51
        $letter = $this->str;
67 51
        while ($count < $nbLabels) {
68 42
            yield $count => $letter++;
69
70 42
            ++$count;
71
        }
72 51
    }
73
74
    /**
75
     * Returns the starting Letter.
76
     */
77 3
    public function startingAt(): string
78
    {
79 3
        return $this->str;
80
    }
81
82
    /**
83
     * Return an instance with the starting Letter.
84
     *
85
     * This method MUST retain the state of the current instance, and return
86
     * an instance that contains the starting Letter.
87
     */
88 3
    public function startsWith(string $str): self
89
    {
90 3
        $str = $this->filterLetter($str);
91 3
        if ($str === $this->str) {
92 3
            return $this;
93
        }
94
95 3
        $clone = clone $this;
96 3
        $clone->str = $str;
97
98 3
        return $clone;
99
    }
100
}
101