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\Condition; |
22
|
|
|
use PhpAidc\LabelPrinter\Contract\Label as LabelContract; |
23
|
|
|
|
24
|
|
|
final class Label implements LabelContract |
25
|
|
|
{ |
26
|
|
|
private $media; |
27
|
|
|
|
28
|
|
|
/** @var int */ |
29
|
|
|
private $copies = 1; |
30
|
|
|
|
31
|
|
|
/** @var Charset|null */ |
32
|
|
|
private $charset; |
33
|
|
|
|
34
|
|
|
/** @var Direction|null */ |
35
|
|
|
private $direction; |
36
|
|
|
|
37
|
|
|
/** @var Command[]|Condition[] */ |
38
|
|
|
private $statements = []; |
39
|
|
|
|
40
|
7 |
|
public static function create(?Unit $unit = null, ?float $width = null, ?float $height = null): self |
41
|
|
|
{ |
42
|
7 |
|
return new self($unit, $width, $height); |
43
|
|
|
} |
44
|
|
|
|
45
|
10 |
|
public function __construct(?Unit $unit = null, ?float $width = null, ?float $height = null) |
46
|
|
|
{ |
47
|
10 |
|
if ($unit === null && ($width !== null || $height !== null)) { |
48
|
1 |
|
throw new \InvalidArgumentException(); |
49
|
|
|
} |
50
|
|
|
|
51
|
9 |
|
$this->media = \compact('unit', 'width', 'height'); |
52
|
9 |
|
} |
53
|
|
|
|
54
|
7 |
|
public function add(Command $command) |
55
|
|
|
{ |
56
|
7 |
|
$this->statements[] = $command; |
57
|
|
|
|
58
|
7 |
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
public function charset(Charset $value) |
62
|
|
|
{ |
63
|
1 |
|
$this->charset = $value; |
64
|
|
|
|
65
|
1 |
|
return $this; |
66
|
|
|
} |
67
|
|
|
|
68
|
2 |
|
public function copies(int $copies) |
69
|
|
|
{ |
70
|
2 |
|
$this->copies = $copies; |
71
|
|
|
|
72
|
2 |
|
return $this; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
public function direction(Direction $value) |
76
|
|
|
{ |
77
|
1 |
|
$this->direction = $value; |
78
|
|
|
|
79
|
1 |
|
return $this; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
public function getCharset(): ?Charset |
83
|
|
|
{ |
84
|
1 |
|
return $this->charset; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function getCopies(): int |
88
|
|
|
{ |
89
|
6 |
|
return $this->copies; |
90
|
|
|
} |
91
|
|
|
|
92
|
1 |
|
public function getDirection(): ?Direction |
93
|
|
|
{ |
94
|
1 |
|
return $this->direction; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getMedia(): array |
98
|
|
|
{ |
99
|
|
|
return $this->media; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function getWidth(): ?float |
103
|
|
|
{ |
104
|
|
|
return $this->media['width'] ?? null; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
public function getHeight(): ?float |
108
|
|
|
{ |
109
|
|
|
return $this->media['height'] ?? null; |
110
|
|
|
} |
111
|
|
|
|
112
|
2 |
|
public function erase() |
113
|
|
|
{ |
114
|
2 |
|
$this->statements = []; |
115
|
|
|
|
116
|
2 |
|
return $this; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Apply the callback only if the languages match. |
121
|
|
|
* |
122
|
|
|
* @param string $language |
123
|
|
|
* @param callable $callback |
124
|
|
|
* |
125
|
|
|
* @return $this |
126
|
|
|
*/ |
127
|
1 |
|
public function for(string $language, callable $callback) |
128
|
|
|
{ |
129
|
1 |
|
$this->statements[] = new LanguageCondition($language, $callback); |
130
|
|
|
|
131
|
1 |
|
return $this; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Apply the callback if the value is truthy. |
136
|
|
|
* |
137
|
|
|
* @param mixed $value |
138
|
|
|
* @param callable $callback |
139
|
|
|
* |
140
|
|
|
* @return $this |
141
|
|
|
*/ |
142
|
2 |
|
public function when($value, callable $callback) |
143
|
|
|
{ |
144
|
2 |
|
$this->statements[] = new BooleanCondition($value, $callback); |
145
|
|
|
|
146
|
2 |
|
return $this; |
147
|
|
|
} |
148
|
|
|
|
149
|
6 |
|
public function getIterator() |
150
|
|
|
{ |
151
|
6 |
|
return new \ArrayIterator($this->statements); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|