@@ -260,6 +260,10 @@ snippet if
260
260
if (${1: /* condition */ } ) {
261
261
${0}
262
262
}
263
+ snippet ifn
264
+ if (!${1: /* condition */ } ) {
265
+ ${2}
266
+ }
263
267
snippet ifil
264
268
<?php if (${1: /* condition */ } ): ?>
265
269
${0}
@@ -412,3 +416,144 @@ snippet static_var
412
416
if (is_null($$1 )){
413
417
$$1 = ${2} ;
414
418
}
419
+
420
+ snippet CSVWriter
421
+ <?php
422
+
423
+ class CSVWriter {
424
+ public function __construct($file_or_handle, $sep = "\t ", $quot = '"'){
425
+ $args = func_get_args();
426
+ $mode = isset($opts['mode']) ? $opts['mode'] : 'w';
427
+
428
+ $this->f =
429
+ is_string($file_or_handle)
430
+ ? fopen($file_or_handle, $mode)
431
+ : $file_or_handle;
432
+
433
+ $this->fputcsv_args = array($this->f, null, $sep, $quot);
434
+
435
+ if (!$this->f) throw new Exception('bad file descriptor');
436
+ }
437
+
438
+ public function write($row){
439
+ $this->fputcsv_args[1] =& $row;
440
+ call_user_func_array('fputcsv', $this->fputcsv_args);
441
+ }
442
+
443
+ public function close(){
444
+ if (!is_null($this->f))
445
+ fclose($this->f);
446
+ $this->f = null;
447
+ }
448
+
449
+ public function __destruct(){
450
+ $this->close();
451
+ }
452
+
453
+ }
454
+
455
+ snippet CSVIterator
456
+
457
+ // http://snipplr.com/view.php?codeview&id=1986 // modified
458
+ class CSVIterator implements Iterator
459
+ {
460
+ private $f;
461
+ private $curr;
462
+ private $rowCounter;
463
+
464
+ /* opts keys:
465
+ * row_size
466
+ * escape
467
+ * enclosure
468
+ * delimiter
469
+ */
470
+ public function __construct( $file_or_handle, $opts = array(4096, ',') )
471
+ {
472
+ $d = function($n) use(&$opts){ return isset($opts[$n]) ? $opts[$n] : false; };
473
+
474
+ $this->combine = $d('combine');
475
+ $this->headers = $d('headers');
476
+ $this->headerCheckFunction = $d('header_check_function');
477
+
478
+ $this->f =
479
+ is_string($file_or_handle)
480
+ ? fopen( $file_or_handle, 'r' )
481
+ : $file_or_handle;
482
+ if (!$this->f) throw new Exception('bad file descriptor');
483
+ $this->fgetcsv_args = array(
484
+ $this->f,
485
+ isset($opts['row_size']) ? $opts['row_size'] : 4096,
486
+ isset($opts['delimiter']) ? $opts['delimiter'] : ',',
487
+ isset($opts['enclosure']) ? $opts['enclosure'] : '"',
488
+ isset($opts['escape']) ? $opts['escape'] : '\\ ',
489
+ );
490
+ $this->start();
491
+ }
492
+
493
+ protected function readRow(){
494
+ $this->curr = call_user_func_array('fgetcsv', $this->fgetcsv_args );
495
+ $this->rowCounter++;
496
+ if ($this->rowCounter == 1){
497
+ $this->processHeader();
498
+ } elseif ($this->curr) {
499
+ $this->processRow();
500
+ }
501
+ }
502
+
503
+ public function processHeader(){
504
+ if ($this->headers || $this->combine){
505
+ $this->header = $this->curr;
506
+ if ($this->headerCheckFunction){
507
+ $f = $this->headerCheckFunction;
508
+ $f($this->header);
509
+ }
510
+ $this->readRow();
511
+ }
512
+ }
513
+
514
+ public function processRow(){
515
+ if ($this->combine)
516
+ $this->curr = array_combine($this->header, $this->curr);
517
+ }
518
+
519
+ public function start(){
520
+ $this->rowCounter = 0;
521
+ rewind( $this->f );
522
+ $this->readRow();
523
+ }
524
+
525
+ public function rewind()
526
+ {
527
+ $this->start();
528
+ }
529
+
530
+ public function current()
531
+ {
532
+ $curr = $this->curr;
533
+ $this->readRow();
534
+ return $curr;
535
+ }
536
+
537
+ public function key()
538
+ {
539
+ return $this->rowCounter;
540
+ }
541
+
542
+ public function next()
543
+ {
544
+ return $this->curr;
545
+ }
546
+
547
+ public function valid(){
548
+ if( !$this->next() )
549
+ {
550
+ fclose( $this->f );
551
+ return FALSE;
552
+ }
553
+ return TRUE;
554
+ }
555
+
556
+ } // end class
557
+
558
+ snippet is
559
+ isset($1 {VISUAL})
0 commit comments