Skip to content

Commit 0dce867

Browse files
committed
Compiler factory; update docs
1 parent 28e566f commit 0dce867

File tree

3 files changed

+55
-11
lines changed

3 files changed

+55
-11
lines changed

README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ You can of course also manually edit your composer.json file
4242

4343
> Some TSPL2-like printers, such as Atol BP41/Rongta RP410, do not support all TSPL2 features.
4444
45-
##### Read data from printer
45+
#### Read data from printer
4646

4747
```php
4848
use PhpAidc\LabelPrinter\Printer;
@@ -51,17 +51,19 @@ use PhpAidc\LabelPrinter\Connector\NetworkConnector;
5151
$printer = new Printer(new NetworkConnector('192.168.x.x'));
5252

5353
\var_dump($printer->ask('? VERSION$(0)'));
54+
5455
// "Direct Protocol 10.15.017559 \r\n"
5556
```
5657

57-
##### Create and print label
58+
#### Create and print label
5859
```php
5960
use PhpAidc\LabelPrinter\Enum\Unit;
6061
use PhpAidc\LabelPrinter\Enum\Anchor;
6162
use PhpAidc\LabelPrinter\Enum\Charset;
6263
use PhpAidc\LabelPrinter\Printer;
6364
use PhpAidc\LabelPrinter\Label\Label;
6465
use PhpAidc\LabelPrinter\Label\Element;
66+
use PhpAidc\LabelPrinter\CompilerFactory;
6567
use PhpAidc\LabelPrinter\Connector\NetworkConnector;
6668

6769
$label = Label::create(Unit::MM(), 43, 25)
@@ -70,10 +72,10 @@ $label = Label::create(Unit::MM(), 43, 25)
7072
->add(Element::barcode(10, 10, '123456', 'CODE93')->height(60))
7173
;
7274

73-
(new Printer(new NetworkConnector('192.168.x.x')))->print($label);
75+
(new Printer(new NetworkConnector('192.168.x.x'), CompilerFactory::tspl()))->print($label);
7476
```
7577

76-
##### Add elements only for a specific language
78+
#### Add elements only for a specific language
7779
```php
7880
use PhpAidc\LabelPrinter\Label\Label;
7981
use PhpAidc\LabelPrinter\Label\Element;
@@ -90,7 +92,7 @@ $label = Label::create()
9092
;
9193
```
9294

93-
##### Add elements if some value is truthy
95+
#### Add elements if some value is truthy
9496
```php
9597
use PhpAidc\LabelPrinter\Label\Label;
9698
use PhpAidc\LabelPrinter\Label\Element;
@@ -105,7 +107,7 @@ $label = Label::create()
105107
;
106108
```
107109

108-
##### Print images
110+
#### Print images
109111
```php
110112
use PhpAidc\LabelPrinter\Label\Label;
111113
use PhpAidc\LabelPrinter\Label\Element;
@@ -130,7 +132,7 @@ $label = Label::create()
130132
;
131133
```
132134

133-
##### Print text with emulation
135+
#### Print text with emulation
134136
```php
135137
use PhpAidc\LabelPrinter\Label\Label;
136138
use PhpAidc\LabelPrinter\Label\Element;
@@ -142,7 +144,7 @@ $label = Label::create()
142144
```
143145
Text will be drawn with Imagick and printed as bitmap.
144146

145-
##### Specify the number of copies
147+
#### Specify the number of copies
146148
```php
147149
use PhpAidc\LabelPrinter\Label\Label;
148150
use PhpAidc\LabelPrinter\Label\Element;
@@ -153,20 +155,21 @@ $label = Label::create()
153155
;
154156
```
155157

156-
##### Batch printing
158+
#### Batch printing
157159
```php
158160
use PhpAidc\LabelPrinter\Printer;
159161
use PhpAidc\LabelPrinter\Label\Batch;
160162
use PhpAidc\LabelPrinter\Label\Label;
161163
use PhpAidc\LabelPrinter\Label\Element;
164+
use PhpAidc\LabelPrinter\CompilerFactory;
162165
use PhpAidc\LabelPrinter\Connector\NetworkConnector;
163166

164167
$batch = (new Batch())
165168
->add(Label::create()->add(Element::textLine(168, 95, 'Hello!', 'Univers', 8)))
166169
->add(Label::create()->add(Element::textLine(168, 95, 'Bye!', 'Univers', 8)))
167170
;
168171

169-
(new Printer(new NetworkConnector('192.168.x.x')))->print($label);
172+
(new Printer(new NetworkConnector('192.168.x.x'), CompilerFactory::fingerprint()))->print($label);
170173
```
171174

172175
## License

src/CompilerFactory.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Appwilio 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;
16+
17+
use PhpAidc\LabelPrinter\Language\Fingerprint;
18+
use PhpAidc\LabelPrinter\Language\Tspl;
19+
20+
class CompilerFactory
21+
{
22+
/**
23+
* @param float $density
24+
*
25+
* @return Compiler
26+
*/
27+
public static function fingerprint(float $density): Compiler
28+
{
29+
return new Compiler(new Fingerprint($density));
30+
}
31+
32+
/**
33+
* @return Compiler
34+
*/
35+
public static function tspl(): Compiler
36+
{
37+
return new Compiler(new Tspl());
38+
}
39+
}

src/Printer.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ public function __construct(Connector $connector, ?Compiler $compiler = null)
3434
public function print(Job $job): void
3535
{
3636
if ($this->compiler === null) {
37-
throw new \DomainException();
37+
throw new \DomainException(
38+
'The Printer object should be constructed with Compiler instance for printing.'
39+
);
3840
}
3941

4042
$this->connector->write($this->compiler->compile($job));

0 commit comments

Comments
 (0)