-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 58fe6e9
Showing
14 changed files
with
497 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
service_name: travis-ci | ||
src_dir: src | ||
coverage_clover: build/logs/clover.xml | ||
json_path: build/logs/coveralls-upload.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
vendor | ||
composer.lock | ||
*.phar | ||
build | ||
phpunit.xml |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
filter: | ||
paths: | ||
- src/* | ||
excluded_paths: | ||
- tests/* | ||
- vendor/* | ||
|
||
tools: | ||
php_code_sniffer: | ||
config: | ||
standard: "PSR2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
language: php | ||
|
||
php: | ||
- 5.3 | ||
- 5.4 | ||
- 5.5 | ||
- 5.6 | ||
- 7.0 | ||
|
||
matrix: | ||
allow_failures: | ||
- php: 7.0 | ||
|
||
before_script: | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then composer install -n ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then composer install --no-dev -n ; fi | ||
|
||
script: | ||
- mkdir -p build/logs | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then phpunit --coverage-clover build/logs/clover.xml ; fi | ||
- if [[ "$TRAVIS_PHP_VERSION" != '5.6' ]]; then phpunit ; fi | ||
|
||
after_script: | ||
- if [[ "$TRAVIS_PHP_VERSION" == '5.6' ]]; then php vendor/bin/coveralls -v ; fi |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
Copyright (c) 2015 Max Grigorian | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is furnished | ||
to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# JsonCollectionParser | ||
[![Build Status](https://api.travis-ci.org/MAXakaWIZARD/JsonCollectionParser.png?branch=master)](https://travis-ci.org/MAXakaWIZARD/JsonCollectionParser) | ||
[![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/MAXakaWIZARD/JsonCollectionParser/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/MAXakaWIZARD/JsonCollectionParser/?branch=master) | ||
[![Code Climate](https://codeclimate.com/github/MAXakaWIZARD/JsonCollectionParser/badges/gpa.svg)](https://codeclimate.com/github/MAXakaWIZARD/JsonCollectionParser) | ||
[![Coverage Status](https://coveralls.io/repos/MAXakaWIZARD/JsonCollectionParser/badge.svg?branch=master)](https://coveralls.io/r/MAXakaWIZARD/JsonCollectionParser?branch=master) | ||
[![SensioLabsInsight](https://insight.sensiolabs.com/projects/801d5faf-e753-4b5c-8a14-5795a1a4d239/mini.png)](https://insight.sensiolabs.com/projects/801d5faf-e753-4b5c-8a14-5795a1a4d239) | ||
|
||
[![GitHub tag](https://img.shields.io/github/tag/MAXakaWIZARD/JsonCollectionParser.svg?label=latest)](https://packagist.org/packages/maxakawizard/json-collection-parser) | ||
[![Packagist](https://img.shields.io/packagist/dt/maxakawizard/json-collection-parser.svg)](https://packagist.org/packages/maxakawizard/json-collection-parser) | ||
[![Minimum PHP Version](http://img.shields.io/badge/php-%3E%3D%205.3-8892BF.svg)](https://php.net/) | ||
[![License](https://img.shields.io/packagist/l/maxakawizard/json-collection-parser.svg)](https://packagist.org/packages/maxakawizard/json-collection-parser) | ||
|
||
Event-based parser for large JSON collections (consumes small amount of memory). | ||
Collection must be an array of objects. | ||
Built on top of [JSON Streaming Parser](https://github.com/salsify/jsonstreamingparser) | ||
|
||
This package is compliant with [PSR-4](http://www.php-fig.org/psr/4/), [PSR-1](http://www.php-fig.org/psr/1/), and [PSR-2](http://www.php-fig.org/psr/2/). | ||
If you notice compliance oversights, please send a patch via pull request. | ||
|
||
## Usage | ||
```php | ||
function processItem (array $item) | ||
{ | ||
print_r($item); | ||
} | ||
|
||
$parser = new \JsonStreamingParser\Parser('/path/to/file.json', 'processItem'); | ||
``` | ||
|
||
## License | ||
This library is released under [MIT](http://www.tldrlegal.com/license/mit-license) license. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
{ | ||
"name": "maxakawizard/json-collection-parser", | ||
"description": "Streaming parser for large JSON files containing array of objects", | ||
"keywords": ["json"], | ||
"homepage": "https://github.com/MAXakaWIZARD/JsonCollectionParser", | ||
"type": "library", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Max Grigorian", | ||
"email": "maxakawizard@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php": ">=5.3.0", | ||
"salsify/json-streaming-parser": "~4.0" | ||
}, | ||
"require-dev": { | ||
"satooshi/php-coveralls": "dev-master" | ||
}, | ||
"autoload": { | ||
"psr-4": {"JsonCollectionParser\\": "src/"} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<phpunit bootstrap="./tests/bootstrap.php"> | ||
<testsuites> | ||
<testsuite name="JsonCollectionParser Test Suite"> | ||
<directory>./tests</directory> | ||
</testsuite> | ||
</testsuites> | ||
<filter> | ||
<whitelist processUncoveredFilesFromWhitelist="true"> | ||
<directory suffix=".php">./src</directory> | ||
<exclude> | ||
<directory suffix=".php">./vendor</directory> | ||
</exclude> | ||
</whitelist> | ||
</filter> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
<?php | ||
namespace JsonCollectionParser; | ||
|
||
class Listener implements \JsonStreamingParser_Listener | ||
{ | ||
/** | ||
* @var array | ||
*/ | ||
protected $stack; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
protected $key; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $keys; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $level; | ||
|
||
/** | ||
* @var integer | ||
*/ | ||
protected $objectLevel; | ||
|
||
/** | ||
* @var array | ||
*/ | ||
protected $objectKeys; | ||
|
||
/** | ||
* @param \Callable $callback the function called when a json object has been fully parsed | ||
*/ | ||
public function __construct($callback) | ||
{ | ||
$this->callback = $callback; | ||
} | ||
|
||
public function start_document() | ||
{ | ||
$this->stack = array(); | ||
$this->key = null; | ||
$this->keys = array(); | ||
$this->objectLevel = 0; | ||
$this->level = 0; | ||
$this->objectKeys = array(); | ||
} | ||
|
||
public function end_document() | ||
{ | ||
$this->stack = array(); | ||
$this->keys = array(); | ||
} | ||
|
||
public function start_object() | ||
{ | ||
$this->objectLevel++; | ||
|
||
$this->start_array_common(); | ||
} | ||
|
||
public function end_object() | ||
{ | ||
$this->end_array_common(); | ||
|
||
$this->objectLevel--; | ||
if ($this->objectLevel === 0) { | ||
$obj = $this->stack[0][0]; | ||
array_shift($this->stack[0]); | ||
call_user_func($this->callback, $obj); | ||
} | ||
} | ||
|
||
public function start_array() | ||
{ | ||
$this->start_array_common(); | ||
} | ||
|
||
public function start_array_common() | ||
{ | ||
$this->level++; | ||
$this->objectKeys[$this->level] = ($this->key) ? $this->key : null; | ||
$this->key = null; | ||
array_push($this->stack, array()); | ||
} | ||
|
||
public function end_array() | ||
{ | ||
$this->end_array_common(); | ||
} | ||
|
||
public function end_array_common() | ||
{ | ||
$obj = array_pop($this->stack); | ||
if (empty($this->stack)) { | ||
// finish | ||
} else { | ||
$parentObj = array_pop($this->stack); | ||
if ($this->objectKeys[$this->level]) { | ||
$parentObj[$this->objectKeys[$this->level]] = $obj; | ||
} else { | ||
array_push($parentObj, $obj); | ||
} | ||
array_push($this->stack, $parentObj); | ||
} | ||
|
||
$this->level--; | ||
} | ||
|
||
/** | ||
* @param $key | ||
*/ | ||
public function key($key) | ||
{ | ||
$this->key = $key; | ||
} | ||
|
||
/** | ||
* @param $value | ||
*/ | ||
public function value($value) | ||
{ | ||
$obj = array_pop($this->stack); | ||
if ($this->key) { | ||
$obj[$this->key] = $value; | ||
$this->key = null; | ||
} else { | ||
array_push($obj, $value); | ||
} | ||
array_push($this->stack, $obj); | ||
} | ||
|
||
public function whitespace($whitespace) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
namespace JsonCollectionParser; | ||
|
||
class Parser | ||
{ | ||
/** | ||
* @param $filePath | ||
* @param $itemCallback | ||
* | ||
* @throws \Exception | ||
*/ | ||
public function parse($filePath, $itemCallback) | ||
{ | ||
if (!is_callable($itemCallback)) { | ||
throw new \Exception("Callback should be callable"); | ||
} | ||
|
||
if (!is_file($filePath)) { | ||
throw new \Exception('File does not exist: ' . $filePath); | ||
} | ||
|
||
$stream = @fopen($filePath, 'r'); | ||
if (false === $stream) { | ||
throw new \Exception('Unable to open file for read: ' . $filePath); | ||
} | ||
|
||
try { | ||
$listener = new Listener($itemCallback); | ||
$parser = new \JsonStreamingParser_Parser($stream, $listener, "\n", true); | ||
$parser->parse(); | ||
} catch (\Exception $e) { | ||
fclose($stream); | ||
throw $e; | ||
} | ||
fclose($stream); | ||
} | ||
} |
Oops, something went wrong.