Skip to content

Commit

Permalink
remove ValidatorTrait
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 16, 2017
1 parent 4b6f1b1 commit fcab5ba
Show file tree
Hide file tree
Showing 12 changed files with 134 additions and 122 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ All Notable changes to `Csv` will be documented in this file
- `League\Csv\HTMLConverter` converts CSV records into HTML table.
- Improved Exception handling
- `League\Csv\Exception` the default exception
- `League\Csv\Exception\CannotInsertRecord`
- `League\Csv\CannotInsertRecord`
- Improved CSV document output
- `League\Csv\AbstractCsv::chunk` method to output the CSV document in chunk
- `League\Csv\bom_match` function to detect BOM sequence in a given string
Expand All @@ -32,7 +32,7 @@ All Notable changes to `Csv` will be documented in this file
- `League\Csv\Writer::setFlushThreshold`
- `League\Csv\Writer::getFlushThreshold`
- Improve RFC4180 compliance
- `League\Csv\RFC4180FieldFormatter` to format field according to RFC4180 rules
- `League\Csv\RFC4180Field` to format field according to RFC4180 rules

### Deprecated

Expand Down
44 changes: 33 additions & 11 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
*/
abstract class AbstractCsv implements ByteSequence
{
use ValidatorTrait;

/**
* The stream filter mode (read or write)
*
Expand Down Expand Up @@ -325,16 +323,24 @@ public function output(string $filename = null): int
*
* @param string $delimiter
*
* @throws Exception If the Csv control character is not one character only.
*
* @return static
*/
public function setDelimiter(string $delimiter): self
{
if ($delimiter != $this->delimiter) {
$this->delimiter = $this->filterControl($delimiter, 'delimiter', __METHOD__);
if ($delimiter === $this->delimiter) {
return $this;
}

if (1 === strlen($delimiter)) {
$this->delimiter = $delimiter;
$this->resetProperties();

return $this;
}

return $this;
throw new Exception(sprintf('%s() expects delimiter to be a single character %s given', __METHOD__, $delimiter));
}

/**
Expand All @@ -349,33 +355,49 @@ protected function resetProperties()
*
* @param string $enclosure
*
* @throws Exception If the Csv control character is not one character only.
*
* @return static
*/
public function setEnclosure(string $enclosure): self
{
if ($enclosure != $this->enclosure) {
$this->enclosure = $this->filterControl($enclosure, 'enclosure', __METHOD__);
if ($enclosure === $this->enclosure) {
return $this;
}

if (1 === strlen($enclosure)) {
$this->enclosure = $enclosure;
$this->resetProperties();

return $this;
}

return $this;
throw new Exception(sprintf('%s() expects enclosure to be a single character %s given', __METHOD__, $enclosure));
}

/**
* Sets the field escape character
*
* @param string $escape
*
* @throws Exception If the Csv control character is not one character only.
*
* @return static
*/
public function setEscape(string $escape): self
{
if ($escape != $this->escape) {
$this->escape = $this->filterControl($escape, 'escape', __METHOD__);
if ($escape === $this->escape) {
return $this;
}

if (1 === strlen($escape)) {
$this->escape = $escape;
$this->resetProperties();

return $this;
}

return $this;
throw new Exception(sprintf('%s() expects escape to be a single character %s given', __METHOD__, $escape));
}

/**
Expand Down
18 changes: 14 additions & 4 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use IteratorAggregate;
use JsonSerializable;
use SplFileObject;
use TypeError;

/**
* A class to manage records selection from a CSV document
Expand Down Expand Up @@ -308,12 +309,21 @@ protected function stripBOM(Iterator $iterator, string $bom): Iterator
*/
public function setHeaderOffset($offset): self
{
$this->filterNullableInteger($offset, 0, __METHOD__.'() expects the header offset index to be a positive integer or 0');
if ($offset !== $this->header_offset) {
$this->header_offset = $offset;
$this->resetProperties();
if ($offset === $this->header_offset) {
return $this;
}

if (!is_nullable_int($offset)) {
throw new TypeError(sprintf(__METHOD__.'() expects 1 Argument to be null or an integer %s given', gettype($offset)));
}

if (null !== $offset && 0 > $offset) {
throw new Exception(__METHOD__.'() expects 1 Argument to be greater or equal to 0');
}

$this->header_offset = $offset;
$this->resetProperties();

return $this;
}

Expand Down
5 changes: 1 addition & 4 deletions src/ResultSet.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,10 +169,7 @@ public function fetchColumn($index = 0): Generator
*/
protected function getColumnIndex($field, string $error_message)
{
$method = 'getColumnIndexByKey';
if (is_string($field)) {
$method = 'getColumnIndexByValue';
}
$method = is_string($field) ? 'getColumnIndexByValue' : 'getColumnIndexByKey';

return $this->$method($field, $error_message);
}
Expand Down
45 changes: 33 additions & 12 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@
*/
class Stream implements SeekableIterator
{
use ValidatorTrait;

/**
* Attached filters
*
Expand Down Expand Up @@ -238,9 +236,36 @@ public function appendFilter(string $filtername, int $read_write, $params = null
*/
public function setCsvControl(string $delimiter = ',', string $enclosure = '"', string $escape = '\\')
{
$this->delimiter = $this->filterControl($delimiter, 'delimiter', __METHOD__);
$this->enclosure = $this->filterControl($enclosure, 'enclosure', __METHOD__);
$this->escape = $this->filterControl($escape, 'escape', __METHOD__);
list($this->delimiter, $this->enclosure, $this->escape) = $this->filterControl($delimiter, $enclosure, $escape, __METHOD__);
}

/**
* Filter Csv control characters
*
* @param string $delimiter CSV delimiter character
* @param string $enclosure CSV enclosure character
* @param string $escape CSV escape character
* @param string $caller public API method calling the method
*
* @throws Exception If the Csv control character is not one character only.
*
* @return array
*/
protected function filterControl(string $delimiter, string $enclosure, string $escape, string $caller): array
{
if (1 !== strlen($delimiter)) {
throw new Exception(sprintf('%s() expects delimiter to be a single character', $caller));
}

if (1 !== strlen($enclosure)) {
throw new Exception(sprintf('%s() expects enclosure to be a single character', $caller));
}

if (1 !== strlen($escape)) {
throw new Exception(sprintf('%s() expects escape to be a single character', $caller));
}

return [$delimiter, $enclosure, $escape];
}

/**
Expand Down Expand Up @@ -281,13 +306,9 @@ public function setFlags(int $flags)
*/
public function fputcsv(array $fields, string $delimiter = ',', string $enclosure = '"', string $escape = '\\')
{
return fputcsv(
$this->stream,
$fields,
$this->filterControl($delimiter, 'delimiter', __METHOD__),
$this->filterControl($enclosure, 'enclosure', __METHOD__),
$this->filterControl($escape, 'escape', __METHOD__)
);
$controls = $this->filterControl($delimiter, $enclosure, $escape, __METHOD__);

return fputcsv($this->stream, $fields, ...$controls);
}

/**
Expand Down
75 changes: 0 additions & 75 deletions src/ValidatorTrait.php

This file was deleted.

15 changes: 10 additions & 5 deletions src/Writer.php
Original file line number Diff line number Diff line change
Expand Up @@ -241,17 +241,22 @@ public function setNewline(string $newline): self
*/
public function setFlushThreshold($threshold): self
{
$this->filterNullableInteger($threshold, 1, __METHOD__.'() expects 1 Argument to be null or a valid integer greater or equal to 1');
if ($threshold === $this->flush_threshold) {
return $this;
}

$this->flush_threshold = $threshold;
if (0 < $this->flush_counter) {
$this->flush_counter = 0;
$this->document->fflush();
if (!is_nullable_int($threshold)) {
throw new TypeError(sprintf(__METHOD__.'() expects 1 Argument to be null or an integer %s given', gettype($threshold)));
}

if (null !== $threshold && 1 >= $threshold) {
throw new Exception(__METHOD__.'() expects 1 Argument to be null or a valid integer greater or equal to 1');
}

$this->flush_threshold = $threshold;
$this->flush_counter = 0;
$this->document->fflush();

return $this;
}
}
18 changes: 16 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,11 +78,11 @@ function delimiter_detect(Reader $csv, array $delimiters, int $limit = 1): array

if (!function_exists('\is_iterable')) {
/**
* Tell whether the contents of the variable is iterable
* Tell whether the content of the variable is iterable
*
* @see http://php.net/manual/en/function.is-iterable.php
*
* @param array|Traversable $iterable
* @param mixed $iterable
*
* @return bool
*/
Expand All @@ -91,3 +91,17 @@ function is_iterable($iterable): bool
return is_array($iterable) || $iterable instanceof Traversable;
}
}

/**
* Tell whether the content of the variable is an int or null
*
* @see https://wiki.php.net/rfc/nullable_types
*
* @param mixed $value
*
* @return bool
*/
function is_nullable_int($value): bool
{
return null === $value || is_int($value);
}
4 changes: 3 additions & 1 deletion tests/CsvTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,13 +194,13 @@ public function testStreamFilterMode()
/**
* @covers ::getDelimiter
* @covers ::setDelimiter
* @covers ::filterControl
*/
public function testDelimeter()
{
$this->expectException(Exception::class);
$this->csv->setDelimiter('o');
$this->assertSame('o', $this->csv->getDelimiter());
$this->assertSame($this->csv, $this->csv->setDelimiter('o'));
$this->csv->setDelimiter('foo');
}

Expand Down Expand Up @@ -251,6 +251,7 @@ public function testEscape()
$this->expectException(Exception::class);
$this->csv->setEscape('o');
$this->assertSame('o', $this->csv->getEscape());
$this->assertSame($this->csv, $this->csv->setEscape('o'));

$this->csv->setEscape('foo');
}
Expand All @@ -264,6 +265,7 @@ public function testEnclosure()
$this->expectException(Exception::class);
$this->csv->setEnclosure('o');
$this->assertSame('o', $this->csv->getEnclosure());
$this->assertSame($this->csv, $this->csv->setEnclosure('o'));

$this->csv->setEnclosure('foo');
}
Expand Down
Loading

0 comments on commit fcab5ba

Please sign in to comment.