| 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\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | final class Compiler | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** @var Language */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     private $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 | 4 |  |     public static function create(Language $language): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 | 4 |  |         return new self($language); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 4 |  |     public function __construct(Language $language) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 4 |  |         $this->language = $language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 4 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 | 3 |  |     public function compile(Job $job): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 | 3 |  |         $instructions = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 | 3 |  |         if ($job instanceof Label) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 3 |  |             $instructions[] = $this->compileLabel($job); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 3 |  |         if ($job instanceof Batch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             $instructions[] = $this->compileBatch($job); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         $payload = \array_reduce($instructions, static function ($carry, $item) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 3 |  |             return $item instanceof \Generator | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 3 |  |                 ? \array_merge($carry, \iterator_to_array($item, false)) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 | 3 |  |                 : \array_merge($carry, $item); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 | 3 |  |         }, []); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 3 |  |         return \implode($payload); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 | 3 |  |     private function compileLabel(Label $label): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 3 |  |         yield from $this->language->compileDeclaration($label); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 3 |  |         foreach ($label->getCommands(\get_class($this->language)) as $command) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 3 |  |             if ($this->language->isSupport($command)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 3 |  |                 yield from $this->language->compileCommand($command); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 | 3 |  |         yield from $this->language->compilePrint($label->getCopies()); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 3 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 71 |  |  |     private function compileBatch(Batch $batch): iterable | 
            
                                                                        
                            
            
                                    
            
            
                | 72 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 73 |  |  |         foreach ($batch->getLabels() as $label) { | 
            
                                                                        
                            
            
                                    
            
            
                | 74 |  |  |             yield from $this->compileLabel($label); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 77 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 78 |  |  |  |