Skip to content

Commit

Permalink
Merge pull request #40 from drolean/master
Browse files Browse the repository at this point in the history
Multiple updates
  • Loading branch information
Jaspaul authored Feb 16, 2018
2 parents 8cdc4a6 + 9b497e0 commit 09f5669
Show file tree
Hide file tree
Showing 30 changed files with 903 additions and 707 deletions.
18 changes: 11 additions & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
language: php

php:
- 5.4
- 5.5
- 5.6
- 7.0
- 7.1
- 7.2

before_script:
- curl -s http://getcomposer.org/installer | php
- php composer.phar install --dev
matrix:
allow_failures:
- php: 7.2

script: phpunit
before_script:
- composer self-update
- composer install --no-interaction

script:
- vendor/bin/phpunit
68 changes: 39 additions & 29 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,43 @@
{
"name": "soapbox/laravel-formatter",
"type": "library",
"description": "A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.",
"keywords": ["laravel", "formatter", "data", "convert", "csv", "xml", "yaml"],
"name": "soapbox/laravel-formatter",
"type": "library",
"description": "A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.",
"keywords": ["laravel", "formatter", "data", "convert", "csv", "xml", "yaml"],
"homepage": "http://github.com/SoapBox/laravel-formatter",
"license": "MIT",
"version": "2.0",
"authors": [
{
"name": "Graham McCarthy",
"email": "[email protected]",
"homepage": "http://grahammccarthy.com"
},
{
"name": "Jaspaul Bola",
"email": "[email protected]",
"homepage": "http://jaspaulbola.com"
}
],
"require": {
"php": ">=5.4.0",
"league/csv": "~6.0",
"mustangostang/spyc": "0.5.*@dev",
"illuminate/support": ">=4.0"
},
"autoload": {
"psr-0": {
"SoapBox\\Formatter": "src/"
}
},
"minimum-stability": "dev"
"version": "3.0",
"authors": [
{
"name": "Graham McCarthy",
"email": "[email protected]",
"homepage": "http://grahammccarthy.com"
},
{
"name": "Jaspaul Bola",
"email": "[email protected]",
"homepage": "http://jaspaulbola.com"
}
],
"require": {
"php" : ">=7.0.10",
"league/csv": "~9.0",
"mustangostang/spyc": "~0.6",
"illuminate/support": "5.5.x"
},
"require-dev": {
"phpunit/phpunit": "^5.7 || ^6.5"
},
"autoload": {
"psr-4": {
"SoapBox\\Formatter\\": "src/"
}
},
"extra": {
"laravel": {
"aliases": {
"Formatter": "SoapBox\\Formatter"
}
}
},
"minimum-stability": "stable"
}
13 changes: 13 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
Changelog
================

- Update composer.json
- Upgrade to PSR-4
- add parameter newline, delimiter, enclosure, and escape to export csv
- When converting a XML to an array, convert @attributes to _attribute_
- add parameter encoding and formated to export xml
- JSON parse fix (Instead of only converting the first level to array, use the associative array parameter with true, so all levels will be decoded to array structure)
- Add support for laravel 5
- add package discovery for laravel 5
- add support delimiter to a csv

Formatter Bundle
================

Expand Down
27 changes: 27 additions & 0 deletions src/ArrayHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php namespace SoapBox\Formatter;

use Illuminate\Support\Arr;

class ArrayHelpers
{
public static function isAssociative($array)
{
return array_keys($array) !== range(0, count($array) - 1);
}

public static function dotKeys(array $data)
{
return array_keys(Arr::dot($data));
}

public static function dot(array $data)
{
return Arr::dot($data);
}

public static function set(array &$data, $key, $value)
{
Arr::set($data, $key, $value);
}

}
94 changes: 94 additions & 0 deletions src/Formatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php namespace SoapBox\Formatter;

use InvalidArgumentException;
use SoapBox\Formatter\Formatter as Formatter;
use SoapBox\Formatter\Parsers\ArrayParser;
use SoapBox\Formatter\Parsers\CsvParser;
use SoapBox\Formatter\Parsers\JsonParser;
use SoapBox\Formatter\Parsers\XmlParser;
use SoapBox\Formatter\Parsers\YamlParser;

class Formatter
{
/**
* Add class constants that help define input format
*/
const CSV = 'csv';
const JSON = 'json';
const XML = 'xml';
const ARR = 'array';
const YAML = 'yaml';

private static $supportedTypes = [self::CSV, self::JSON, self::XML, self::ARR, self::YAML];

/**
* @var Parser
*/
private $parser;

/**
* Make: Returns an instance of formatter initialized with data and type
*
* @param mixed $data The data that formatter should parse
* @param string $type The type of data formatter is expected to parse
* @param string $delimiter The delimitation of data formatter to csv
* @return Formatter
*/
public static function make($data, $type, $delimiter = null)
{
if (in_array($type, self::$supportedTypes)) {
$parser = null;
switch ($type) {
case self::CSV:
$parser = new CsvParser($data, $delimiter);
break;
case self::JSON:
$parser = new JsonParser($data);
break;
case self::XML:
$parser = new XmlParser($data);
break;
case self::ARR:
$parser = new ArrayParser($data);
break;
case self::YAML:
$parser = new YamlParser($data);
break;
}
return new Formatter($parser, $type);
}
throw new InvalidArgumentException(
'make function only accepts [csv, json, xml, array] for $type but ' . $type . ' was provided.'
);
}

private function __construct($parser)
{
$this->parser = $parser;
}

public function toJson()
{
return $this->parser->toJson();
}

public function toArray()
{
return $this->parser->toArray();
}

public function toYaml()
{
return $this->parser->toYaml();
}

public function toXml($baseNode = 'xml', $encoding = 'utf-8', $formated = false)
{
return $this->parser->toXml($baseNode, $encoding, $formated);
}

public function toCsv($newline = "\n", $delimiter = ",", $enclosure = '"', $escape = "\\")
{
return $this->parser->toCsv($newline, $delimiter, $enclosure, $escape);
}
}
63 changes: 63 additions & 0 deletions src/FormatterServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php namespace SoapBox\Formatter;

use Illuminate\Support\ServiceProvider;
//use Whoops\Exception\Formatter as Formatter;
//use PhpSpec\Console\Formatter as Formatter;
//use phpDocumentor\Reflection\DocBlock\Tags\Formatter as Formatter;
//use Mdanter\Ecc\Serializer\PublicKey\Der\Formatter as Formatter;
//use Mdanter\Ecc\Serializer\Signature\Der\Formatter as Formatter;
//use Psy\Formatter\Formatter as Formatter;
use SoapBox\Formatter\Formatter as Formatter;
//use Whoops\Exception\Formatter as Formatter;
//use PhpSpec\Console\Formatter as Formatter;
//use phpDocumentor\Reflection\DocBlock\Tags\Formatter as Formatter;
//use Mdanter\Ecc\Serializer\PublicKey\Der\Formatter as Formatter;
//use Mdanter\Ecc\Serializer\Signature\Der\Formatter as Formatter;
//use Psy\Formatter\Formatter as Formatter;
use SoapBox\Formatter\Formatter as Formatter;

/**
* Used to register Authroize with service providers, mainly for Laravel.
*/
class FormatterServiceProvider extends ServiceProvider
{
/**
* Indicates if loading of the provider is deferred.
*
* @var bool
*/
protected $defer = false;

/**
* Bootstrap the application events.
*
* @return void
*/
public function boot()
{
$this->package('soapbox/laravel-formatter');
}

/**
* Register the service provider.
*
* @return void
*/
public function register()
{
$this->app['formatter'] = $this->app->share(function ($app) {
return new Formatter;
});
}

/**
* Get the services provided by the provider.
*
* @return array
*/
public function provides()
{
return ['formatter'];
}

}
29 changes: 29 additions & 0 deletions src/Parsers/ArrayParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php namespace SoapBox\Formatter\Parsers;

use InvalidArgumentException;

class ArrayParser extends Parser
{

private $array;

public function __construct($data)
{
if (is_string($data)) {
$data = unserialize($data);
}

if (is_array($data) || is_object($data)) {
$this->array = (array) $data;
} else {
throw new InvalidArgumentException(
'ArrayParser only accepts (optionally serialized) [object, array] for $data.'
);
}
}

public function toArray()
{
return $this->array;
}
}
50 changes: 50 additions & 0 deletions src/Parsers/CsvParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php namespace SoapBox\Formatter\Parsers;

use InvalidArgumentException;
use League\Csv\Reader;
use SoapBox\Formatter\ArrayHelpers;

class CsvParser extends Parser
{
private $csv;

public function __construct($data, $delimiter = null)
{
if (is_string($data)) {
$this->csv = Reader::createFromString($data);
if ($delimiter) {
$this->csv->setDelimiter($delimiter);
}
$this->csv->setEnclosure('|');
} else {
throw new InvalidArgumentException(
'CsvParser only accepts (string) [csv] for $data.'
);
}
}

public function toArray()
{
$temp = $this->csv->jsonSerialize();

$headings = $temp[0];
$result = $headings;

if (count($temp) > 1) {
$result = [];
for ($i = 1; $i < count($temp); ++$i) {
$row = [];
for ($j = 0; $j < count($headings); ++$j) {
$row[$headings[$j]] = $temp[$i][$j];
}
$expanded = [];
foreach ($row as $key => $value) {
ArrayHelpers::set($expanded, $key, $value);
}
$result[] = $expanded;
}
}

return $result;
}
}
Loading

0 comments on commit 09f5669

Please sign in to comment.