Changeset 20719
- Timestamp:
- 08/03/09 13:59:11 (4 years ago)
- Files:
-
- plugins/tmCsvPlugin/lib/tmCsvReader.class.php (modified) (15 diffs)
- plugins/tmCsvPlugin/test/unit/_employees02.csv (added)
- plugins/tmCsvPlugin/test/unit/doctrine01Test.php (modified) (2 diffs)
- plugins/tmCsvPlugin/test/unit/encodingTest.php (modified) (2 diffs)
- plugins/tmCsvPlugin/test/unit/readCsvTest.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
plugins/tmCsvPlugin/lib/tmCsvReader.class.php
r20712 r20719 7 7 * @license LGPL 8 8 */ 9 10 9 11 10 class tmCsvException extends Exception … … 29 28 const ERR_HEADER = 4; 30 29 31 32 30 private $hasHeader; 33 31 … … 35 33 * @var array The header as parsed from the CSV file in a format array('column'=>'column'). We do not allow for 2 columns with the same name. 36 34 */ 37 private $header, $schema;35 private $header, $schema; 38 36 39 37 /** … … 90 88 $this->escape = null; 91 89 } 92 90 93 91 if(isset($options['from'])) { 94 92 $this->from = $options['from']; … … 96 94 $this->from = 'auto'; 97 95 } 98 96 99 97 if(isset($options['to'])) { 100 98 $this->to = $options['to']; … … 116 114 throw new CsvException('Header option must equal to true or false', self::ERR_WRONG_OPTION); 117 115 } 118 119 116 120 117 $this->header = null; … … 141 138 } 142 139 143 public function removeHeader($columns) 144 { 145 if(! $this->hasHeader) {140 public function removeHeader($columns) 141 { 142 if(! $this->hasHeader) { 146 143 throw new CsvException('Header is not set to be parsed', self::ERR_WRONG_OPTION); 147 144 } … … 149 146 $this->readHeader(); 150 147 } 151 148 152 149 if(! is_array($columns)) { 153 $columns = array ( $columns => $columns ); 154 } 155 150 $columns = array ( 151 $columns => $columns ); 152 } 153 156 154 foreach($columns as $c => $column) { 157 unset($this->currentHeader[$c]);158 unset($this->currentHeader[$column]);159 } 160 155 unset($this->currentHeader[$c]); 156 unset($this->currentHeader[$column]); 157 } 158 161 159 } 162 160 … … 166 164 public function getRemoved() 167 165 { 168 if(! $this->hasHeader) {166 if(! $this->hasHeader) { 169 167 throw new CsvException('Header is not set to be parsed', self::ERR_WRONG_OPTION); 170 168 } … … 175 173 return array_diff_assoc($this->header, $this->currentHeader); 176 174 } 177 178 175 179 176 private function readIntoMemory() … … 227 224 228 225 //for PHP 5.3: fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure, $this->escape) 229 $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 230 231 var_dump($data); 226 $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 227 232 228 if($data === false || count($data) < 2) { 233 229 $this->state = self::ALL_READ; … … 236 232 237 233 //encoding 238 foreach($data as $k =>$field) {239 $data[$k] = $this->encode($field);234 foreach($data as $k => $field) { 235 $data[$k] = $this->encode($field); 240 236 } 241 237 … … 243 239 //we need to consider removed columns 244 240 //first, we compare data we've got with the original header 241 245 242 246 243 //@todo check the option on what do we do in this case 247 244 if(count($data) > count($this->header)) { 248 array_splice($data,count($this->header));249 } 250 245 array_splice($data, count($this->header)); 246 } 247 251 248 //@todo check the option on what do we do in this case 252 249 if(count($data) < count($this->header)) { 253 $data = array_merge($data, array_fill(0, count($this->header) - count($data), null));254 } 255 256 $structuredData = array ();250 $data = array_merge($data, array_fill(0, count($this->header) - count($data), null)); 251 } 252 253 $structuredData = array (); 257 254 reset($this->header); 258 255 //next, current 259 foreach ($data as $field) {260 if(array_key_exists(current($this->header), $this->currentHeader)) {256 foreach($data as $field) { 257 if(array_key_exists(current($this->header), $this->currentHeader)) { 261 258 $structuredData[current($this->header)] = $field; 262 259 } … … 289 286 { 290 287 rewind($this->fhandle); 291 288 292 289 $this->header = array (); 293 290 $data = fgetcsv($this->fhandle, $this->length, $this->delimiter, $this->enclosure); 294 291 foreach($data as $field) { 295 if(key_exists($field, $this->header)) {292 if(key_exists($field, $this->header)) { 296 293 throw new tmCsvException("Field: '$field' is repeated twice in a header.", self::ERR_HEADER); 297 294 } … … 300 297 $this->currentHeader = $this->header; 301 298 } 302 303 299 304 300 public function next() plugins/tmCsvPlugin/test/unit/doctrine01Test.php
r20712 r20719 30 30 $csv->setTable(Doctrine::getTable('Employee')); 31 31 $testResults = $csv->parse(); 32 //$testResults['valid']->save();33 32 $first = $testResults['valid'][0]; 34 33 $t->cmp_ok($first['id'], '===',null); … … 47 46 $csv->setTemplate($eTmpl); 48 47 $parsed = $csv->parse(); 49 50 48 $third = $parsed['valid'][1]; 51 49 $t->is($third['firstname'], 'Zabuch'); 52 50 $t->is($third['lastname'], 'lastname4'); 53 var_dump($third->toArray());54 $third->save();55 var_dump($third->toArray());56 $third->save();57 $third['age'] = 776;58 var_dump($third->toArray());59 51 $csv->close(); 60 52 61 62 $eTmpl = new Employee();63 $eTmpl['firstname'] = 'Zabuch';64 var_dump($eTmpl->toArray());65 $eTmpl2 = $eTmpl->copy(true);66 //$eTmpl["updated_at"] = '2006-09-09';67 $eTmpl2->save();68 var_dump($eTmpl2->toArray());69 plugins/tmCsvPlugin/test/unit/encodingTest.php
r20712 r20719 7 7 $t = new lime_test(null, new lime_output_color()); 8 8 9 $t->comment('Testing encoding'); 9 10 10 11 if(setlocale(LC_ALL, 'pl_PL.ISO-8859-2') === false) { … … 14 15 $csv = new tmCsvReader($testFile, array('header'=>false, 'from'=>'iso-8859-2')); 15 16 $arr = $csv->toArray(); 16 var_dump($arr[0]);17 17 $csv->close(); 18 18 $strUtf8 = "\x67\xc5\xbc\x65\x67\xc5\xbc\xc3\xb3\xc5\x82\x6b\x61"; 19 19 $t->is($strUtf8, $arr[0][0]); 20 20 $strUtf8="\xc4\x99\xc3\xb3\xc5\x82\xc5\x84\xc5\xbc\xc5\xba\xc4\x87\xc4\x85\xc5\x9b"; 21 var_dump($strUtf8);22 21 $t->is($strUtf8, $arr[0][1]); 22 23 if(setlocale(LC_ALL, 'en_US.UTF-8') === false) { 24 $t->fail("Can't set the locale needed for this test"); 25 } 26 27 $testFile = '_encoding_utf-8.csv'; 28 $csv = new tmCsvReader($testFile, array('header'=>false, 'from'=>'utf-8', 'to'=>'iso-8859-2')); 29 $arr = $csv->toArray(); 30 //var_dump($arr[0]); 31 $csv->close(); 32 $strIso = "\x67\xbf\x65\x67\xbf\xf3\xb3\x6b\x61"; 33 $t->is($strIso, $arr[0][0]); 34 $strIso = "\xea\xf3\xb3\xf1\xbf\xbc\xe6\xb1\xb6"; 35 //var_dump($strUtf8); 36 $t->is($strIso, $arr[0][1]); plugins/tmCsvPlugin/test/unit/readCsvTest.php
r20625 r20719 53 53 $csv->close(); 54 54 55 56 $testFile = '_employees02.csv'; 57 $csv = new tmCsvReader($testFile, array ( 58 'header' => true )); 59 60 $expectedValues = array ( 61 0 => array ( 62 'id1' => '17', 'id2' => '12', 'foreignKey' => '34', 'name' => 'Nam\'\'e1', 'lastname' => 'very long last 63 name with 64 few 65 newlines embedded', 'age' => '17', 'date' => '2009-12-01', 'bonus' => '17' ) ); 66 $t->is_deeply($expectedValues,$csv->toArray());