1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PhpAidc LabelPrinter package. |
5
|
|
|
* |
6
|
|
|
* © Appwilio (https://appwilio.com) |
7
|
|
|
* © JhaoDa (https://github.com/jhaoda) |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
declare(strict_types=1); |
14
|
|
|
|
15
|
|
|
namespace PhpAidc\LabelPrinter\Label; |
16
|
|
|
|
17
|
|
|
use PhpAidc\LabelPrinter\Enum\Unit; |
18
|
|
|
use PhpAidc\LabelPrinter\Enum\Charset; |
19
|
|
|
use PhpAidc\LabelPrinter\Enum\Direction; |
20
|
|
|
use PhpAidc\LabelPrinter\Contract\Command; |
21
|
|
|
use PhpAidc\LabelPrinter\Contract\Label as LabelContract; |
22
|
|
|
|
23
|
|
|
final class Label implements LabelContract |
24
|
|
|
{ |
25
|
|
|
private $media; |
26
|
|
|
|
27
|
|
|
private $commands = []; |
28
|
|
|
|
29
|
|
|
/** @var int */ |
30
|
|
|
private $copies = 1; |
31
|
|
|
|
32
|
|
|
/** @var Charset|null */ |
33
|
|
|
private $charset; |
34
|
|
|
|
35
|
|
|
/** @var Direction|null */ |
36
|
|
|
private $direction; |
37
|
|
|
|
38
|
3 |
|
public static function create(?Unit $unit = null, ?float $width = null, ?float $height = null): self |
39
|
|
|
{ |
40
|
3 |
|
return new self($unit, $width, $height); |
41
|
|
|
} |
42
|
|
|
|
43
|
5 |
|
public function __construct(?Unit $unit = null, ?float $width = null, ?float $height = null) |
44
|
|
|
{ |
45
|
5 |
|
if ($unit === null && ($width !== null || $height !== null)) { |
46
|
1 |
|
throw new \InvalidArgumentException(); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
$this->media = \compact('unit', 'width', 'height'); |
50
|
4 |
|
} |
51
|
|
|
|
52
|
3 |
|
public function add(Command $command) |
53
|
|
|
{ |
54
|
3 |
|
$this->commands[] = $command; |
55
|
|
|
|
56
|
3 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
1 |
|
public function direction(Direction $value) |
60
|
|
|
{ |
61
|
1 |
|
$this->direction = $value; |
62
|
|
|
|
63
|
1 |
|
return $this; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
public function charset(Charset $value) |
67
|
|
|
{ |
68
|
1 |
|
$this->charset = $value; |
69
|
|
|
|
70
|
1 |
|
return $this; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function copies(int $copies) |
74
|
|
|
{ |
75
|
|
|
$this->copies = $copies; |
76
|
|
|
|
77
|
|
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
3 |
|
public function getCopies(): int |
81
|
|
|
{ |
82
|
3 |
|
return $this->copies; |
83
|
|
|
} |
84
|
|
|
|
85
|
1 |
|
public function getCharset(): ?Charset |
86
|
|
|
{ |
87
|
1 |
|
return $this->charset; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
public function getMedia(): array |
91
|
|
|
{ |
92
|
|
|
return $this->media; |
93
|
|
|
} |
94
|
|
|
|
95
|
1 |
|
public function getDirection(): ?Direction |
96
|
|
|
{ |
97
|
1 |
|
return $this->direction; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function getWidth(): ?float |
101
|
|
|
{ |
102
|
|
|
return $this->media['width'] ?? null; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getHeight(): ?float |
106
|
|
|
{ |
107
|
|
|
return $this->media['height'] ?? null; |
108
|
|
|
} |
109
|
|
|
|
110
|
3 |
|
public function getCommands(string $language): iterable |
111
|
|
|
{ |
112
|
3 |
|
foreach ($this->commands as $command) { |
113
|
3 |
|
if ($command instanceof Condition) { |
114
|
1 |
|
if ($command->isMatch($language)) { |
115
|
1 |
|
foreach ($command->call((clone $this)->erase()) as $item) { |
116
|
1 |
|
yield $item; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} else { |
120
|
3 |
|
yield $command; |
121
|
|
|
} |
122
|
|
|
} |
123
|
3 |
|
} |
124
|
|
|
|
125
|
1 |
|
public function erase() |
126
|
|
|
{ |
127
|
1 |
|
$this->commands = []; |
128
|
|
|
|
129
|
1 |
|
return $this; |
130
|
|
|
} |
131
|
|
|
|
132
|
1 |
|
public function when(string $language, \Closure $closure) |
133
|
|
|
{ |
134
|
1 |
|
$this->commands[] = new Condition($language, $closure); |
135
|
|
|
|
136
|
1 |
|
return $this; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|