Skip to content

Commit

Permalink
Compiler factory; update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaoda committed Nov 17, 2021
1 parent 28e566f commit 0dce867
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 11 deletions.
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ You can of course also manually edit your composer.json file

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

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

\var_dump($printer->ask('? VERSION$(0)'));

// "Direct Protocol 10.15.017559 \r\n"
```

##### Create and print label
#### Create and print label
```php
use PhpAidc\LabelPrinter\Enum\Unit;
use PhpAidc\LabelPrinter\Enum\Anchor;
use PhpAidc\LabelPrinter\Enum\Charset;
use PhpAidc\LabelPrinter\Printer;
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\CompilerFactory;
use PhpAidc\LabelPrinter\Connector\NetworkConnector;

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

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

##### Add elements only for a specific language
#### Add elements only for a specific language
```php
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
Expand All @@ -90,7 +92,7 @@ $label = Label::create()
;
```

##### Add elements if some value is truthy
#### Add elements if some value is truthy
```php
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
Expand All @@ -105,7 +107,7 @@ $label = Label::create()
;
```

##### Print images
#### Print images
```php
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
Expand All @@ -130,7 +132,7 @@ $label = Label::create()
;
```

##### Print text with emulation
#### Print text with emulation
```php
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
Expand All @@ -142,7 +144,7 @@ $label = Label::create()
```
Text will be drawn with Imagick and printed as bitmap.

##### Specify the number of copies
#### Specify the number of copies
```php
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
Expand All @@ -153,20 +155,21 @@ $label = Label::create()
;
```

##### Batch printing
#### Batch printing
```php
use PhpAidc\LabelPrinter\Printer;
use PhpAidc\LabelPrinter\Label\Batch;
use PhpAidc\LabelPrinter\Label\Label;
use PhpAidc\LabelPrinter\Label\Element;
use PhpAidc\LabelPrinter\CompilerFactory;
use PhpAidc\LabelPrinter\Connector\NetworkConnector;

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

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

## License
Expand Down
39 changes: 39 additions & 0 deletions src/CompilerFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

/*
* This file is part of Appwilio LabelPrinter package.
*
* © appwilio (https://appwilio.com)
* © JhaoDa (https://github.com/jhaoda)
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace PhpAidc\LabelPrinter;

use PhpAidc\LabelPrinter\Language\Fingerprint;
use PhpAidc\LabelPrinter\Language\Tspl;

class CompilerFactory
{
/**
* @param float $density
*
* @return Compiler
*/
public static function fingerprint(float $density): Compiler
{
return new Compiler(new Fingerprint($density));
}

/**
* @return Compiler
*/
public static function tspl(): Compiler
{
return new Compiler(new Tspl());
}
}
4 changes: 3 additions & 1 deletion src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ public function __construct(Connector $connector, ?Compiler $compiler = null)
public function print(Job $job): void
{
if ($this->compiler === null) {
throw new \DomainException();
throw new \DomainException(
'The Printer object should be constructed with Compiler instance for printing.'
);
}

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

0 comments on commit 0dce867

Please sign in to comment.