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: 
<?php
/**
 * @author: stev leibelt <artodeto@bazzline.net>
 * @since: 2015-04-17
 */

namespace Net\Bazzline\Component\Csv\Writer;

use Net\Bazzline\Component\Csv\AbstractBase;
use Net\Bazzline\Component\Csv\InvalidArgumentException;

/**
 * Class Writer
 * @package Net\Bazzline\Component\Csv\Writer
 */
class Writer extends AbstractBase implements WriterInterface
{
    const OPEN_MODE_APPEND      = 'a';
    const OPEN_MODE_TRUNCATE    = 'w';

    /** @var boolean */
    private $useTruncateAsOpenMode = false;

    /**
     * @param mixed|array $data
     * @return false|int
     */
    public function __invoke($data)
    {
        return $this->writeOne($data);
    }


    //begin of general
    /**
     * @param string $path
     * @param bool $setPathAsCurrentPath
     * @return bool
     * @throws InvalidArgumentException
     * @todo implement path validation
     */
    public function copy($path, $setPathAsCurrentPath = false)
    {
        $couldBeCopied = copy($this->getPath(), $path);

        if ($setPathAsCurrentPath) {
            if ($couldBeCopied) {
                $this->close();
                $this->setPath($path);
            }
        }

        return $couldBeCopied;
    }

    /**
     * @param string $path
     * @return bool
     * @todo implement path validation
     */
    public function move($path)
    {
        $couldBeMoved = rename($this->getPath(), $path);

        if ($couldBeMoved) {
            $this->close();
            $this->setPath($path);
        }

        return $couldBeMoved;
    }

    /**
     * @return bool
     */
    public function delete()
    {
        $this->close();

        return unlink($this->getPath());
    }

    public function truncate()
    {
        $this->close();
        $this->useTruncateAsOpenMode = true;
        $this->open($this->getPath());
        $this->useTruncateAsOpenMode = false;
    }

    /**
     * truncates file and writes content
     *
     * @param array $collection
     * @return false|int
     */
    public function writeAll(array $collection)
    {
        $this->truncate();

        return $this->writeMany($collection);
    }

    /**
     * @param array $headlines
     * @return false|int
     */
    public function writeHeadlines(array $headlines)
    {
        $this->setHeadline($headlines);

        return $this->writeOne($headlines);
    }

    /**
     * @param array $collection
     * @return false|int
     */
    public function writeMany(array $collection)
    {
        $lengthOfTheWrittenStrings = 0;

        foreach ($collection as $data) {
            $lengthOfTheWrittenString = $this->writeOne($data);

            if ($lengthOfTheWrittenString === false) {
                $lengthOfTheWrittenStrings = $lengthOfTheWrittenString;
                break;
            } else {
                $lengthOfTheWrittenStrings += $lengthOfTheWrittenString;
            }
        }

        return $lengthOfTheWrittenStrings;
    }

    /**
     * @param string|array $data
     * @return false|int
     */
    public function writeOne($data)
    {
        $data = $this->convertToArrayIfNeeded($data);

        return $this->getFileHandler()->fputcsv($data, $this->getDelimiter(), $this->getEnclosure());
    }
    //end of general

    /**
     * @param string|array $data
     * @return array
     */
    protected function convertToArrayIfNeeded($data)
    {
        if (!is_array($data)) {
            $data = explode($this->getDelimiter(), $data);
            $data = array_map(function($value) {
                return trim($value);
            }, $data);
        }

        return $data;
    }

    /**
     * @return string
     */
    protected function getFileHandlerOpenMode()
    {
        return ($this->useTruncateAsOpenMode) ? self::OPEN_MODE_TRUNCATE : self::OPEN_MODE_APPEND;
    }
}
PHP Csv Component by bazzline.net API documentation generated by ApiGen