| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * @author: stev leibelt <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @since: 2015-04-17 | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | namespace Net\Bazzline\Component\Csv\Writer; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Net\Bazzline\Component\Csv\AbstractBase; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  | use Net\Bazzline\Component\Csv\InvalidArgumentException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * Class Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * @package Net\Bazzline\Component\Csv\Writer | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | class Writer extends AbstractBase implements WriterInterface | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |     const OPEN_MODE_APPEND      = 'a'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     const OPEN_MODE_TRUNCATE    = 'w'; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** @var boolean */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |     private $useTruncateAsOpenMode = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |      * @param mixed|array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     public function __invoke($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |         return $this->writeOne($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     //begin of general | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * @param bool $setPathAsCurrentPath | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @throws InvalidArgumentException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @todo implement path validation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |     public function copy($path, $setPathAsCurrentPath = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |         $couldBeCopied = copy($this->getPath(), $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |         if ($setPathAsCurrentPath) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |             if ($couldBeCopied) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |                 $this->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |                 $this->setPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |         return $couldBeCopied; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param string $path | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @todo implement path validation | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     public function move($path) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |         $couldBeMoved = rename($this->getPath(), $path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |         if ($couldBeMoved) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |             $this->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |             $this->setPath($path); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |         return $couldBeMoved; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @return bool | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 75 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 76 |  |  |     public function delete() | 
            
                                                                        
                            
            
                                    
            
            
                | 77 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 78 |  |  |         $this->close(); | 
            
                                                                        
                            
            
                                    
            
            
                | 79 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 80 |  |  |         return unlink($this->getPath()); | 
            
                                                                        
                            
            
                                    
            
            
                | 81 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |     public function truncate() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |         $this->close(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |         $this->useTruncateAsOpenMode = true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |         $this->open($this->getPath()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |         $this->useTruncateAsOpenMode = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * truncates file and writes content | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @param array $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     public function writeAll(array $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |         $this->truncate(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |         return $this->writeMany($collection); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * @param array $headlines | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |     public function writeHeadlines(array $headlines) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |         $this->setHeadline($headlines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |         return $this->writeOne($headlines); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |      * @param array $collection | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |     public function writeMany(array $collection) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |         $lengthOfTheWrittenStrings = 0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |         foreach ($collection as $data) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |             $lengthOfTheWrittenString = $this->writeOne($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |             if ($lengthOfTheWrittenString === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |                 $lengthOfTheWrittenStrings = $lengthOfTheWrittenString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  |                 break; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |             } else { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  |                 $lengthOfTheWrittenStrings += $lengthOfTheWrittenString; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |         return $lengthOfTheWrittenStrings; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @param string|array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @return false|int | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function writeOne($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $data = $this->convertToArrayIfNeeded($data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         return $this->getFileHandler()->fputcsv($data, $this->getDelimiter(), $this->getEnclosure()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |     //end of general | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param string|array $data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |     protected function convertToArrayIfNeeded($data) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |         if (!is_array($data)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             $data = explode($this->getDelimiter(), $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |             $data = array_map(function($value) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |                 return trim($value); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 |  |  |             }, $data); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |         return $data; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 165 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 166 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 167 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 168 |  |  |     protected function getFileHandlerOpenMode() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 169 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 170 |  |  |         return ($this->useTruncateAsOpenMode) ? self::OPEN_MODE_TRUNCATE : self::OPEN_MODE_APPEND; | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 171 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 172 |  |  | } |