forked from studiofrenetic/laravel-formatter
-
Notifications
You must be signed in to change notification settings - Fork 95
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- 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
- Loading branch information
Showing
22 changed files
with
658 additions
and
546 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 |
---|---|---|
@@ -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" | ||
} |
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
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,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); | ||
} | ||
|
||
} |
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,93 @@ | ||
<?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 | ||
* @return Formatter | ||
*/ | ||
public static function make($data, $type) | ||
{ | ||
if (in_array($type, self::$supportedTypes)) { | ||
$parser = null; | ||
switch ($type) { | ||
case self::CSV: | ||
$parser = new CsvParser($data); | ||
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); | ||
} | ||
} |
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,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']; | ||
} | ||
|
||
} |
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,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; | ||
} | ||
} |
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,47 @@ | ||
<?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) | ||
{ | ||
if (is_string($data)) { | ||
$this->csv = Reader::createFromString($data); | ||
} 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; | ||
} | ||
} |
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,18 @@ | ||
<?php namespace SoapBox\Formatter\Parsers; | ||
|
||
class JsonParser extends Parser | ||
{ | ||
|
||
private $json; | ||
|
||
public function __construct($data) | ||
{ | ||
$this->json = json_decode(trim($data), true); | ||
} | ||
|
||
public function toArray() | ||
{ | ||
return $this->json; | ||
} | ||
|
||
} |
Oops, something went wrong.