Skip to content

Commit 34e8700

Browse files
committed
Batch printing
1 parent e96423c commit 34e8700

File tree

6 files changed

+106
-13
lines changed

6 files changed

+106
-13
lines changed

src/Compiler.php

+29-9
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414

1515
namespace PhpAidc\LabelPrinter;
1616

17+
use PhpAidc\LabelPrinter\Label\Batch;
18+
use PhpAidc\LabelPrinter\Contract\Job;
1719
use PhpAidc\LabelPrinter\Contract\Label;
1820
use PhpAidc\LabelPrinter\Contract\Language;
1921

@@ -32,19 +34,17 @@ public function __construct(Language $language)
3234
$this->language = $language;
3335
}
3436

35-
public function compile(Label $label, int $copies = 1): string
37+
public function compile(Job $job): string
3638
{
37-
$instructions = [
38-
$this->language->compileDeclaration($label),
39-
];
39+
$instructions = [];
4040

41-
foreach ($label->getCommands(\get_class($this->language)) as $command) {
42-
if ($this->language->isSupport($command)) {
43-
$instructions[] = $this->language->compileCommand($command);
44-
}
41+
if ($job instanceof Label) {
42+
$instructions[] = $this->compileLabel($job);
4543
}
4644

47-
$instructions[] = $this->language->compilePrint($copies);
45+
if ($job instanceof Batch) {
46+
$instructions[] = $this->compileBatch($job);
47+
}
4848

4949
$payload = \array_reduce($instructions, static function ($carry, $item) {
5050
return $item instanceof \Generator
@@ -54,4 +54,24 @@ public function compile(Label $label, int $copies = 1): string
5454

5555
return \implode($payload);
5656
}
57+
58+
private function compileLabel(Label $label): iterable
59+
{
60+
yield from $this->language->compileDeclaration($label);
61+
62+
foreach ($label->getCommands(\get_class($this->language)) as $command) {
63+
if ($this->language->isSupport($command)) {
64+
yield from $this->language->compileCommand($command);
65+
}
66+
}
67+
68+
yield from $this->language->compilePrint($label->getCopies());
69+
}
70+
71+
private function compileBatch(Batch $batch): iterable
72+
{
73+
foreach ($batch->getLabels() as $label) {
74+
yield from $this->compileLabel($label);
75+
}
76+
}
5777
}

src/Contract/Job.php

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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\Contract;
16+
17+
interface Job
18+
{
19+
//
20+
}

src/Contract/Label.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
use PhpAidc\LabelPrinter\Enum\Charset;
1818
use PhpAidc\LabelPrinter\Enum\Direction;
1919

20-
interface Label
20+
interface Label extends Job
2121
{
2222
public function charset(Charset $value);
2323

src/Label/Batch.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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\Label;
16+
17+
use PhpAidc\LabelPrinter\Contract\Job;
18+
use PhpAidc\LabelPrinter\Contract\Label as LabelContract;
19+
20+
final class Batch implements Job
21+
{
22+
private $labels = [];
23+
24+
public function add(LabelContract $label)
25+
{
26+
$this->labels[] = $label;
27+
28+
return $this;
29+
}
30+
31+
/**
32+
* @return LabelContract[]
33+
*/
34+
public function getLabels()
35+
{
36+
return $this->labels;
37+
}
38+
}

src/Label/Label.php

+15
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ final class Label implements LabelContract
2626

2727
private $commands = [];
2828

29+
/** @var int */
30+
private $copies = 1;
31+
2932
/** @var Charset|null */
3033
private $charset;
3134

@@ -67,6 +70,18 @@ public function charset(Charset $value)
6770
return $this;
6871
}
6972

73+
public function copies(int $copies)
74+
{
75+
$this->copies = $copies;
76+
77+
return $this;
78+
}
79+
80+
public function getCopies(): int
81+
{
82+
return $this->copies;
83+
}
84+
7085
public function getCharset(): ?Charset
7186
{
7287
return $this->charset;

src/Printer.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace PhpAidc\LabelPrinter;
1616

17-
use PhpAidc\LabelPrinter\Contract\Label;
17+
use PhpAidc\LabelPrinter\Contract\Job;
1818
use PhpAidc\LabelPrinter\Contract\Connector;
1919

2020
final class Printer
@@ -31,13 +31,13 @@ public function __construct(Connector $connector, ?Compiler $compiler = null)
3131
$this->compiler = $compiler;
3232
}
3333

34-
public function print(Label $label, int $copies = 1): void
34+
public function print(Job $job): void
3535
{
3636
if ($this->compiler === null) {
3737
throw new \DomainException();
3838
}
3939

40-
$this->connector->write($this->compiler->compile($label, $copies));
40+
$this->connector->write($this->compiler->compile($job));
4141
}
4242

4343
public function send($payload): void

0 commit comments

Comments
 (0)