1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace EaselDrawing\Templates\Builders; |
4
|
|
|
|
5
|
|
|
use EaselDrawing\Align; |
6
|
|
|
use EaselDrawing\Color; |
7
|
|
|
use EaselDrawing\ElementInterface; |
8
|
|
|
use EaselDrawing\Elements\Label as Element; |
9
|
|
|
use EaselDrawing\Font; |
10
|
|
|
use EaselDrawing\Templates\Template; |
11
|
|
|
use EaselDrawing\Templates\Utilities; |
12
|
|
|
use EaselDrawing\TextBackground; |
13
|
|
|
|
14
|
|
|
class Label extends AbstractBuilder |
15
|
|
|
{ |
16
|
|
|
/** @var string */ |
17
|
|
|
private $content = ''; |
18
|
|
|
|
19
|
|
|
/** @var Color */ |
20
|
|
|
private $color; |
21
|
|
|
|
22
|
|
|
/** @var Font */ |
23
|
|
|
private $font; |
24
|
|
|
|
25
|
|
|
/** @var float */ |
26
|
|
|
private $fontSize = 12.0; |
27
|
|
|
|
28
|
|
|
/** @var Align */ |
29
|
|
|
private $align; |
30
|
|
|
|
31
|
|
|
/** @var TextBackground */ |
32
|
|
|
private $textBackground; |
33
|
|
|
|
34
|
|
|
public function getContent(): string |
35
|
|
|
{ |
36
|
|
|
return $this->content; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
public function getColor() : Color |
40
|
|
|
{ |
41
|
|
|
if (null === $this->color) { |
42
|
|
|
throw new \RuntimeException("Color property has not been set"); |
43
|
|
|
} |
44
|
|
|
return $this->color; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
public function getFont(): Font |
48
|
|
|
{ |
49
|
|
|
if (null === $this->font) { |
50
|
|
|
throw new \RuntimeException("Font property has not been set"); |
51
|
|
|
} |
52
|
|
|
return $this->font; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function getFontSize(): float |
56
|
|
|
{ |
57
|
|
|
return $this->fontSize; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function getAlign(): Align |
61
|
|
|
{ |
62
|
|
|
if (null === $this->align) { |
63
|
|
|
throw new \RuntimeException("Align property has not been set"); |
64
|
|
|
} |
65
|
|
|
return $this->align; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function getTextBackground(): TextBackground |
69
|
|
|
{ |
70
|
|
|
if (null === $this->textBackground) { |
71
|
|
|
throw new \RuntimeException("Text background property has not been set"); |
72
|
|
|
} |
73
|
|
|
return $this->textBackground; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function configure(array $data, Template $template) |
77
|
|
|
{ |
78
|
|
|
parent::configure($data, $template); |
79
|
|
|
|
80
|
|
|
// content |
81
|
|
View Code Duplication |
if (! isset($data['content']) || ! is_string($data['content'])) { |
|
|
|
|
82
|
|
|
$data['content'] = ''; |
83
|
|
|
} |
84
|
|
|
$this->content = $data['content']; |
85
|
|
|
|
86
|
|
|
// color |
87
|
|
|
if (isset($data['color'])) { |
88
|
|
|
$this->color = Utilities::interpretColor($data['color'], $template->getForeground()); |
89
|
|
|
} else { |
90
|
|
|
$this->color = $template->getForeground(); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
// font |
94
|
|
View Code Duplication |
if (! isset($data['font']) || ! is_string($data['font'])) { |
|
|
|
|
95
|
|
|
$data['font'] = ''; |
96
|
|
|
} |
97
|
|
|
$this->font = $template->getFont($data['font']); |
98
|
|
|
|
99
|
|
|
// fontSize |
100
|
|
|
if (isset($data['fontsize']) && is_numeric($data['fontsize']) && $data['fontsize'] > 0.1) { |
101
|
|
|
$this->fontSize = (float) $data['fontsize']; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
// align |
105
|
|
View Code Duplication |
if (! isset($data['align']) || ! is_string($data['align'])) { |
|
|
|
|
106
|
|
|
$data['align'] = ''; |
107
|
|
|
} |
108
|
|
|
$this->align = Align::newFromString($data['align']); |
109
|
|
|
|
110
|
|
|
|
111
|
|
|
// textbackground: background |
112
|
|
|
if (isset($data['background'])) { |
113
|
|
|
$background = Utilities::interpretColor($data['background'], $template->getBackground()); |
114
|
|
|
} else { |
115
|
|
|
$background = $template->getBackground(); |
116
|
|
|
} |
117
|
|
|
// textbackground: backgroundtype |
118
|
|
View Code Duplication |
if (! isset($data['backgroundtype']) || ! is_string($data['backgroundtype'])) { |
|
|
|
|
119
|
|
|
$data['backgroundtype'] = ''; |
120
|
|
|
} |
121
|
|
|
$this->textBackground = TextBackground::newFromString($data['backgroundtype'], $background); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function build(): ElementInterface |
125
|
|
|
{ |
126
|
|
|
return new Element( |
127
|
|
|
$this->getX(), |
128
|
|
|
$this->getY(), |
129
|
|
|
$this->getWidth(), |
130
|
|
|
$this->getHeight(), |
131
|
|
|
$this->getContent(), |
132
|
|
|
$this->getFont(), |
133
|
|
|
$this->getFontSize(), |
134
|
|
|
$this->getColor(), |
135
|
|
|
$this->getAlign(), |
136
|
|
|
$this->getTextBackground() |
137
|
|
|
); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.