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_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
|
24 |
|
public function __construct(LabelGenerator $labelGenerator, string $prefix = '', string $suffix = '') |
39
|
|
|
{ |
40
|
24 |
|
$this->labelGenerator = $labelGenerator; |
41
|
24 |
|
$this->prefix = $this->filterString($prefix); |
42
|
24 |
|
$this->suffix = $this->filterString($suffix); |
43
|
24 |
|
} |
44
|
|
|
|
45
|
24 |
|
private function filterString(string $str): string |
46
|
|
|
{ |
47
|
24 |
|
return (string) preg_replace("/[\r\n]/", '', $str); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
18 |
|
public function generate(int $nbLabels): \Iterator |
54
|
|
|
{ |
55
|
18 |
|
foreach ($this->labelGenerator->generate($nbLabels) as $key => $label) { |
56
|
15 |
|
yield $key => $this->prefix.$label.$this->suffix; |
57
|
|
|
} |
58
|
18 |
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
3 |
|
public function format(string $label): string |
64
|
|
|
{ |
65
|
3 |
|
return $this->prefix.$this->labelGenerator->format($label).$this->suffix; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Returns the suffix. |
70
|
|
|
*/ |
71
|
3 |
|
public function suffix(): string |
72
|
|
|
{ |
73
|
3 |
|
return $this->suffix; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* Returns the prefix. |
78
|
|
|
*/ |
79
|
3 |
|
public function prefix(): string |
80
|
|
|
{ |
81
|
3 |
|
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
|
21 |
|
public function withSuffix(string $suffix): self |
91
|
|
|
{ |
92
|
21 |
|
$suffix = $this->filterString($suffix); |
93
|
21 |
|
if ($suffix === $this->suffix) { |
94
|
12 |
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
9 |
|
$clone = clone $this; |
98
|
9 |
|
$clone->suffix = $suffix; |
99
|
|
|
|
100
|
9 |
|
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
|
21 |
|
public function withPrefix(string $prefix): self |
110
|
|
|
{ |
111
|
21 |
|
$prefix = $this->filterString($prefix); |
112
|
21 |
|
if ($prefix === $this->prefix) { |
113
|
6 |
|
return $this; |
114
|
|
|
} |
115
|
|
|
|
116
|
15 |
|
$clone = clone $this; |
117
|
15 |
|
$clone->prefix = $prefix; |
118
|
|
|
|
119
|
15 |
|
return $clone; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|