1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of PhpAidc 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; |
16
|
|
|
|
17
|
|
|
use PhpAidc\LabelPrinter\Label\Batch; |
18
|
|
|
use PhpAidc\LabelPrinter\Contract\Job; |
19
|
|
|
use PhpAidc\LabelPrinter\Contract\Label; |
20
|
|
|
use PhpAidc\LabelPrinter\Contract\Command; |
21
|
|
|
use PhpAidc\LabelPrinter\Contract\Language; |
22
|
|
|
use PhpAidc\LabelPrinter\Label\BooleanCondition; |
23
|
|
|
use PhpAidc\LabelPrinter\Label\LanguageCondition; |
24
|
|
|
|
25
|
|
|
final class Compiler |
26
|
|
|
{ |
27
|
|
|
/** @var Language */ |
28
|
|
|
private $language; |
29
|
|
|
|
30
|
7 |
|
public static function create(Language $language): self |
31
|
|
|
{ |
32
|
7 |
|
return new self($language); |
33
|
|
|
} |
34
|
|
|
|
35
|
7 |
|
public function __construct(Language $language) |
36
|
|
|
{ |
37
|
7 |
|
$this->language = $language; |
38
|
7 |
|
} |
39
|
|
|
|
40
|
6 |
|
public function compile(Job $job): string |
41
|
|
|
{ |
42
|
6 |
|
$instructions = []; |
43
|
|
|
|
44
|
6 |
|
if ($job instanceof Label) { |
45
|
5 |
|
$instructions[] = $this->compileLabel($job); |
46
|
|
|
} |
47
|
|
|
|
48
|
6 |
|
if ($job instanceof Batch) { |
49
|
1 |
|
$instructions[] = $this->compileBatch($job); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
$payload = \array_reduce($instructions, static function ($carry, $item) { |
53
|
6 |
|
return $item instanceof \Generator |
54
|
6 |
|
? \array_merge($carry, \iterator_to_array($item, false)) |
55
|
6 |
|
: \array_merge($carry, $item); |
56
|
6 |
|
}, []); |
57
|
|
|
|
58
|
6 |
|
return \implode($payload); |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
private function compileBatch(Batch $batch): \Generator |
62
|
|
|
{ |
63
|
1 |
|
foreach ($batch as $label) { |
64
|
1 |
|
yield from $this->compileLabel($label); |
65
|
|
|
} |
66
|
1 |
|
} |
67
|
|
|
|
68
|
6 |
|
private function compileLabel(Label $label): \Generator |
69
|
|
|
{ |
70
|
6 |
|
yield from $this->language->compileDeclaration($label); |
71
|
|
|
|
72
|
6 |
|
yield from $this->compileStatements($label); |
73
|
|
|
|
74
|
6 |
|
yield from $this->language->compilePrint($label->getCopies()); |
75
|
6 |
|
} |
76
|
|
|
|
77
|
6 |
|
private function compileStatements(Label $label): \Generator |
78
|
|
|
{ |
79
|
6 |
|
foreach ($label as $statement) { |
80
|
6 |
|
yield from $this->compileStatement($label, $statement); |
81
|
|
|
} |
82
|
6 |
|
} |
83
|
|
|
|
84
|
6 |
|
private function compileStatement(Label $label, $statement): \Generator |
85
|
|
|
{ |
86
|
6 |
|
if ($statement instanceof LanguageCondition && $statement->isMatch(\get_class($this->language))) { |
87
|
1 |
|
yield from $this->compileStatements($statement->apply((clone $label)->erase())); |
|
|
|
|
88
|
|
|
|
89
|
1 |
|
return; |
90
|
|
|
} |
91
|
|
|
|
92
|
6 |
|
if ($statement instanceof BooleanCondition && $statement->isTruthy()) { |
93
|
1 |
|
yield from $this->compileStatements($statement->apply((clone $label)->erase())); |
94
|
|
|
|
95
|
1 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
6 |
|
if ($statement instanceof Command && $this->language->isSupport($statement)) { |
99
|
5 |
|
yield from $this->language->compileCommand($statement); |
100
|
|
|
|
101
|
5 |
|
return; |
102
|
|
|
} |
103
|
2 |
|
} |
104
|
|
|
} |
105
|
|
|
|