csv/src/Deblan/Csv/Csv.php

342 lines
6.2 KiB
PHP
Raw Permalink Normal View History

2015-03-09 18:50:07 +01:00
<?php
namespace Deblan\Csv;
2017-03-12 15:04:19 +01:00
/**
* class Csv.
*
* @author Simon Vieille <simon@deblan.fr>
*/
2015-03-09 18:50:07 +01:00
class Csv
{
2017-03-12 15:04:19 +01:00
/**
* @var string
*/
protected $delimiter = ';';
/**
* @var string
*/
protected $enclosure = '"';
/**
* @var string
*/
protected $endOfLine = "\n";
/**
* @var array
*/
protected $datas = [];
/**
* @var array
*/
protected $headers = [];
/**
* @var string
*/
protected $charset = 'UTF-8';
/**
* @var bool
*/
protected $isModified = false;
/**
* @var string
*/
protected $render;
/**
* Set the value of "delimiter".
*
* @param string $delimiter
*
* @return Csv
*/
public function setDelimiter($delimiter)
{
if ($this->delimiter !== $delimiter) {
$this->delimiter = $delimiter;
$this->isModified = true;
}
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
return $this;
}
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
/**
* Get the value of "delimiter".
*
* @return array
*/
public function getDelimiter()
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->delimiter;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Set the value of "enclosure".
*
* @param string $enclosure
*
* @return Csv
*/
public function setEnclosure($enclosure)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
$enclosure = (string) $enclosure;
if ($this->enclosure !== $enclosure) {
$this->enclosure = $enclosure;
$this->isModified = true;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Get the value of "enclosure".
*
* @return string
*/
public function getEnclosure()
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->enclosure;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Set the value of "endOfLine".
*
* @param string $endOfLine
*
* @return Csv
*/
public function setEndOfLine($endOfLine)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
if ($this->endOfLine !== $endOfLine) {
$this->endOfLine = (string) $endOfLine;
$this->isModified = true;
}
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Get the value of "endOfLine".
*
* @return string
*/
public function getEndOfLine()
2015-03-09 20:06:08 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->endOfLine;
2015-03-09 20:06:08 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Set the value of "headers".
*
* @param array $headers
*
* @return Csv
*/
public function setHeaders(array $headers)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
$this->headers = $headers;
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
if ($this->headers !== $headers) {
$this->headers = $headers;
$this->isModified = true;
}
2016-05-19 13:41:56 +02:00
2017-03-12 15:04:19 +01:00
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Get the value of "headers".
*
* @return array
*/
public function getHeaders()
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->headers;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Set the value of "charset".
*
* @param string $charset
*
* @return Csv
*/
public function setCharset($charset)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
if ($this->charset !== $charset) {
$this->charset = (string) $charset;
$this->isModified = true;
}
2015-03-09 18:50:07 +01:00
return $this;
}
2017-03-12 15:04:19 +01:00
/**
* Get the value of "charset".
*
* @return string
*/
public function getCharset()
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->charset;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Sets the value of "datas".
*
* @param array $datas
*
* @return Csv
*/
public function setDatas(array $datas)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
if ($this->datas !== $datas) {
$this->datas = $datas;
$this->isModified = true;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Get the value of "datas".
*
* @return array
*/
public function getDatas()
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->datas;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Appends data.
*
* @param array $data
*
* @return Csv
*/
public function appendData(array $data)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
$this->datas[] = $data;
$this->isModified = true;
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Alias of "appendData()".
*
* {@inheritdoc self::appendData()}
*/
public function addData(array $data)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return $this->appendData($data);
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Prepends data.
*
* @param array $data
*
* @return Csv
*/
public function preprendData(array $data)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
array_unshift($this->datas, $data);
$this->isModified = true;
return $this;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/**
* Formats an array data to a CSV string.
*
* @param array $data
*
* @return array
*/
protected function formatData(array $data)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
$columns = [];
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
foreach ($data as $value) {
$value = (string) $value;
if (!empty($this->enclosure)) {
$value = str_replace($this->enclosure, str_repeat($this->enclosure, 2), $value);
}
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
$value = sprintf('%1$s%2$s%1$s', $this->enclosure, (string) $value);
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
$columns[] = $value;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
$data = implode($this->delimiter, $columns);
$data = $this->encode($data);
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
return $data;
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Changes the charset if needed.
*
* @param string $value
*
* @return string
*/
public function encode($value)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
return mb_convert_encoding(
$value,
$this->charset,
mb_detect_encoding($value, mb_list_encodings())
);
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
/*
* Renders the CSV.
*
* @param string $filename @see file_put_contents
* @param int $flags @see file_put_contents
*
* @return string
*/
public function render($filename = null, $flags = null)
2015-03-09 18:50:07 +01:00
{
2017-03-12 15:04:19 +01:00
if ($this->isModified || empty($this->render)) {
$lines = [];
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
if (!empty($this->headers)) {
$lines[] = $this->formatData($this->headers);
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
foreach ($this->datas as $data) {
$lines[] = $this->formatData($data);
}
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
$this->render = implode($this->encode($this->endOfLine), $lines);
}
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
$this->isModified = false;
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
if ($filename !== null) {
$content = $this->render;
2015-03-09 18:50:07 +01:00
2017-03-12 15:04:19 +01:00
if ($flags === FILE_APPEND && file_exists($filename)) {
$content = $this->endOfLine.$content;
}
2017-03-12 15:20:09 +01:00
2017-03-12 15:23:41 +01:00
file_put_contents($filename, $content, $flags);
2015-03-09 18:50:07 +01:00
}
2017-03-12 15:04:19 +01:00
return $this->render;
2015-03-09 18:50:07 +01:00
}
}