Skip to content

Commit

Permalink
Adding an autoloader script
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed May 23, 2016
1 parent 6bc7868 commit 86821ef
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 190 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ All Notable changes to `Csv` will be documented in this file

### Added

- The package now includes its own autoloader.
- `Ouput::getInputEncoding`
- `Ouput::setInputEncoding`

Expand All @@ -17,6 +18,7 @@ All Notable changes to `Csv` will be documented in this file
### Fixed

- Stream Filters are now url encoded before usage [issue #72](https://github.com/thephpleague/csv/issues/72)
- All parameters are now using the snake case format

### Removed

Expand Down
21 changes: 21 additions & 0 deletions autoload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

spl_autoload_register(function ($class) {

$prefix = 'League\Csv\\';
if (0 !== strpos($class, $prefix)) {
return;
}

$file = __DIR__
.DIRECTORY_SEPARATOR
.'src'
.DIRECTORY_SEPARATOR
.str_replace('\\', DIRECTORY_SEPARATOR, substr($class, strlen($prefix)))
.'.php';
if (!is_readable($file)) {
return;
}

require $file;
});
46 changes: 23 additions & 23 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
*
* @var string
*/
protected $openMode;
protected $open_mode;

/**
* Creates a new instance
Expand All @@ -88,12 +88,12 @@ abstract class AbstractCsv implements JsonSerializable, IteratorAggregate
* an object that implements the `__toString` method
* a path to a file
*
* @param SplFileObject|string $path The file path
* @param string $openMode The file open mode flag
* @param SplFileObject|string $path The file path
* @param string $open_mode The file open mode flag
*/
protected function __construct($path, $openMode = 'r+')
protected function __construct($path, $open_mode = 'r+')
{
$this->openMode = strtolower($openMode);
$this->open_mode = strtolower($open_mode);
$this->path = $path;
$this->initStreamFilter($this->path);
}
Expand Down Expand Up @@ -156,14 +156,14 @@ protected static function validateString($str)
/**
* Return a new {@link AbstractCsv} from a string
*
* @param mixed $path file path
* @param string $openMode the file open mode flag
* @param mixed $path file path
* @param string $open_mode the file open mode flag
*
* @throws InvalidArgumentException If $path is a SplTempFileObject object
*
* @return static
*/
public static function createFromPath($path, $openMode = 'r+')
public static function createFromPath($path, $open_mode = 'r+')
{
if ($path instanceof SplTempFileObject) {
throw new InvalidArgumentException('an `SplTempFileObject` object does not contain a valid path');
Expand All @@ -173,26 +173,26 @@ public static function createFromPath($path, $openMode = 'r+')
$path = $path->getPath().'/'.$path->getBasename();
}

return new static(static::validateString($path), $openMode);
return new static(static::validateString($path), $open_mode);
}

/**
* Return a new {@link AbstractCsv} instance from another {@link AbstractCsv} object
*
* @param string $class the class to be instantiated
* @param string $openMode the file open mode flag
* @param string $class the class to be instantiated
* @param string $open_mode the file open mode flag
*
* @return static
*/
protected function newInstance($class, $openMode)
protected function newInstance($class, $open_mode)
{
$csv = new $class($this->path, $openMode);
$csv = new $class($this->path, $open_mode);
$csv->delimiter = $this->delimiter;
$csv->enclosure = $this->enclosure;
$csv->escape = $this->escape;
$csv->inputEncoding = $this->inputEncoding;
$csv->inputBom = $this->inputBom;
$csv->outputBom = $this->outputBom;
$csv->input_encoding = $this->input_encoding;
$csv->input_bom = $this->input_bom;
$csv->output_bom = $this->output_bom;
$csv->newline = $this->newline;

return $csv;
Expand All @@ -201,25 +201,25 @@ protected function newInstance($class, $openMode)
/**
* Return a new {@link Writer} instance from a {@link AbstractCsv} object
*
* @param string $openMode the file open mode flag
* @param string $open_mode the file open mode flag
*
* @return Writer
*/
public function newWriter($openMode = 'r+')
public function newWriter($open_mode = 'r+')
{
return $this->newInstance(Writer::class, $openMode);
return $this->newInstance(Writer::class, $open_mode);
}

/**
* Return a new {@link Reader} instance from a {@link AbstractCsv} object
*
* @param string $openMode the file open mode flag
* @param string $open_mode the file open mode flag
*
* @return Reader
*/
public function newReader($openMode = 'r+')
public function newReader($open_mode = 'r+')
{
return $this->newInstance(Reader::class, $openMode);
return $this->newInstance(Reader::class, $open_mode);
}

/**
Expand All @@ -231,7 +231,7 @@ public function getIterator()
{
$iterator = $this->path;
if (!$iterator instanceof SplFileObject) {
$iterator = new SplFileObject($this->getStreamFilterPath(), $this->openMode);
$iterator = new SplFileObject($this->getStreamFilterPath(), $this->open_mode);
}
$iterator->setCsvControl($this->delimiter, $this->enclosure, $this->escape);
$iterator->setFlags(SplFileObject::READ_CSV | SplFileObject::READ_AHEAD | SplFileObject::SKIP_EMPTY);
Expand Down
10 changes: 5 additions & 5 deletions src/Config/Controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,22 +102,22 @@ public function getDelimiter()
* a valid delimiter and each value the number of occurences
*
* @param string[] $delimiters the delimiters to consider
* @param int $nbRows Detection is made using $nbRows of the CSV
* @param int $nb_rows Detection is made using $nb_rows of the CSV
*
* @return array
*/
public function fetchDelimitersOccurrence(array $delimiters, $nbRows = 1)
public function fetchDelimitersOccurrence(array $delimiters, $nb_rows = 1)
{
$nbRows = $this->validateInteger($nbRows, 1, 'The number of rows to consider must be a valid positive integer');
$filterRow = function ($row) {
$nb_rows = $this->validateInteger($nb_rows, 1, 'The number of rows to consider must be a valid positive integer');
$filter_row = function ($row) {
return is_array($row) && count($row) > 1;
};
$delimiters = array_unique(array_filter($delimiters, [$this, 'isValidCsvControls']));
$csv = $this->getIterator();
$res = [];
foreach ($delimiters as $delim) {
$csv->setCsvControl($delim, $this->enclosure, $this->escape);
$iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nbRows), $filterRow);
$iterator = new CallbackFilterIterator(new LimitIterator($csv, 0, $nb_rows), $filter_row);
$res[$delim] = count(iterator_to_array($iterator, false), COUNT_RECURSIVE);
}
arsort($res, SORT_NUMERIC);
Expand Down
60 changes: 30 additions & 30 deletions src/Config/Output.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,19 +33,19 @@ trait Output
*
* @var string
*/
protected $inputEncoding = 'UTF-8';
protected $input_encoding = 'UTF-8';

/**
* The Input file BOM character
* @var string
*/
protected $inputBom;
protected $input_bom;

/**
* The Output file BOM character
* @var string
*/
protected $outputBom = '';
protected $output_bom = '';

/**
* Sets the CSV encoding charset
Expand All @@ -61,7 +61,7 @@ public function setInputEncoding($str)
if (empty($str)) {
throw new InvalidArgumentException('you should use a valid charset');
}
$this->inputEncoding = strtoupper($str);
$this->input_encoding = strtoupper($str);

return $this;
}
Expand Down Expand Up @@ -89,7 +89,7 @@ public function setEncodingFrom($str)
*/
public function getInputEncoding()
{
return $this->inputEncoding;
return $this->input_encoding;
}

/**
Expand All @@ -103,7 +103,7 @@ public function getInputEncoding()
*/
public function getEncodingFrom()
{
return $this->inputEncoding;
return $this->input_encoding;
}

/**
Expand All @@ -116,12 +116,12 @@ public function getEncodingFrom()
public function setOutputBOM($str)
{
if (empty($str)) {
$this->outputBom = '';
$this->output_bom = '';

return $this;
}

$this->outputBom = (string) $str;
$this->output_bom = (string) $str;

return $this;
}
Expand All @@ -133,7 +133,7 @@ public function setOutputBOM($str)
*/
public function getOutputBOM()
{
return $this->outputBom;
return $this->output_bom;
}

/**
Expand All @@ -143,7 +143,7 @@ public function getOutputBOM()
*/
public function getInputBOM()
{
if (null === $this->inputBom) {
if (null === $this->input_bom) {
$bom = [
AbstractCsv::BOM_UTF32_BE, AbstractCsv::BOM_UTF32_LE,
AbstractCsv::BOM_UTF16_BE, AbstractCsv::BOM_UTF16_LE, AbstractCsv::BOM_UTF8,
Expand All @@ -156,10 +156,10 @@ public function getInputBOM()
return strpos($line, $sequence) === 0;
});

$this->inputBom = (string) array_shift($res);
$this->input_bom = (string) array_shift($res);
}

return $this->inputBom;
return $this->input_bom;
}

/**
Expand Down Expand Up @@ -196,15 +196,15 @@ public function output($filename = null)
protected function fpassthru()
{
$bom = '';
$inputBom = $this->getInputBOM();
if ($this->outputBom && $inputBom != $this->outputBom) {
$bom = $this->outputBom;
$input_bom = $this->getInputBOM();
if ($this->output_bom && $input_bom != $this->output_bom) {
$bom = $this->output_bom;
}
$csv = $this->getIterator();
$csv->setFlags(SplFileObject::READ_CSV);
$csv->rewind();
if (!empty($bom)) {
$csv->fseek(mb_strlen($inputBom));
$csv->fseek(mb_strlen($input_bom));
}
echo $bom;
$res = $csv->fpassthru();
Expand Down Expand Up @@ -249,19 +249,19 @@ abstract protected function getQueryIterator();
*/
protected function convertToUtf8(Iterator $iterator)
{
if (stripos($this->inputEncoding, 'UTF-8') !== false) {
if (stripos($this->input_encoding, 'UTF-8') !== false) {
return $iterator;
}

$convertCell = function ($value) {
return mb_convert_encoding($value, 'UTF-8', $this->inputEncoding);
$convert_cell = function ($value) {
return mb_convert_encoding($value, 'UTF-8', $this->input_encoding);
};

$convertRow = function (array $row) use ($convertCell) {
return array_map($convertCell, $row);
$convert_row = function (array $row) use ($convert_cell) {
return array_map($convert_cell, $row);
};

return new MapIterator($iterator, $convertRow);
return new MapIterator($iterator, $convert_row);
}

/**
Expand All @@ -282,21 +282,21 @@ public function toHTML($class_attr = 'table-csv-data')
/**
* Transforms a CSV into a XML
*
* @param string $rootName XML root node name
* @param string $rowName XML row node name
* @param string $cellName XML cell node name
* @param string $root_name XML root node name
* @param string $row_name XML row node name
* @param string $cell_name XML cell node name
*
* @return DomDocument
*/
public function toXML($rootName = 'csv', $rowName = 'row', $cellName = 'cell')
public function toXML($root_name = 'csv', $row_name = 'row', $cell_name = 'cell')
{
$doc = new DomDocument('1.0', 'UTF-8');
$root = $doc->createElement($rootName);
$root = $doc->createElement($root_name);
foreach ($this->convertToUtf8($this->getQueryIterator()) as $row) {
$rowElement = $doc->createElement($rowName);
array_walk($row, function ($value) use (&$rowElement, $doc, $cellName) {
$rowElement = $doc->createElement($row_name);
array_walk($row, function ($value) use (&$rowElement, $doc, $cell_name) {
$content = $doc->createTextNode($value);
$cell = $doc->createElement($cellName);
$cell = $doc->createElement($cell_name);
$cell->appendChild($content);
$rowElement->appendChild($cell);
});
Expand Down
Loading

0 comments on commit 86821ef

Please sign in to comment.