| 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\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | use PhpAidc\LabelPrinter\Command\Raw; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  | use PhpAidc\LabelPrinter\Command\Clear; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  | use PhpAidc\LabelPrinter\Command\Bitmap; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  | use PhpAidc\LabelPrinter\Command\Barcode; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  | use PhpAidc\LabelPrinter\Command\TextLine; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  | use PhpAidc\LabelPrinter\Command\TextBlock; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | use PhpAidc\LabelPrinter\Command\InternalImage; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | use PhpAidc\LabelPrinter\Contract\Label; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | use PhpAidc\LabelPrinter\Contract\Command; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | use PhpAidc\LabelPrinter\Contract\Language; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use PhpAidc\LabelPrinter\Enum\Unit; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use PhpAidc\LabelPrinter\Enum\Charset; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use PhpAidc\LabelPrinter\Language\Tspl as Handlers; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | final class Tspl implements Language | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     private const EOC = "\n"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     private const HANDLERS = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |         Raw::class           => Handlers\TsplRaw::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         Clear::class         => Handlers\TsplClear::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |         Bitmap::class        => Handlers\TsplBitmap::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         Barcode::class       => Handlers\TsplBarcode::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         TextLine::class      => Handlers\TsplTextLine::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |         TextBlock::class     => Handlers\TsplTextBlock::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         InternalImage::class => Handlers\TsplInternalImage::class, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     ]; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 45 |  |  |     public function compileDeclaration(Label $label): iterable | 
            
                                                                        
                            
            
                                    
            
            
                | 46 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 47 |  |  |         yield from $this->translateMedia($label->getMedia()); | 
            
                                                                        
                            
            
                                    
            
            
                | 48 |  |  |         yield from $this->translateCharset($label->getCharset()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |     public function isSupport(Command $command): bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return isset(self::HANDLERS[\get_class($command)]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     public function compilePrint(int $copies): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         if ($copies <= 0) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |             throw new \InvalidArgumentException('Number of copies must be greather than 0.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |         yield "PRINT 1,{$copies}".self::EOC; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     public function compileCommand(Command $command): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |         if ($this->isSupport($command)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             $handler = self::HANDLERS[\get_class($command)]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |             foreach ((new $handler)->translate($command) as $instruction) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                 yield $instruction.self::EOC; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |         } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |             // throw exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |             yield null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     private function translateCharset(?Charset $charset): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |         if ($charset) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |             yield \sprintf('CODEPAGE %s', $charset->getValue()).self::EOC; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |     private function translateMedia(array $media): iterable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         ['unit' => $unit, 'width' => $width, 'height' => $height] = $media; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         if ($width && $height === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |             yield \sprintf('SIZE %s'.self::EOC, $this->valueWithUnit($width, $unit)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |             return; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |         if ($width && $height) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |             yield \vsprintf('SIZE %s,%s'.self::EOC, [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 $this->valueWithUnit($width, $unit), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                 $this->valueWithUnit($height, $unit), | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             ]); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     private function valueWithUnit(float $value, Unit $unit): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         if ($unit->equals(Unit::IN())) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |             return (string) $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         return "{$value} {$unit->getValue()}"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 112 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 113 |  |  |  |