Skip to content

Commit

Permalink
Implemented support for splitting per chunk
Browse files Browse the repository at this point in the history
  • Loading branch information
waltertamboer committed Oct 13, 2017
1 parent 870e2fa commit fa11d84
Show file tree
Hide file tree
Showing 8 changed files with 134 additions and 18 deletions.
10 changes: 9 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,18 @@ cache:
- $HOME/.composer/cache

before_script:
- travis_retry wget https://scrutinizer-ci.com/ocular.phar
- travis_retry composer self-update
- travis_retry composer install

script:
- composer validate --strict
- vendor/bin/phpcs --standard=psr2 src/
- vendor/bin/phpunit --coverage-text --coverage-clover=coverage.clover
- vendor/bin/phpunit

after_script:
- php ocular.phar code-coverage:upload --format=php-clover build/clover.xml

notifications:
email: false
irc: "irc.freenode.org#chesszebra"
22 changes: 22 additions & 0 deletions CONDUCT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Contributor Code of Conduct

As contributors and maintainers of this project, and in the interest of fostering an open and welcoming community, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.

We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, religion, or nationality.

Examples of unacceptable behavior by participants include:

* The use of sexualized language or imagery
* Personal attacks
* Trolling or insulting/derogatory comments
* Public or private harassment
* Publishing other's private information, such as physical or electronic addresses, without explicit permission
* Other unethical or unprofessional conduct.

Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. By adopting this Code of Conduct, project maintainers commit themselves to fairly and consistently applying these principles to every aspect of managing this project. Project maintainers who do not follow or enforce the Code of Conduct may be permanently removed from the project team.

This code of conduct applies both within project spaces and in public spaces when an individual is representing the project or its community in a direct capacity. Personal views, beliefs and values of individuals do not necessarily reflect those of the organisation or affiliated individuals and organisations.

Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.

This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.2.0, available at [http://contributor-covenant.org/version/1/2/0/](http://contributor-covenant.org/version/1/2/0/)
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Contributing

All contributions are much appreciated and will be fully credited.
We accept contributions via Pull Requests on [Github](https://github.com/chesszebra).

Your Pull Requests must comply to our contribution guidelines which
can be found on developers.chesszebra.com.
File renamed without changes.
58 changes: 53 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# pgn-splitter

[![Build Status](https://travis-ci.org/chesszebra/pgn-splitter.svg?branch=master)](https://travis-ci.org/chesszebra/pgn-splitter)
[![Latest Version on Packagist][ico-version]][link-packagist]
[![Software License][ico-license]](LICENSE.md)
[![Build Status][ico-travis]][link-travis]
[![Coverage Status][ico-scrutinizer]][link-scrutinizer]
[![Quality Score][ico-code-quality]][link-code-quality]
[![Total Downloads][ico-downloads]][link-downloads]

A PHP library to split PGN files into chunks per game.
A PHP library to split PGN files into chunks per game or per section.

## Installation

Expand All @@ -14,12 +19,55 @@ composer require chesszebra/pgn-splitter

## Usage

Split a stream per game:

```php
$handle = fopen('my-games.pgn', 'r');
use ChessZebra\Chess\Pgn\Splitter;

$stream = fopen('my-games.pgn', 'r');

$splitter = new \ChessZebra\Chess\Pgn\Splitter();
$splitter = new Splitter($stream, Splitter::SPLIT_GAMES);
$splitter->split(function(string $buffer) {
echo $buffer;
});

```

Or split a stream per chunk (tags and moves chunks):

```php
use ChessZebra\Chess\Pgn\Splitter;

$stream = fopen('my-games.pgn', 'r');

$splitter = new Splitter($stream, Splitter::SPLIT_CHUNKS);
$splitter->split(function(string $buffer) {
echo $buffer;
});
```

## Contributing

Please see [CONTRIBUTING](CONTRIBUTING.md) and [CONDUCT](CONDUCT.md) for details.

## Security

If you discover any security related issues, please report them via [HackerOne][link-hackerone].

## License

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

[ico-version]: https://img.shields.io/packagist/v/chesszebra/pgn-splitter.svg?style=flat-square
[ico-license]: https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square
[ico-travis]: https://img.shields.io/travis/chesszebra/pgn-splitter/master.svg?style=flat-square
[ico-scrutinizer]: https://img.shields.io/scrutinizer/coverage/g/chesszebra/pgn-splitter.svg?style=flat-square
[ico-code-quality]: https://img.shields.io/scrutinizer/g/chesszebra/pgn-splitter.svg?style=flat-square
[ico-downloads]: https://img.shields.io/packagist/dt/chesszebra/pgn-splitter.svg?style=flat-square

[link-packagist]: https://packagist.org/packages/chesszebra/pgn-splitter
[link-travis]: https://travis-ci.org/chesszebra/pgn-splitter
[link-scrutinizer]: https://scrutinizer-ci.com/g/chesszebra/pgn-splitter/code-structure
[link-code-quality]: https://scrutinizer-ci.com/g/chesszebra/pgn-splitter
[link-downloads]: https://packagist.org/packages/chesszebra/pgn-splitter
[link-contributors]: ../../contributors
[link-hackerone]: https://hackerone.com/chesszebra
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "chesszebra/pgn-splitter",
"description": "A PHP library to split PGN files into chunks per game.",
"description": "A PHP library to split PGN files into chunks per game or per section.",
"license": "MIT",
"type": "library",
"require": {
Expand Down
27 changes: 22 additions & 5 deletions src/Splitter.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

final class Splitter
{
const SPLIT_GAMES = 0;
const SPLIT_CHUNKS = 1;

const STATE_TAGS = 0;
const STATE_MOVES = 1;

Expand All @@ -23,25 +26,34 @@ final class Splitter
*/
private $stream;

/**
* The mode to split the stream on.
*
* @var int
*/
private $mode;

/**
* Initializes a new instance of this class.
*
* @param resource $stream The stream to split.
* @throws InvalidStreamException
* @param int $mode The mode to split on.
* @throws InvalidStreamException Thrown when an invalid stream is provided.
*/
public function __construct($stream)
public function __construct($stream, int $mode = self::SPLIT_GAMES)
{
if (!is_resource($stream)) {
throw new InvalidStreamException('The provided stream is not a valid resource.');
}

$this->stream = $stream;
$this->mode = $mode;
}

/**
* Splits the stream into loose pgn files which stored in the given directory.
* Splits the stream into loose chunks and provides them to the given callback.
*
* @param callable $callback The callback that is called for each splitted file.
* @param callable $callback The callback that is called for each found chunk.
* @return int Returns the amount of chunks found in the stream.
*/
public function split(callable $callback): int
Expand All @@ -64,12 +76,17 @@ public function split(callable $callback): int
$buffer .= $line;

if ($line[0] !== '[') {
if ($this->mode === self::SPLIT_CHUNKS) {
$result++;
$callback($buffer);
$buffer = '';
}

$state = self::STATE_MOVES;
}
} elseif ($state === self::STATE_MOVES && $line === "\n") {
$result++;
$callback($buffer);

$buffer = '';

$state = self::STATE_TAGS;
Expand Down
26 changes: 20 additions & 6 deletions tests/SplitterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@

final class SplitterTest extends TestCase
{
private function createStream(string $content)
{
$stream = fopen('php://memory','r+');

fwrite($stream, $content);

rewind($stream);

return $stream;
}

/**
* @expectedException \ChessZebra\Chess\Pgn\Exception\InvalidStreamException
* @expectedExceptionCode 0
Expand Down Expand Up @@ -86,14 +97,17 @@ public function testSplitWithTagsAndGame()
static::assertEquals(1, $result);
}

private function createStream(string $content)
public function testSplitPerChunk()
{
$stream = fopen('php://memory','r+');

fwrite($stream, $content);
// Arrange
$stream = $this->createStream("[A]\n[B]\n\n*");
$splitter = new Splitter($stream, Splitter::SPLIT_CHUNKS);
$callback = function(string $buffer) { };

rewind($stream);
// Act
$result = $splitter->split($callback);

return $stream;
// Assert
static::assertEquals(2, $result);
}
}

0 comments on commit fa11d84

Please sign in to comment.