From fa11d84a953543d737afbe8d624e0970950c4844 Mon Sep 17 00:00:00 2001 From: Walter Tamboer Date: Fri, 13 Oct 2017 13:09:06 +0200 Subject: [PATCH] Implemented support for splitting per chunk --- .travis.yml | 10 +++++++- CONDUCT.md | 22 ++++++++++++++++ CONTRIBUTING.md | 7 +++++ LICENSE => LICENSE.md | 0 README.md | 58 ++++++++++++++++++++++++++++++++++++++---- composer.json | 2 +- src/Splitter.php | 27 ++++++++++++++++---- tests/SplitterTest.php | 26 ++++++++++++++----- 8 files changed, 134 insertions(+), 18 deletions(-) create mode 100644 CONDUCT.md create mode 100644 CONTRIBUTING.md rename LICENSE => LICENSE.md (100%) diff --git a/.travis.yml b/.travis.yml index a141b87..8648ebc 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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" diff --git a/CONDUCT.md b/CONDUCT.md new file mode 100644 index 0000000..6ff94ca --- /dev/null +++ b/CONDUCT.md @@ -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/) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..35e0a56 --- /dev/null +++ b/CONTRIBUTING.md @@ -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. diff --git a/LICENSE b/LICENSE.md similarity index 100% rename from LICENSE rename to LICENSE.md diff --git a/README.md b/README.md index c233e41..15860e8 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/composer.json b/composer.json index c830b0a..38e29d7 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Splitter.php b/src/Splitter.php index 6bf536e..aff96dc 100644 --- a/src/Splitter.php +++ b/src/Splitter.php @@ -13,6 +13,9 @@ final class Splitter { + const SPLIT_GAMES = 0; + const SPLIT_CHUNKS = 1; + const STATE_TAGS = 0; const STATE_MOVES = 1; @@ -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 @@ -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; diff --git a/tests/SplitterTest.php b/tests/SplitterTest.php index cd2ccfc..30cf73c 100644 --- a/tests/SplitterTest.php +++ b/tests/SplitterTest.php @@ -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 @@ -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); } }