diff --git a/src/Compiler.php b/src/Compiler.php index 4be1dd6..6ed2a28 100644 --- a/src/Compiler.php +++ b/src/Compiler.php @@ -14,6 +14,8 @@ namespace PhpAidc\LabelPrinter; +use PhpAidc\LabelPrinter\Label\Batch; +use PhpAidc\LabelPrinter\Contract\Job; use PhpAidc\LabelPrinter\Contract\Label; use PhpAidc\LabelPrinter\Contract\Language; @@ -32,19 +34,17 @@ public function __construct(Language $language) $this->language = $language; } - public function compile(Label $label, int $copies = 1): string + public function compile(Job $job): string { - $instructions = [ - $this->language->compileDeclaration($label), - ]; + $instructions = []; - foreach ($label->getCommands(\get_class($this->language)) as $command) { - if ($this->language->isSupport($command)) { - $instructions[] = $this->language->compileCommand($command); - } + if ($job instanceof Label) { + $instructions[] = $this->compileLabel($job); } - $instructions[] = $this->language->compilePrint($copies); + if ($job instanceof Batch) { + $instructions[] = $this->compileBatch($job); + } $payload = \array_reduce($instructions, static function ($carry, $item) { return $item instanceof \Generator @@ -54,4 +54,24 @@ public function compile(Label $label, int $copies = 1): string return \implode($payload); } + + private function compileLabel(Label $label): iterable + { + yield from $this->language->compileDeclaration($label); + + foreach ($label->getCommands(\get_class($this->language)) as $command) { + if ($this->language->isSupport($command)) { + yield from $this->language->compileCommand($command); + } + } + + yield from $this->language->compilePrint($label->getCopies()); + } + + private function compileBatch(Batch $batch): iterable + { + foreach ($batch->getLabels() as $label) { + yield from $this->compileLabel($label); + } + } } diff --git a/src/Contract/Job.php b/src/Contract/Job.php new file mode 100644 index 0000000..d21b8a2 --- /dev/null +++ b/src/Contract/Job.php @@ -0,0 +1,20 @@ +labels[] = $label; + + return $this; + } + + /** + * @return LabelContract[] + */ + public function getLabels() + { + return $this->labels; + } +} diff --git a/src/Label/Label.php b/src/Label/Label.php index fe678c3..2ce892b 100644 --- a/src/Label/Label.php +++ b/src/Label/Label.php @@ -26,6 +26,9 @@ final class Label implements LabelContract private $commands = []; + /** @var int */ + private $copies = 1; + /** @var Charset|null */ private $charset; @@ -67,6 +70,18 @@ public function charset(Charset $value) return $this; } + public function copies(int $copies) + { + $this->copies = $copies; + + return $this; + } + + public function getCopies(): int + { + return $this->copies; + } + public function getCharset(): ?Charset { return $this->charset; diff --git a/src/Printer.php b/src/Printer.php index de8a074..f2555ec 100644 --- a/src/Printer.php +++ b/src/Printer.php @@ -14,7 +14,7 @@ namespace PhpAidc\LabelPrinter; -use PhpAidc\LabelPrinter\Contract\Label; +use PhpAidc\LabelPrinter\Contract\Job; use PhpAidc\LabelPrinter\Contract\Connector; final class Printer @@ -31,13 +31,13 @@ public function __construct(Connector $connector, ?Compiler $compiler = null) $this->compiler = $compiler; } - public function print(Label $label, int $copies = 1): void + public function print(Job $job): void { if ($this->compiler === null) { throw new \DomainException(); } - $this->connector->write($this->compiler->compile($label, $copies)); + $this->connector->write($this->compiler->compile($job)); } public function send($payload): void