Overview

Namespaces

  • Net
    • Bazzline
      • Component
        • Csv
          • Filter
          • Reader
          • Writer

Classes

  • Net\Bazzline\Component\Csv\AbstractBase
  • Net\Bazzline\Component\Csv\AbstractFactory
  • Net\Bazzline\Component\Csv\Filter\AbstractFilter
  • Net\Bazzline\Component\Csv\Filter\PermeableFilter
  • Net\Bazzline\Component\Csv\Reader\EasyCsvReaderAdapter
  • Net\Bazzline\Component\Csv\Reader\FilteredReader
  • Net\Bazzline\Component\Csv\Reader\FilteredReaderFactory
  • Net\Bazzline\Component\Csv\Reader\Reader
  • Net\Bazzline\Component\Csv\Reader\ReaderFactory
  • Net\Bazzline\Component\Csv\Writer\EasyCsvWriterAdapter
  • Net\Bazzline\Component\Csv\Writer\FilteredWriter
  • Net\Bazzline\Component\Csv\Writer\FilteredWriterFactory
  • Net\Bazzline\Component\Csv\Writer\FilteredWriterForPhp3Dot3
  • Net\Bazzline\Component\Csv\Writer\Writer
  • Net\Bazzline\Component\Csv\Writer\WriterFactory
  • Net\Bazzline\Component\Csv\Writer\WriterForPhp5Dot3

Interfaces

  • Net\Bazzline\Component\Csv\BaseInterface
  • Net\Bazzline\Component\Csv\FactoryInterface
  • Net\Bazzline\Component\Csv\Reader\ReaderInterface
  • Net\Bazzline\Component\Csv\Writer\WriterInterface

Exceptions

  • Net\Bazzline\Component\Csv\InvalidArgumentException
  • Net\Bazzline\Component\Csv\RuntimeException
  • Overview
  • Namespace
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 
<?php
/**
 * @author: stev leibelt <artodeto@bazzline.net>
 * @since: 2015-04-17
 */

namespace Net\Bazzline\Component\Csv\Reader;

//@see https://github.com/ajgarlag/AjglCsv/blob/master/Reader/ReaderAbstract.php
//@see https://github.com/jwage/easy-csv/blob/master/lib/EasyCSV/Reader.php
//@todo implement save version to call enable/disable headline before setDelimiter etc.
use Net\Bazzline\Component\Csv\AbstractBase;
use Net\Bazzline\Component\Csv\InvalidArgumentException;
use Net\Bazzline\Component\Toolbox\HashMap\Combine;
use SplFileObject;

class Reader extends AbstractBase implements ReaderInterface
{
    /** @var bool */
    private $addHeadlineToOutput = true;

    /** @var Combine */
    private $combine;

    /** @var int */
    private $initialLineNumber = 0;

    /**
     * @param null $currentLineNumber
     * @return array|bool|string
     */
    public function __invoke($currentLineNumber = null)
    {
        return $this->readOne($currentLineNumber);
    }

    //begin of AbstractBase
    /**
     * @param string $delimiter
     * @throws InvalidArgumentException
     */
    public function setDelimiter($delimiter)
    {
        parent::setDelimiter($delimiter);
        if ($this->hasHeadline()) {
            $this->enableHasHeadline();
        }
    }

    /**
     * @param string $enclosure
     * @throws InvalidArgumentException
     */
    public function setEnclosure($enclosure)
    {
        parent::setEnclosure($enclosure);
        if ($this->hasHeadline()) {
            $this->enableHasHeadline();
        }
    }

    /**
     * @param string $escapeCharacter
     * @throws InvalidArgumentException
     */
    public function setEscapeCharacter($escapeCharacter)
    {
        parent::setEscapeCharacter($escapeCharacter);
        if ($this->hasHeadline()) {
            $this->enableHasHeadline();
        }
    }

    //end of AbstractBase

    //begin of Iterator
    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Return the current element
     * @link http://php.net/manual/en/iterator.current.php
     * @return mixed Can return any type.
     */
    public function current()
    {
        return $this->getFileHandler()->current();
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Move forward to next element
     * @link http://php.net/manual/en/iterator.next.php
     * @return void Any returned value is ignored.
     */
    public function next()
    {
        $this->getFileHandler()->next();
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Return the key of the current element
     * @link http://php.net/manual/en/iterator.key.php
     * @return mixed scalar on success, or null on failure.
     */
    public function key()
    {
        return $this->getFileHandler()->key();
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Checks if current position is valid
     * @link http://php.net/manual/en/iterator.valid.php
     * @return boolean The return value will be casted to boolean and then evaluated.
     * Returns true on success or false on failure.
     */
    public function valid()
    {
        return $this->getFileHandler()->valid();
    }

    /**
     * (PHP 5 &gt;= 5.0.0)<br/>
     * Rewind the Iterator to the first element
     * @link http://php.net/manual/en/iterator.rewind.php
     * @return void Any returned value is ignored.
     */
    public function rewind()
    {
        if ($this->hasHeadline()) {
            $this->updateHeadline();
            $lineNumber = 1;
        } else {
            $lineNumber = 0;
        }
        $this->initialLineNumber = $lineNumber;
        $this->seekFileToCurrentLineNumberIfNeeded(
            $this->getFileHandler(),
            $lineNumber
        );
    }
    //end of Iterator

    //begin of headlines
    /**
     * @return $this
     */
    public function disableAddHeadlineToOutput()
    {
        $this->addHeadlineToOutput = false;

        return $this;
    }

    /**
     * @return $this
     */
    public function enableAddHeadlineToOutput()
    {
        $this->addHeadlineToOutput = true;

        return $this;
    }

    /**
     * @return $this
     */
    public function disableHasHeadline()
    {
        $this->resetHeadline();
        $this->rewind();

        return $this;
    }

    /**
     * @return $this
     */
    public function enableHasHeadline()
    {
        $this->updateHeadline();
        $this->rewind();

        return $this;
    }

    private function updateHeadline()
    {
        $this->initialLineNumber    = 0;
        $wasEnabled                 = $this->addHeadlineToOutput;

        if ($wasEnabled) {
            $this->disableAddHeadlineToOutput();
        }
        $this->setHeadline($this->readOne(0));
        if ($wasEnabled) {
            $this->enableAddHeadlineToOutput();
        }
    }

    /**
     * @return false|array
     */
    public function readHeadline()
    {
        return $this->getHeadline();
    }
    //end of headlines

    //begin of general
    /**
     * @param Combine $combine
     * @return $this
     */
    public function setCombine(Combine $combine)
    {
        $this->combine = $combine;

        return $this;
    }

    /**
     * @param null|int $lineNumber - if "null", current line number is used
     * @return array|bool|string
     */
    public function readOne($lineNumber = null)
    {
        $file           = $this->getFileHandler();
        $headline       = $this->getHeadline();
        $hasHeadline    = $this->hasHeadline();
        $this->seekFileToCurrentLineNumberIfNeeded($file, $lineNumber);

        $addHeadline    = ($hasHeadline && $this->addHeadlineToOutput && ($this->current() !== false));
        $content        = ($addHeadline)
            ? $this->combine->combine($headline, $this->current())
            : $this->current();
        $this->next();

        return $content;
    }

    /**
     * @param int $length
     * @param null|int $lineNumberToStartWith - if "null", current line number is used
     * @return array
     */
    public function readMany($length, $lineNumberToStartWith = null)
    {
        $this->rewind();
        $lastLine       = $lineNumberToStartWith + $length;
        $lines          = [];
        $currentLine    = $lineNumberToStartWith;

        //foreach not usable here since it is calling rewind before iterating
        while ($currentLine < $lastLine) {
            $line   = $this->readOne($currentLine);
            $lines  = $this->addToLinesIfLineIsValid($lines, $line);
            if (!$this->valid()) {
                $currentLine = $lastLine;
            }
            ++$currentLine;
        }

        return $lines;
    }

    /**
     * @return array
     */
    public function readAll()
    {
        $this->rewind();
        $lines = [];

        while ($line = $this()) {
            $lines = $this->addToLinesIfLineIsValid($lines, $line);
        }

        return $lines;
    }
    //end of general

    /**
     * @return string
     */
    protected function getFileHandlerOpenMode()
    {
        return 'r';
    }

    /**
     * @param array $lines
     * @param mixed $line
     * @return array
     */
    private function addToLinesIfLineIsValid(array &$lines, $line)
    {
        if (!is_null($line)) {
            $lines[] = $line;
        }

        return $lines;
    }

    /**
     * @param SplFileObject $file
     * @param null|int $newLineNumber
     * @return SplFileObject
     */
    private function seekFileToCurrentLineNumberIfNeeded(SplFileObject $file, $newLineNumber = null)
    {
        $seekIsNeeded = ((!is_null($newLineNumber))
            && ($newLineNumber >= $this->initialLineNumber)
            && ($newLineNumber !== $this->key()));

        if ($seekIsNeeded) {
            $file->seek($newLineNumber);
        }

        return $file;
    }
}
PHP Csv Component by bazzline.net API documentation generated by ApiGen