Skip to content

Commit cd6a0bb

Browse files
committed
Refactoring compilation
1 parent 12203db commit cd6a0bb

File tree

5 files changed

+101
-42
lines changed

5 files changed

+101
-42
lines changed

src/Compiler.php

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717
use PhpAidc\LabelPrinter\Label\Batch;
1818
use PhpAidc\LabelPrinter\Contract\Job;
1919
use PhpAidc\LabelPrinter\Contract\Label;
20+
use PhpAidc\LabelPrinter\Contract\Command;
2021
use PhpAidc\LabelPrinter\Contract\Language;
22+
use PhpAidc\LabelPrinter\Label\BooleanCondition;
23+
use PhpAidc\LabelPrinter\Label\LanguageCondition;
2124

2225
final class Compiler
2326
{
@@ -55,23 +58,47 @@ public function compile(Job $job): string
5558
return \implode($payload);
5659
}
5760

58-
private function compileLabel(Label $label): iterable
61+
private function compileBatch(Batch $batch): \Generator
62+
{
63+
foreach ($batch as $label) {
64+
yield from $this->compileLabel($label);
65+
}
66+
}
67+
68+
private function compileLabel(Label $label): \Generator
5969
{
6070
yield from $this->language->compileDeclaration($label);
6171

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-
}
72+
yield from $this->compileStatements($label);
6773

6874
yield from $this->language->compilePrint($label->getCopies());
6975
}
7076

71-
private function compileBatch(Batch $batch): iterable
77+
private function compileStatements(Label $label): \Generator
7278
{
73-
foreach ($batch as $label) {
74-
yield from $this->compileLabel($label);
79+
foreach ($label as $statement) {
80+
yield from $this->compileStatement($label, $statement);
81+
}
82+
}
83+
84+
private function compileStatement(Label $label, $statement): \Generator
85+
{
86+
if ($statement instanceof LanguageCondition && $statement->isMatch(\get_class($this->language))) {
87+
yield from $this->compileStatements($statement->apply((clone $label)->erase()));
88+
89+
return;
90+
}
91+
92+
if ($statement instanceof BooleanCondition && $statement->isTruthy()) {
93+
yield from $this->compileStatements($statement->apply((clone $label)->erase()));
94+
95+
return;
96+
}
97+
98+
if ($statement instanceof Command && $this->language->isSupport($statement)) {
99+
yield from $this->language->compileCommand($statement);
100+
101+
return;
75102
}
76103
}
77104
}

src/Contract/Label.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616

1717
use PhpAidc\LabelPrinter\Enum\Charset;
1818

19-
interface Label extends Job
19+
interface Label extends Job, \IteratorAggregate
2020
{
2121
public function add(Command $command);
2222

@@ -25,6 +25,4 @@ public function getMedia(): array;
2525
public function getCharset(): ?Charset;
2626

2727
public function getCopies(): int;
28-
29-
public function getCommands(string $language): iterable;
3028
}

src/Label/Label.php

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ final class Label implements LabelContract
3535
private $direction;
3636

3737
/** @var Command[]|Condition[] */
38-
private $commands = [];
38+
private $statements = [];
3939

4040
public static function create(?Unit $unit = null, ?float $width = null, ?float $height = null): self
4141
{
@@ -53,7 +53,7 @@ public function __construct(?Unit $unit = null, ?float $width = null, ?float $he
5353

5454
public function add(Command $command)
5555
{
56-
$this->commands[] = $command;
56+
$this->statements[] = $command;
5757

5858
return $this;
5959
}
@@ -109,24 +109,9 @@ public function getHeight(): ?float
109109
return $this->media['height'] ?? null;
110110
}
111111

112-
public function getCommands(string $language): iterable
113-
{
114-
foreach ($this->commands as $command) {
115-
if ($command instanceof Condition) {
116-
if ($command->isMatch($language)) {
117-
foreach ($command->call((clone $this)->erase()) as $item) {
118-
yield $item;
119-
}
120-
}
121-
} else {
122-
yield $command;
123-
}
124-
}
125-
}
126-
127112
public function erase()
128113
{
129-
$this->commands = [];
114+
$this->statements = [];
130115

131116
return $this;
132117
}
@@ -141,7 +126,7 @@ public function erase()
141126
*/
142127
public function for(string $language, callable $callback)
143128
{
144-
$this->commands[] = new LanguageCondition($language, $callback);
129+
$this->statements[] = new LanguageCondition($language, $callback);
145130

146131
return $this;
147132
}
@@ -156,8 +141,13 @@ public function for(string $language, callable $callback)
156141
*/
157142
public function when($value, callable $callback)
158143
{
159-
$this->commands[] = new BooleanCondition($value, $callback);
144+
$this->statements[] = new BooleanCondition($value, $callback);
160145

161146
return $this;
162147
}
148+
149+
public function getIterator()
150+
{
151+
return new \ArrayIterator($this->statements);
152+
}
163153
}

tests/Unit/CompilerTest.php

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace PhpAidc\LabelPrinter\Tests\Unit;
1616

1717
use PhpAidc\LabelPrinter\Compiler;
18+
use PhpAidc\LabelPrinter\Label\Batch;
1819
use PhpAidc\LabelPrinter\Label\Label;
1920
use PhpAidc\LabelPrinter\Label\Element;
2021
use PhpAidc\LabelPrinter\Contract\Command;
@@ -24,16 +25,31 @@
2425

2526
class CompilerTest extends TestCase
2627
{
27-
public function testCompile(): void
28+
public function testCompileLabel(): void
2829
{
2930
$compiler = Compiler::create(new LanguageA());
3031

3132
$label = Label::create()->add(Element::raw(''));
33+
$this->assertEquals('ASIZE|ACMD|APRINT1|', $compiler->compile($label));
3234

33-
$this->assertEquals('ASIZE|ACMD|APRINT|', $compiler->compile($label));
35+
$label->copies(2);
36+
$this->assertEquals('ASIZE|ACMD|APRINT2|', $compiler->compile($label));
3437
}
3538

36-
public function testConditionalCompilation(): void
39+
public function testCompileBatch(): void
40+
{
41+
$compiler = Compiler::create(new LanguageA());
42+
43+
$label = Label::create()->add(Element::raw(''));
44+
$batch = new Batch();
45+
46+
$batch->add(clone $label);
47+
$batch->add((clone $label)->copies(2));
48+
49+
$this->assertEquals('ASIZE|ACMD|APRINT1|ASIZE|ACMD|APRINT2|', $compiler->compile($batch));
50+
}
51+
52+
public function testLanguageCondition(): void
3753
{
3854
$label = Label::create()
3955
->for(LanguageA::class, static function (Label $label) {
@@ -43,8 +59,28 @@ public function testConditionalCompilation(): void
4359
$label->add(Element::raw(''));
4460
});
4561

46-
$this->assertEquals('ASIZE|ACMD|APRINT|', Compiler::create(new LanguageA())->compile($label));
47-
$this->assertEquals('BSIZE|BCMD|BPRINT|', Compiler::create(new LanguageB())->compile($label));
62+
$this->assertEquals('ASIZE|ACMD|APRINT1|', Compiler::create(new LanguageA())->compile($label));
63+
$this->assertEquals('BSIZE|BCMD|BPRINT1|', Compiler::create(new LanguageB())->compile($label));
64+
}
65+
66+
public function testBooleanConditionTruthy(): void
67+
{
68+
$label = Label::create()
69+
->when(1 > 0, static function (Label $label) {
70+
$label->add(Element::raw(''));
71+
});
72+
73+
$this->assertEquals('ASIZE|ACMD|APRINT1|', Compiler::create(new LanguageA())->compile($label));
74+
}
75+
76+
public function testBooleanConditionFalsy(): void
77+
{
78+
$label = Label::create()
79+
->when(0 > 1, static function (Label $label) {
80+
$label->add(Element::raw(''));
81+
});
82+
83+
$this->assertEquals('ASIZE|APRINT1|', Compiler::create(new LanguageA())->compile($label));
4884
}
4985
}
5086

@@ -67,7 +103,7 @@ public function compileCommand(Command $command): iterable
67103

68104
public function compilePrint(int $copies): iterable
69105
{
70-
yield 'APRINT|';
106+
yield "APRINT{$copies}|";
71107
}
72108
}
73109

@@ -90,6 +126,6 @@ public function compileCommand(Command $command): iterable
90126

91127
public function compilePrint(int $copies): iterable
92128
{
93-
yield 'BPRINT|';
129+
yield "BPRINT{$copies}|";
94130
}
95131
}

tests/Unit/PrinterTest.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,21 @@
2727

2828
class PrinterTest extends TestCase
2929
{
30-
public function testPrint(): void
30+
public function testPrintWithCompiler(): void
3131
{
3232
$printer = new Printer($connector = new ArrayConnector(), Compiler::create(new LanguageC()));
3333

3434
$printer->print(Label::create()->add(Element::raw('TEST')));
3535

36-
$this->assertEquals('CSIZE|CTEST|CPRINT|', \implode('', $connector->get()));
36+
$this->assertEquals('CSIZE|CTEST|CPRINT1|', \implode('', $connector->get()));
37+
}
38+
39+
public function testPrintWithoutCompiler(): void
40+
{
41+
$this->expectException(\DomainException::class);
42+
$printer = new Printer($connector = new ArrayConnector());
43+
44+
$printer->print(Label::create()->add(Element::raw('TEST')));
3745
}
3846

3947
public function testSend(): void
@@ -69,7 +77,7 @@ public function compileCommand(Command $command): iterable
6977

7078
public function compilePrint(int $copies): iterable
7179
{
72-
yield 'CPRINT|';
80+
yield "CPRINT{$copies}|";
7381
}
7482
}
7583

0 commit comments

Comments
 (0)