Skip to content

Skipping lines #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ You can:
- register a pre flight check to determine whether or not a row has to be parsed
- register a callback to handle each line
- register a chopping map which transforms each row into an assiciative array
- register a number of lines to skip at the top of the file


## Usage ##
Expand Down Expand Up @@ -76,11 +77,35 @@ This example ignores any line which md5 sum is `f23f81318ef24f1ba4df4781d79b7849

### Registering a callback ###

Finally you can register a callback which is applied to each parsed line and allows you to process it.
You can register a callback which is applied to each parsed line and allows you to process it.
The closure gets the parsed line as an array and it is expected to return an array of the same format.

$parser->setCallback(function(array $currentLine) {
$currentLine['team'] = ucwords(strtolower($currentLine['team']));
return $currentLine;
}
);

### Registering lines to skip ###

Finally you, can register a certain number of lines to skip from the top of the parsed file. Just in case
the first lines introduce non-interesting information, like titles for the data or headers. Of course, you must do this before parsing.

$parser->setLinesToSkip(2);
$parser->parse();

For a file like this:

```
Name Score
______________________________ _____
Jules 8
Fer 10
Albert 5
...
```

To unset the skips:

$parser->setLinesToSkip(0);

22 changes: 22 additions & 0 deletions library/Fanatique/Parser/FixedLengthFileParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class FixedLengthFileParser implements ParserInterface
*/
protected $content = array();

/** @var int $linesToSkip Amount of lines to skip */
protected $linesToSkip = 0;

/**
* Expects an array with n arrays containing :
* 'field_name', 'start', 'length'
Expand Down Expand Up @@ -123,6 +126,17 @@ public function getContent()
return $this->content;
}

/**
* Sets a certain amount of lines to skip at the top of the file
*
* @param int $lines Amount of lines to skip
* @return void
**/
public function setLinesToSkip(int $lines = 0)
{
$this->linesToSkip = $lines;
}

/**
* Main method for parsing.
*
Expand All @@ -147,6 +161,14 @@ public function parse()
//Parse file line by line
$this->content = array();
$filePointer = fopen($this->file, "r");

// skip as many lines as setted
$i = 0;
while ($i < $this->linesToSkip && !feof($filePointer)) {
fgets($filePointer, 4096);
$i++;
}

while (!feof($filePointer)) {
$buffer = fgets($filePointer, 4096);

Expand Down