This commit is contained in:
Andrea Bontempi 2021-12-23 11:53:46 +01:00
parent 4a44fc1ec2
commit c46d884740
5 changed files with 43 additions and 25 deletions

2
.gitignore vendored
View file

@ -1 +1,3 @@
/.idea/
/vendor/
composer.lock

View file

@ -107,11 +107,11 @@ class Csv implements Iterator {
$csvColumnIndex = 0;
foreach (str_getcsv($this->currentLine, $this->separator, $this->enclosure, $this->escape) as $csvColumnName) {
if(!array_key_exists($csvColumnName, $this->headerList)) {
$this->headerList[$csvColumnName] = (new HeaderColumn())->setName($csvColumnName);
$this->headerList[$csvColumnName] = new HeaderColumn($csvColumnName);
}
$this->headerList[$csvColumnName]->setIndex($csvColumnIndex);
if(array_key_exists($csvColumnName, $expectedColumns)) {
unset($expectedColumns[$csvColumnName]);
if(in_array($csvColumnName, $expectedColumns)) {
unset($expectedColumns[array_search($csvColumnName, $expectedColumns)]);
}
$csvColumnIndex++;
}
@ -134,9 +134,9 @@ class Csv implements Iterator {
}
/**
* @return array|CsvLine
* @return mixed
*/
public function current() {
public function current() : mixed {
$line = str_getcsv($this->currentLine, $this->separator, $this->enclosure, $this->escape);
if(!$this->hasHeader) return $line;
return new CsvLine($line, $this->currentRow, $this->headerList);
@ -152,7 +152,7 @@ class Csv implements Iterator {
/**
* @return void
*/
public function next() {
public function next() : void {
$this->currentLine = fgets($this->fp, $this->length);
$this->currentRow++;
}
@ -160,7 +160,7 @@ class Csv implements Iterator {
/**
* @return void
*/
public function rewind() {
public function rewind() : void {
fseek($this->fp, $this->dataStartPoint, SEEK_SET);
$this->currentRow = 0;
$this->next();

View file

@ -9,6 +9,7 @@ use PrettyCSV\Interfaces\TypeParserInterface;
use PrettyCSV\Parsers\ParserException;
use ArrayObject;
use ArrayIterator;
use Iterator;
class CsvLine extends ArrayObject {
@ -25,7 +26,7 @@ class CsvLine extends ArrayObject {
/**
* @var HeaderColumnInterface[]
*/
protected array $headers = [];
protected array $headerList = [];
/**
* @var array
@ -35,38 +36,38 @@ class CsvLine extends ArrayObject {
/**
* @param array $currentLine
* @param int $currentRow
* @param array $headers
* @param array $headerList
*/
public function __construct(
array $currentLine,
int $currentRow,
array $headers
array $headerList
){
$this->currentLine = $currentLine;
$this->currentRow = $currentRow;
$this->headers = $headers;
$this->headerList = $headerList;
}
/**
* @param string $name
* @return bool
*/
private function existsField(string $name) {
return array_key_exists($name, $this->headers) && !is_null($this->headers[$name]->getIndex());
private function existsField(string $name): bool {
return array_key_exists($name, $this->headerList) && !is_null($this->headerList[$name]->getIndex());
}
/**
* @param string $name
* @return mixed|null
* @return mixed
* @throws ParserException
*/
private function getField(string $name) {
private function getField(string $name): mixed {
if(!$this->existsField($name)) {
throw new ParserException("Field '{$name}' not found", $this->currentRow);
}
$index = $this->headers[$name]->getIndex();
$index = $this->headerList[$name]->getIndex();
/** @var TypeParserInterface $parser */
$parser = $this->headers[$name]->getParser();
$parser = $this->headerList[$name]->getParser();
if(!array_key_exists($name, $this->parsedDataCache)) {
if(!array_key_exists($index, $this->currentLine)) return null;
$data = $this->currentLine[$index];
@ -95,10 +96,10 @@ class CsvLine extends ArrayObject {
/**
* @param $key
* @return mixed|null
* @return mixed
* @throws ParserException
*/
public function offsetGet($key) {
public function offsetGet($key) : mixed {
return $this->getField($key);
}
@ -116,8 +117,8 @@ class CsvLine extends ArrayObject {
*/
public function getArrayCopy(): array {
$result = [];
foreach ($this->headers as $name => $header) {
if (!is_null($header->getIndex())) {
foreach ($this->headerList as $name => $header) {
if (is_null($header->getIndex())) {
continue;
}
$result[$name] = $this->getField($name);
@ -126,10 +127,10 @@ class CsvLine extends ArrayObject {
}
/**
* @return ArrayIterator
* @return Iterator
* @throws ParserException
*/
public function getIterator() {
public function getIterator() : Iterator {
return new ArrayIterator($this->getArrayCopy(), $this->getFlags());
}
@ -137,7 +138,7 @@ class CsvLine extends ArrayObject {
* @return array
* @throws ParserException
*/
public function __debugInfo() {
public function __debugInfo() : array {
return $this->getArrayCopy();
}

View file

@ -29,6 +29,21 @@ class HeaderColumn implements HeaderColumnInterface {
*/
private ?TypeParserInterface $parser;
/**
* @param string $name
* @param bool $is_required
* @param TypeParserInterface|null $parser
*/
public function __construct(
string $name,
bool $is_required = false,
?TypeParserInterface $parser = null
) {
$this->name = $name;
$this->is_required = $is_required;
$this->parser = $parser;
}
/**
* @inheritDoc
*/

View file

@ -7,7 +7,7 @@ namespace PrettyCSV\Parsers;
use PrettyCSV\Interfaces\TypeParserInterface;
use DateTime;
class DateTimeParser implements TypeParserInterface {
class ListParser implements TypeParserInterface {
/**
* @var string