-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from keboola/zajca-csv-options
separate csv options to own class
- Loading branch information
Showing
7 changed files
with
183 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
<?php | ||
|
||
namespace Keboola\Csv; | ||
|
||
class CsvOptions | ||
{ | ||
const DEFAULT_DELIMITER = ','; | ||
const DEFAULT_ENCLOSURE = '"'; | ||
const DEFAULT_ESCAPED_BY = ""; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $delimiter; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $enclosure; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private $escapedBy; | ||
|
||
/** | ||
* @param string $delimiter | ||
* @param string $enclosure | ||
* @param string $escapedBy | ||
* @throws InvalidArgumentException | ||
*/ | ||
public function __construct( | ||
$delimiter = self::DEFAULT_DELIMITER, | ||
$enclosure = self::DEFAULT_ENCLOSURE, | ||
$escapedBy = self::DEFAULT_ESCAPED_BY | ||
) { | ||
$this->escapedBy = $escapedBy; | ||
$this->validateDelimiter($delimiter); | ||
$this->delimiter = $delimiter; | ||
$this->validateEnclosure($enclosure); | ||
$this->enclosure = $enclosure; | ||
} | ||
|
||
/** | ||
* @param string $enclosure | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function validateEnclosure($enclosure) | ||
{ | ||
if (strlen($enclosure) > 1) { | ||
throw new InvalidArgumentException( | ||
"Enclosure must be a single character. " . json_encode($enclosure) . " received", | ||
Exception::INVALID_PARAM | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @param string $delimiter | ||
* @throws InvalidArgumentException | ||
*/ | ||
protected function validateDelimiter($delimiter) | ||
{ | ||
if (strlen($delimiter) > 1) { | ||
throw new InvalidArgumentException( | ||
"Delimiter must be a single character. " . json_encode($delimiter) . " received", | ||
Exception::INVALID_PARAM | ||
); | ||
} | ||
|
||
if (strlen($delimiter) == 0) { | ||
throw new InvalidArgumentException( | ||
"Delimiter cannot be empty.", | ||
Exception::INVALID_PARAM | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getEscapedBy() | ||
{ | ||
return $this->escapedBy; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getDelimiter() | ||
{ | ||
return $this->delimiter; | ||
} | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function getEnclosure() | ||
{ | ||
return $this->enclosure; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
namespace Keboola\Csv\Tests; | ||
|
||
use Keboola\Csv\CsvOptions; | ||
use Keboola\Csv\InvalidArgumentException; | ||
|
||
class CsvOptionsTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testAccessors() | ||
{ | ||
$csvFile = new CsvOptions(); | ||
self::assertEquals("\"", $csvFile->getEnclosure()); | ||
self::assertEquals("", $csvFile->getEscapedBy()); | ||
self::assertEquals(",", $csvFile->getDelimiter()); | ||
} | ||
|
||
public function testInvalidDelimiter() | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage('Delimiter must be a single character. ",," received'); | ||
new CsvOptions(",,"); | ||
} | ||
|
||
public function testInvalidDelimiterEmpty() | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage('Delimiter cannot be empty.'); | ||
new CsvOptions(""); | ||
} | ||
|
||
public function testInvalidEnclosure() | ||
{ | ||
self::expectException(InvalidArgumentException::class); | ||
self::expectExceptionMessage('Enclosure must be a single character. ",," received'); | ||
new CsvOptions(CsvOptions::DEFAULT_DELIMITER, ",,"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.