https://github.com/hhpack/file
File utility library for Hack
https://github.com/hhpack/file
csv field hacklang hhvm
Last synced: about 1 month ago
JSON representation
File utility library for Hack
- Host: GitHub
- URL: https://github.com/hhpack/file
- Owner: hhpack
- License: mit
- Created: 2015-03-20T06:43:21.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2017-10-03T00:51:58.000Z (over 8 years ago)
- Last Synced: 2024-11-18T09:40:42.167Z (about 1 year ago)
- Topics: csv, field, hacklang, hhvm
- Language: Hack
- Homepage:
- Size: 78.1 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
file
======================================================
[](https://packagist.org/packages/hhpack/file)
[](https://travis-ci.org/hhpack/file)
[](https://www.versioneye.com/user/projects/56239db636d0ab0016000be8)
[](https://packagist.org/packages/hhpack/file)
This package is a library for performing a simple to file operations hacklang.
Will provide a lightweight and simple api to the user.
Basic usage
------------------------------------------------------
Read processing of files can be realized by a simple code as follows.
### Reading one line at a time.
```hack
use HHPack\File\FileLineStream;
$lineStream = FileLineStream::fromString('/path/to/text.log');
foreach ($lineStream as $line) {
echo $line->length(), "\n"; //output length
echo $line->value(), "\n"; //output content
};
```
Read the CSV file
------------------------------------------------------
```hack
use HHPack\File\FileLineStream;
use HHPack\File\SeparatedRecordStream;
use HHPack\File\ColumnSpecification;
$spec = new ColumnSpecification(',', '"');
$spec->addColumn(0, 'name');
$spec->addColumn(1, 'description');
$lineStream = FileLineStream::fromString(__DIR__ . '/example.csv');
$csvStream = new SeparatedRecordStream($lineStream, $spec);
foreach ($csvStream as $record) {
echo $record->get('name'), "\n";
echo $record->get('description'), "\n";
}
```
Customizing the reading of the record
------------------------------------------------------
Will create a parser that implements the **ParseSpecification**.
Then use the **ParsedFileReader**, and then apply the parser.
```hack
use HHPack\File\FileLineStream;
use HHPack\File\ParsedChunkStream;
use HHPack\File\ParseSpecification;
final class CustomRecordSpecification implements ParseSpecification>
{
public function parse(Chunk $line) : array
{
return $line->split(',');
}
}
$spec = new CustomRecordSpecification();
$lineStream = FileLineStream::fromString(__DIR__ . '/example.csv');
$csvStream = new ParsedChunkStream($lineStream, $spec);
foreach ($csvStream as $values) {
echo $values[0], "\n";
echo $values[1], "\n";
}
```
Run the test
------------------------------------------------
You can run the test with the following command.
composer install
composer test