Skip to content

Commit

Permalink
Improve RFC4180 property validation
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Sep 24, 2018
1 parent 7e89821 commit eec61a4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
46 changes: 31 additions & 15 deletions src/RFC4180Parser.php
Expand Up @@ -75,26 +75,42 @@ final class RFC4180Parser implements IteratorAggregate
*/
public function __construct($document, string $delimiter = ',', string $enclosure = '"')
{
if (!$document instanceof Stream && !$document instanceof SplFileObject) {
throw new TypeError(sprintf(
'Expected a %s or an SplFileObject object, % given',
Stream::class,
is_object($document) ? get_class($document) : gettype($document)
));
}
$this->document = $this->filterDocument($document);
$this->delimiter = $this->filterControl($delimiter, 'delimiter');
$this->enclosure = $this->filterControl($enclosure, 'enclosure');
$this->trim_mask = str_replace([$this->delimiter, $this->enclosure], '', " \t\0\x0B");
}

if (1 !== strlen($delimiter)) {
throw new Exception(sprintf('%s() expects delimiter to be a single character %s given', __METHOD__, $delimiter));
/**
* Filter the submitted document.
*
* @param SplFileObject|Stream $document
*
* @return SplFileObject|Stream
*/
private function filterDocument($document)
{
if ($document instanceof Stream || $document instanceof SplFileObject) {
return $document;
}

if (1 !== strlen($enclosure)) {
throw new Exception(sprintf('%s() expects enclosure to be a single character %s given', __METHOD__, $enclosure));
throw new TypeError(sprintf(
'Expected a %s or an SplFileObject object, % given',
Stream::class,
is_object($document) ? get_class($document) : gettype($document)
));
}

/**
* Filter the control characters.
*/
private function filterControl(string $control, string $name): string
{
if (1 === strlen($control)) {
return $control;
}

$this->document = $document;
$this->delimiter = $delimiter;
$this->enclosure = $enclosure;
$this->trim_mask = str_replace([$this->delimiter, $this->enclosure], '', " \t\0\x0B");
throw new Exception(sprintf('Expected %s to be a single character %s given', $name, $control));
}

/**
Expand Down
4 changes: 4 additions & 0 deletions tests/RFC4180ParserTest.php
Expand Up @@ -31,6 +31,7 @@ class RFC4180ParserTest extends TestCase
{
/**
* @covers ::__construct
* @covers ::filterDocument
*/
public function testConstructorThrowsTypeErrorWithUnknownDocument()
{
Expand All @@ -40,6 +41,7 @@ public function testConstructorThrowsTypeErrorWithUnknownDocument()

/**
* @covers ::__construct
* @covers ::filterControl
*/
public function testConstructorThrowExceptionWithInvalidDelimiter()
{
Expand All @@ -49,6 +51,7 @@ public function testConstructorThrowExceptionWithInvalidDelimiter()

/**
* @covers ::__construct
* @covers ::filterControl
*/
public function testConstructorThrowExceptionWithInvalidEnclosure()
{
Expand All @@ -59,6 +62,7 @@ public function testConstructorThrowExceptionWithInvalidEnclosure()
/**
* @covers \League\Csv\Stream::fgets
* @covers ::__construct
* @covers ::filterDocument
* @covers ::getIterator
* @covers ::extractFieldContent
* @covers ::extractEnclosedFieldContent
Expand Down

0 comments on commit eec61a4

Please sign in to comment.