Skip to content

Commit

Permalink
internals improved
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Aug 27, 2014
1 parent 85f17cf commit dce2d0f
Show file tree
Hide file tree
Showing 5 changed files with 160 additions and 160 deletions.
48 changes: 24 additions & 24 deletions src/AbstractCsv.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,28 @@ public function __construct($path, $open_mode = 'r+')
$this->initStreamFilter($this->path);
}

/**
* Return a normalize path which could be a SplFileObject
* or a string path
*
* @param object|string $path the filepath
*
* @return \SplFileObject|string
*/
protected function normalizePath($path)
{
if ($path instanceof SplFileObject) {
return $path;
} elseif ($path instanceof SplFileInfo) {
return $path->getPath().'/'.$path->getBasename();
}

$path = (string) $path;
$path = trim($path);

return $path;
}

/**
* The destructor
*/
Expand Down Expand Up @@ -114,8 +136,8 @@ public function __destruct()
* ?>
* ```
*
* @param \SplFileInfo|\SplFileObject|object|string $path file path
* @param string $open_mode the file open mode flag
* @param object|string $path file path
* @param string $open_mode the file open mode flag
*
* @return static
*
Expand Down Expand Up @@ -184,28 +206,6 @@ public static function createFromString($str)
return static::createFromFileObject($obj);
}

/**
* Return a normalize path which could be a SplFileObject
* or a string path
*
* @param object|string $path the filepath
*
* @return \SplFileObject|string
*/
protected function normalizePath($path)
{
if ($path instanceof SplFileObject) {
return $path;
} elseif ($path instanceof SplFileInfo) {
return $path->getPath().'/'.$path->getBasename();
}

$path = (string) $path;
$path = trim($path);

return $path;
}

/**
* Create a {@link AbstractCsv} instance from another {@link AbstractCsv} object
*
Expand Down
14 changes: 7 additions & 7 deletions src/Config/Controls.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ trait Controls
*/
protected $flags = SplFileObject::READ_CSV;

/**
* return a SplFileOjbect
*
* @return \SplFileOjbect
*/
abstract public function getIterator();

/**
* set the field delimeter
*
Expand Down Expand Up @@ -137,13 +144,6 @@ public function detectDelimiterList($nb_rows = 1, array $delimiters = [])
return array_keys(array_filter($res));
}

/**
* return a SplFileOjbect
*
* @return \SplFileOjbect
*/
abstract public function getIterator();

/**
* set the field enclosure
*
Expand Down
30 changes: 15 additions & 15 deletions src/Config/StreamFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -163,21 +163,6 @@ public function getStreamFilterMode()
return $this->stream_filter_mode;
}

/**
* Sanitize the stream filter name
*
* @param string $filter_name the stream filter name
*
* @return string
*/
protected function sanitizeStreamFilter($filter_name)
{
$this->assertStreamable();
$filter_name = (string) $filter_name;

return trim($filter_name);
}

/**
* append a stream filter
*
Expand Down Expand Up @@ -208,6 +193,21 @@ public function prependStreamFilter($filter_name)
return $this;
}

/**
* Sanitize the stream filter name
*
* @param string $filter_name the stream filter name
*
* @return string
*/
protected function sanitizeStreamFilter($filter_name)
{
$this->assertStreamable();
$filter_name = (string) $filter_name;

return trim($filter_name);
}

/**
* Detect if the stream filter is already present
*
Expand Down
48 changes: 24 additions & 24 deletions src/Reader.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,6 @@ class Reader extends AbstractCsv
*/
protected $stream_filter_mode = STREAM_FILTER_READ;

/**
* Intelligent Array Combine
*
* add or remove values from the $value array to
* match array $keys length before using PHP array_combine function
*
* @param array $keys
* @param array $value
*
* @return array
*/
protected static function combineArray(array $keys, array $value)
{
$nbKeys = count($keys);
$diff = $nbKeys - count($value);
if ($diff > 0) {
$value = array_merge($value, array_fill(0, $diff, null));
} elseif ($diff < 0) {
$value = array_slice($value, 0, $nbKeys);
}

return array_combine($keys, $value);
}

/**
* Return a Filtered Iterator
*
Expand Down Expand Up @@ -177,6 +153,30 @@ public function fetchAssoc(array $keys, callable $callable = null)
return iterator_to_array($iterator, false);
}

/**
* Intelligent Array Combine
*
* add or remove values from the $value array to
* match array $keys length before using PHP array_combine function
*
* @param array $keys
* @param array $value
*
* @return array
*/
protected static function combineArray(array $keys, array $value)
{
$nbKeys = count($keys);
$diff = $nbKeys - count($value);
if ($diff > 0) {
$value = array_merge($value, array_fill(0, $diff, null));
} elseif ($diff < 0) {
$value = array_slice($value, 0, $nbKeys);
}

return array_combine($keys, $value);
}

/**
* Return a single column from the CSV data
*
Expand Down
Loading

0 comments on commit dce2d0f

Please sign in to comment.