kaloa/filesystem
Install
Via Composer:, (*1)
~~~ bash
$ composer require kaloa/filesystem, (*2)
## Requirements
The following PHP versions are supported:
- PHP 5.3
- PHP 5.4
- PHP 5.5
- PHP 5.6
- PHP 7.0
- PHP 7.1
- HHVM
## Documentation
### CsvReader
#### Goals
- Provide an interface to read CSV data from streams into associative arrays.
#### Usage
Read all CSV rows from a stream into a numeric array. This is also a general usage example.
~~~ php
use Kaloa\Filesystem\CsvReader;
$stream = fopen(__DIR__ . '/file.csv', 'rb');
$csvReader = new CsvReader($stream);
$data = $csvReader->fetchAll();
fclose($stream);
Read all CSV rows from a stream into an associative array. The first row from the input will be used as keys., (*3)
~~~ php
$data = $csvReader->fetchAllAssoc();, (*4)
If the file doesn't contain a row with keys, keys can be provided manually. The first row will be seen as regular data.
~~~ php
$data = $csvReader->fetchAllAssoc(array('id', 'title', 'date_added'));
Thereās also a streaming mode available., (*5)
~~~ php
while ($row = $csvReader->fetch()) {
// ...
}, (*6)
Streaming works with associative arrays, too. Here, the first call to `fetchAssoc` will transparently read the first two rows from the input to read both the keys and the first data row.
~~~ php
while ($row = $csvReader->fetchAssoc()) {
// ...
}
Respectively:, (*7)
~~~ php
while ($row = $csvReader->fetchAssoc(array('id', 'title', 'date_added'))) {
// ...
}, (*8)
The reader class is intended to always return UTF-8 data. Differing CSV input encodings will be converted automatically if the input encoding is specified in the constructor.
~~~ php
$csvReader = new CsvReader($iso88591Stream, 'ISO-8859-1');
Thereās also support for non-standard delimiter, enclosure and escape characters., (*9)
~~~ php
$csvReader = new CsvReader($stream, 'UTF-8', ':', '|', '%');, (*10)
#### Further notes
- The `fetch` and `fetchAll` methods accept rows with varying numbers of fields in the same stream. The `fetchAssoc` and `fetchAllAssoc` methods will throw an exception if the number of fields in a row differs from the number of keys.
- It is not possible to change the names of the keys while iterating over input data with `fetchAssoc`. The reader always uses the keys from the first call to `fetchAssoc`.
- Calls to different `fetch*` methods must not be mixed. Currently, the code doesnāt prevent this, but itās very likely that such functionality will be added in a future release.
#### Recipes
For usage with the reader, PHP strings can be converted to streams using the [data protocol](http://php.net/manual/en/wrappers.data.php).
~~~ php
$csvString = <<<'CSV'
"Col a","Col b"
"value 1a","value 1b"
"value 2a","value 2b"
CSV;
$dataUri = 'data://text/plain;base64,' . base64_encode($csvString);
$stream = fopen($dataUri, 'rb');
$reader = new CsvReader($stream);
PathHelper
Goals
- ā
realpath for non-existing filesā is the general idea.
- Resolve relative components (
. and ..) of āvirtualā filesystem paths which donāt point to existing files or directories.
- Generate a normalized representation for such paths.
- The code has to be able to work with both absolute and relative paths on multiple systems.
- It has to be able to deal with absolute paths on Windows. (A path like
c:\foo is to be considered absolute on Windows but relative on Linux. c:\foo is a valid filename on Linux.)
Usage
~~~ php
use Kaloa\Filesystem\PathHelper;, (*11)
$pathHelper = new PathHelper();, (*12)
$pathHelper->normalize('./dir1/dir2/dir3/dir4/../../../'); // "dir1", (*13)
## Testing
(Tools are not included in this package.)
~~~ bash
$ phpunit
Further quality assurance:, (*14)
~~~ bash
$ phpcs --standard=PSR2 ./src
$ phpmd ./src text codesize,design,naming
~~~, (*15)
Credits
License
The package is published under the MIT License. See LICENSE for full license info., (*16)