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

AffixLabel::withPrefix()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 2
nop 1
dl 0
loc 11
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
use function preg_replace;
17
18
final class AffixLabel implements LabelGenerator
19
{
20
    /**
21
     * @var LabelGenerator
22
     */
23
    private $labelGenerator;
24
25
    /**
26
     * @var string
27
     */
28
    private $prefix = '';
29
30
    /**
31
     * @var string
32
     */
33
    private $suffix = '';
34
35
    /**
36
     * New instance.
37
     */
38
    public function __construct(LabelGenerator $labelGenerator, string $prefix = '', string $suffix = '')
39
    {
40
        $this->labelGenerator = $labelGenerator;
41
        $this->prefix = $this->filterString($prefix);
42
        $this->suffix = $this->filterString($suffix);
43
    }
44
45
    private function filterString(string $str): string
46
    {
47
        return (string) preg_replace("/[\r\n]/", '', $str);
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function generate(int $nbLabels): \Iterator
54
    {
55
        foreach ($this->labelGenerator->generate($nbLabels) as $key => $label) {
56
            yield $key => $this->prefix.$label.$this->suffix;
57
        }
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63
    public function format(string $label): string
64
    {
65
        return $this->prefix.$this->labelGenerator->format($label).$this->suffix;
66
    }
67
68
    /**
69
     * Returns the suffix.
70
     */
71
    public function suffix(): string
72
    {
73
        return $this->suffix;
74
    }
75
76
    /**
77
     * Returns the prefix.
78
     */
79
    public function prefix(): string
80
    {
81
        return $this->prefix;
82
    }
83
84
    /**
85
     * Return an instance with the suffix.
86
     *
87
     * This method MUST retain the state of the current instance, and return
88
     * an instance that contains the suffix.
89
     */
90
    public function withSuffix(string $suffix): self
91
    {
92
        $suffix = $this->filterString($suffix);
93
        if ($suffix === $this->suffix) {
94
            return $this;
95
        }
96
97
        $clone = clone $this;
98
        $clone->suffix = $suffix;
99
100
        return $clone;
101
    }
102
103
    /**
104
     * Return an instance with the prefix.
105
     *
106
     * This method MUST retain the state of the current instance, and return
107
     * an instance that contains the prefix.
108
     */
109
    public function withPrefix(string $prefix): self
110
    {
111
        $prefix = $this->filterString($prefix);
112
        if ($prefix === $this->prefix) {
113
            return $this;
114
        }
115
116
        $clone = clone $this;
117
        $clone->prefix = $prefix;
118
119
        return $clone;
120
    }
121
}
122