| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace MewesK\TwigSpreadsheetBundle\Wrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\Drawing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use PhpOffice\PhpSpreadsheet\Worksheet\HeaderFooterDrawing; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * Class DrawingWrapper. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | class DrawingWrapper extends BaseWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |      * @var SheetWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     protected $sheetWrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      * @var HeaderFooterWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     protected $headerFooterWrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      * @var Drawing | HeaderFooterDrawing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     protected $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * DrawingWrapper constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      * @param array               $context | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @param \Twig_Environment   $environment | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      * @param SheetWrapper        $sheetWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      * @param HeaderFooterWrapper $headerFooterWrapper | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public function __construct(array $context, \Twig_Environment $environment, SheetWrapper $sheetWrapper, HeaderFooterWrapper $headerFooterWrapper) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |         parent::__construct($context, $environment); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |         $this->sheetWrapper = $sheetWrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |         $this->headerFooterWrapper = $headerFooterWrapper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         $this->object = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      * @param array  $properties | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |      * @throws \LogicException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      * @throws \PhpOffice\PhpSpreadsheet\Exception | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     public function start(string $path, array $properties = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |         if ($this->sheetWrapper->getObject() === null) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |             throw new \LogicException(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |         // create local copy of the asset | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |         $tempPath = $this->createTempCopy($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         // add to header/footer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |         if ($this->headerFooterWrapper->getObject()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |             $headerFooterAttributes = $this->headerFooterWrapper->getAttributes(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $location = ''; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |             switch (strtolower($this->headerFooterWrapper->getAlignmentAttributes()['type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |                 case 'left': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |                     $location .= 'L'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |                     $headerFooterAttributes['value']['left'] .= '&G'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |                 case 'center': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |                     $location .= 'C'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |                     $headerFooterAttributes['value']['center'] .= '&G'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |                 case 'right': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |                     $location .= 'R'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |                     $headerFooterAttributes['value']['right'] .= '&G'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |                     throw new \InvalidArgumentException(sprintf('Unknown alignment type "%s"', $this->headerFooterWrapper->getAlignmentAttributes()['type'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |             switch (strtolower($headerFooterAttributes['type'])) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |                 case 'header': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |                 case 'oddheader': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |                 case 'evenheader': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |                 case 'firstheader': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |                     $location .= 'H'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |                 case 'footer': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |                 case 'oddfooter': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |                 case 'evenfooter': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |                 case 'firstfooter': | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |                     $location .= 'F'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |                     break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |                 default: | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |                     throw new \InvalidArgumentException(sprintf('Unknown type "%s"', $headerFooterAttributes['type'])); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |             $this->object = new HeaderFooterDrawing(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |             $this->object->setPath($tempPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |             $this->headerFooterWrapper->getObject()->addImage($this->object, $location); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |             $this->headerFooterWrapper->setAttributes($headerFooterAttributes); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |         // add to worksheet | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |         else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |             $this->object = new Drawing(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |             $this->object->setWorksheet($this->sheetWrapper->getObject()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |             $this->object->setPath($tempPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |         $this->setProperties($properties, $this->mappings); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     public function end() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |         $this->object = null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $this->attributes = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |      * @return Drawing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |     public function getObject(): Drawing | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |         return $this->object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |      * @param Drawing $object | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     public function setObject(Drawing $object) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |         $this->object = $object; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |     protected function configureMappings(): array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |             'coordinates' => function ($value) { $this->object->setCoordinates($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |             'description' => function ($value) { $this->object->setDescription($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |             'height' => function ($value) { $this->object->setHeight($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |             'name' => function ($value) { $this->object->setName($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |             'offsetX' => function ($value) { $this->object->setOffsetX($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |             'offsetY' => function ($value) { $this->object->setOffsetY($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |             'resizeProportional' => function ($value) { $this->object->setResizeProportional($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |             'rotation' => function ($value) { $this->object->setRotation($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |             'shadow' => [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |                 'alignment' => function ($value) { $this->object->getShadow()->setAlignment($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |                 'alpha' => function ($value) { $this->object->getShadow()->setAlpha($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |                 'blurRadius' => function ($value) { $this->object->getShadow()->setBlurRadius($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 'color' => function ($value) { $this->object->getShadow()->getColor()->setRGB($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |                 'direction' => function ($value) { $this->object->getShadow()->setDirection($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |                 'distance' => function ($value) { $this->object->getShadow()->setDistance($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |                 'visible' => function ($value) { $this->object->getShadow()->setVisible($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |             ], | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |             'width' => function ($value) { $this->object->setWidth($value); }, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |      * @throws \RuntimeException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 171 |  |  |      * @throws \InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 172 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 173 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 174 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 175 |  |  |     private function createTempCopy(string $path): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 176 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 177 |  |  |         // create temp path | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 178 |  |  |         $pathExtension = pathinfo($path, PATHINFO_EXTENSION); | 
            
                                                                        
                            
            
                                    
            
            
                | 179 |  |  |         $tempPath = sys_get_temp_dir().DIRECTORY_SEPARATOR.'xlsdrawing'.'_'.md5($path).($pathExtension ? '.'.$pathExtension : ''); | 
            
                                                                        
                            
            
                                    
            
            
                | 180 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 181 |  |  |         // create local copy | 
            
                                                                        
                            
            
                                    
            
            
                | 182 |  |  |         if (!file_exists($tempPath)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 183 |  |  |             $data = file_get_contents($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 184 |  |  |             if ($data === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 185 |  |  |                 throw new \InvalidArgumentException($path.' does not exist.'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 186 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 187 |  |  |             $temp = fopen($tempPath, 'wb+'); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 188 |  |  |             if ($temp === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 189 |  |  |                 throw new \RuntimeException('Cannot open '.$tempPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 190 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 191 |  |  |             fwrite($temp, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 192 |  |  |             if (fclose($temp) === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 193 |  |  |                 throw new \RuntimeException('Cannot close '.$tempPath); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 194 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 195 |  |  |             unset($data, $temp); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 196 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 197 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 198 |  |  |         return $tempPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 199 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 200 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 201 |  |  |  |