Make CSV reader return Row objects

This commit is contained in:
Adrien Loison committed 2017-11-18 19:08:27 +01:00
1 parent 139f7fdfb3
commit a665b974fa
37 files changed
+664 -214

No files matched your search

@@ -0,0 +1,25 @@
<?php
namespace Box\Spout\Reader\Common\Creator;
use Box\Spout\Reader\ReaderFactory;
use Box\Spout\Reader\ReaderInterface;
/**
* Class EntityFactory
* Factory to create external entities
*/
class EntityFactory
{
/**
* This creates an instance of the appropriate reader, given the type of the file to be read
*
* @param string $readerType Type of the reader to instantiate
* @throws \Box\Spout\Common\Exception\UnsupportedTypeException
* @return ReaderInterface
*/
public static function createReader($readerType)
{
return (new ReaderFactory())->create($readerType);
}
}
@@ -5,6 +5,6 @@ namespace Box\Spout\Reader\Common\Creator;
/**
* Interface EntityFactoryInterface
*/
interface EntityFactoryInterface
interface InternalEntityFactoryInterface
{
}
+159
View File
@@ -0,0 +1,159 @@
<?php
namespace Box\Spout\Reader\Common\Entity;
use Box\Spout\Common\Helper\CellTypeHelper;
/**
* Class Cell
*/
class Cell
{
/**
* Numeric cell type (whole numbers, fractional numbers, dates)
*/
const TYPE_NUMERIC = 0;
/**
* String (text) cell type
*/
const TYPE_STRING = 1;
/**
* Formula cell type
* Not used at the moment
*/
const TYPE_FORMULA = 2;
/**
* Empty cell type
*/
const TYPE_EMPTY = 3;
/**
* Boolean cell type
*/
const TYPE_BOOLEAN = 4;
/**
* Error cell type
*/
const TYPE_ERROR = 5;
/**
* The value of this cell
* @var mixed|null
*/
protected $value;
/**
* The cell type
* @var int|null
*/
protected $type;
/**
* @param $value mixed
*/
public function __construct($value)
{
$this->setValue($value);
}
/**
* @param mixed|null $value
*/
public function setValue($value)
{
$this->value = $value;
$this->type = $this->detectType($value);
}
/**
* @return mixed|null
*/
public function getValue()
{
return $this->value;
}
/**
* @return int|null
*/
public function getType()
{
return $this->type;
}
/**
* Get the current value type
*
* @param mixed|null $value
* @return int
*/
protected function detectType($value)
{
if (CellTypeHelper::isBoolean($value)) {
return self::TYPE_BOOLEAN;
}
if (CellTypeHelper::isEmpty($value)) {
return self::TYPE_EMPTY;
}
if (CellTypeHelper::isNumeric($this->getValue())) {
return self::TYPE_NUMERIC;
}
if (CellTypeHelper::isNonEmptyString($value)) {
return self::TYPE_STRING;
}
return self::TYPE_ERROR;
}
/**
* @return bool
*/
public function isBoolean()
{
return $this->type === self::TYPE_BOOLEAN;
}
/**
* @return bool
*/
public function isEmpty()
{
return $this->type === self::TYPE_EMPTY;
}
/**
* @return bool
*/
public function isNumeric()
{
return $this->type === self::TYPE_NUMERIC;
}
/**
* @return bool
*/
public function isString()
{
return $this->type === self::TYPE_STRING;
}
/**
* @return bool
*/
public function isError()
{
return $this->type === self::TYPE_ERROR;
}
/**
* @return string
*/
public function __toString()
{
return (string) $this->value;
}
}
+64
View File
@@ -0,0 +1,64 @@
<?php
namespace Box\Spout\Reader\Common\Entity;
class Row
{
/**
* The cells in this row
* @var Cell[]
*/
protected $cells = [];
/**
* Row constructor.
* @param Cell[] $cells
*/
public function __construct(array $cells)
{
$this->setCells($cells);
}
/**
* @return Cell[] $cells
*/
public function getCells()
{
return $this->cells;
}
/**
* @param Cell[] $cells
* @return $this
*/
public function setCells(array $cells)
{
$this->cells = [];
foreach ($cells as $cell) {
$this->addCell($cell);
}
return $this;
}
/**
* @param Cell $cell
* @return Row
*/
public function addCell(Cell $cell)
{
$this->cells[] = $cell;
return $this;
}
/**
* @return array The row values, as array
*/
public function toArray()
{
return array_map(function (Cell $cell) {
return $cell->getValue();
}, $this->cells);
}
}