Skip to content

Commit

Permalink
Init
Browse files Browse the repository at this point in the history
  • Loading branch information
MAXakaWIZARD committed Apr 29, 2015
0 parents commit 58fe6e9
Show file tree
Hide file tree
Showing 14 changed files with 497 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .coveralls.yml
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
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
vendor
composer.lock
*.phar
build
phpunit.xml
11 changes: 11 additions & 0 deletions .scrutinizer.yml
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"
24 changes: 24 additions & 0 deletions .travis.yml
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
19 changes: 19 additions & 0 deletions LICENSE
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.
31 changes: 31 additions & 0 deletions README.md
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.
24 changes: 24 additions & 0 deletions composer.json
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/"}
}
}
15 changes: 15 additions & 0 deletions phpunit.xml.dist
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>
141 changes: 141 additions & 0 deletions src/Listener.php
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)
{
}
}
37 changes: 37 additions & 0 deletions src/Parser.php
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);
}
}
Loading

0 comments on commit 58fe6e9

Please sign in to comment.