Skip to content

Commit

Permalink
Batch printing
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaoda committed Jul 2, 2020
1 parent e96423c commit 34e8700
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 13 deletions.
38 changes: 29 additions & 9 deletions src/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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
Expand All @@ -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);
}
}
}
20 changes: 20 additions & 0 deletions src/Contract/Job.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?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\Contract;

interface Job
{
//
}
2 changes: 1 addition & 1 deletion src/Contract/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use PhpAidc\LabelPrinter\Enum\Charset;
use PhpAidc\LabelPrinter\Enum\Direction;

interface Label
interface Label extends Job
{
public function charset(Charset $value);

Expand Down
38 changes: 38 additions & 0 deletions src/Label/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?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\Label;

use PhpAidc\LabelPrinter\Contract\Job;
use PhpAidc\LabelPrinter\Contract\Label as LabelContract;

final class Batch implements Job
{
private $labels = [];

public function add(LabelContract $label)
{
$this->labels[] = $label;

return $this;
}

/**
* @return LabelContract[]
*/
public function getLabels()
{
return $this->labels;
}
}
15 changes: 15 additions & 0 deletions src/Label/Label.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ final class Label implements LabelContract

private $commands = [];

/** @var int */
private $copies = 1;

/** @var Charset|null */
private $charset;

Expand Down Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Printer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit 34e8700

Please # to comment.