Skip to content

Commit

Permalink
resolve issue #15
Browse files Browse the repository at this point in the history
  • Loading branch information
jabranr committed Feb 9, 2017
1 parent 60ba63c commit 4c284b1
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions src/CSV_Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
*/
class CSV_Parser {

const DEFAULT_ENCODING = 'UTF-8';

/* @var string $data */
protected $data;

Expand All @@ -46,7 +48,7 @@ class CSV_Parser {
*/
public function __construct() {
$this->setData(null);
$this->setEncoding('UTF-8');
$this->setEncoding(static::DEFAULT_ENCODING);
$this->setHeaders(null);
$this->setRows(null);
$this->setColumns(null);
Expand All @@ -66,7 +68,7 @@ public function setData($data = null) {
throw new InvalidArgumentException('Required arguments are missing.');
}

if (null !== $data && ! is_string($data)) {
if (null !== $data && !is_string($data)) {
throw new InvalidDataException('Unexpected data.');
}

Expand Down Expand Up @@ -96,7 +98,7 @@ public function setEncoding($encoding = null) {
throw new InvalidArgumentException('Required arguments are missing.');
}

if (null !== $encoding && ! is_string($encoding)) {
if (null !== $encoding && !is_string($encoding)) {
throw new InvalidEncodingException('Unexpected encoding.');
}

Expand Down Expand Up @@ -130,7 +132,7 @@ public function setHeaders($headers = null) {
throw new InvalidArgumentException('Required arguments are missing.');
}

if (null !== $headers && ! is_array($headers)) {
if (null !== $headers && !is_array($headers)) {
throw new InvalidDataException('Unexpected headers data.');
}

Expand Down Expand Up @@ -160,7 +162,7 @@ public function setColumns($columns = null) {
throw new InvalidArgumentException('Required arguments are missing.');
}

if (null !== $columns && ! is_array($columns)) {
if (null !== $columns && !is_array($columns)) {
throw new InvalidDataException('Unexpected columns data.');
}

Expand Down Expand Up @@ -190,7 +192,7 @@ public function setRows($rows = null) {
throw new InvalidArgumentException('Required arguments are missing.');
}

if (null !== $rows && ! is_array($rows)) {
if (null !== $rows && !is_array($rows)) {
throw new InvalidDataException('Unexpected rows data.');
}

Expand Down Expand Up @@ -230,11 +232,11 @@ public function fromFile($file = null) {
* @since 2.0.2
*/
public function fromPath($path = null) {
if (null === $path || ! is_string($path)) {
if (null === $path || !is_string($path)) {
throw new InvalidPathException('Invalid resource path.');
}

if (! file_exists($path) || ! is_readable($path)) {
if (!file_exists($path) || !is_readable($path)) {
throw new InvalidAccessException('Unable to retrieve the resource.');
}

Expand Down Expand Up @@ -263,7 +265,7 @@ public function fromPath($path = null) {
* @return self
*/
public function fromString($string = null) {
if (null === $string || ! is_string($string)) {
if (null === $string || !is_string($string)) {
throw new InvalidDataTypeException('Invalid or unexpected data.');
}

Expand Down Expand Up @@ -296,7 +298,7 @@ public function fromStream($stream = null) {
* @since 2.0.2
*/
public function fromResource($resource = null) {
if (null === $resource || ! is_resource($resource)) {
if (null === $resource || !is_resource($resource)) {
throw new InvalidResourceException('Invalid or unexpected resource.');
}

Expand Down Expand Up @@ -332,22 +334,19 @@ public function parse($headers = true) {

/**
* Convert character encoding of data
*
* @throws Jabran\Exception\InvalidEncodingException
* @return self
*/
public function encode() {
$data = $this->getData();
$this->setData(mb_convert_encoding(
$data,
$this->getEncoding(),
mb_detect_encoding($data)
));
$encodedData = mb_convert_encoding($data, $this->getEncoding(), mb_detect_encoding($this->getData()));
$this->setData($encodedData);

if ($this->getEncoding() !== mb_detect_encoding($this->getData())) {
throw new InvalidEncodingException(
'Unable to convert character encoding from '. mb_detect_encoding($this->getData()).
' to '.$this->getEncoding().'.'
);
throw new InvalidEncodingException(
sprintf('Unable to convert character encoding from "%s" to "%s".', mb_detect_encoding($this->getData()), $this->getEncoding())
);
}

return $this;
Expand Down Expand Up @@ -413,7 +412,7 @@ private function _makeRows() {
$columns = $this->getColumns();
$rows = array();

if (! is_array($columns) || count($columns) < 1) {
if (!is_array($columns) || count($columns) < 1) {
$this->setRows($rows);
return $this;
}
Expand Down

0 comments on commit 4c284b1

Please sign in to comment.