Skip to content

Commit 09f5669

Browse files
authored
Merge pull request #40 from drolean/master
Multiple updates
2 parents 8cdc4a6 + 9b497e0 commit 09f5669

30 files changed

+903
-707
lines changed

.travis.yml

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
language: php
22

33
php:
4-
- 5.4
5-
- 5.5
6-
- 5.6
4+
- 7.0
5+
- 7.1
6+
- 7.2
77

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

12-
script: phpunit
12+
before_script:
13+
- composer self-update
14+
- composer install --no-interaction
1315

16+
script:
17+
- vendor/bin/phpunit

composer.json

+39-29
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,43 @@
11
{
2-
"name": "soapbox/laravel-formatter",
3-
"type": "library",
4-
"description": "A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.",
5-
"keywords": ["laravel", "formatter", "data", "convert", "csv", "xml", "yaml"],
2+
"name": "soapbox/laravel-formatter",
3+
"type": "library",
4+
"description": "A formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others.",
5+
"keywords": ["laravel", "formatter", "data", "convert", "csv", "xml", "yaml"],
66
"homepage": "http://github.com/SoapBox/laravel-formatter",
77
"license": "MIT",
8-
"version": "2.0",
9-
"authors": [
10-
{
11-
"name": "Graham McCarthy",
12-
"email": "[email protected]",
13-
"homepage": "http://grahammccarthy.com"
14-
},
15-
{
16-
"name": "Jaspaul Bola",
17-
"email": "[email protected]",
18-
"homepage": "http://jaspaulbola.com"
19-
}
20-
],
21-
"require": {
22-
"php": ">=5.4.0",
23-
"league/csv": "~6.0",
24-
"mustangostang/spyc": "0.5.*@dev",
25-
"illuminate/support": ">=4.0"
26-
},
27-
"autoload": {
28-
"psr-0": {
29-
"SoapBox\\Formatter": "src/"
30-
}
31-
},
32-
"minimum-stability": "dev"
8+
"version": "3.0",
9+
"authors": [
10+
{
11+
"name": "Graham McCarthy",
12+
"email": "[email protected]",
13+
"homepage": "http://grahammccarthy.com"
14+
},
15+
{
16+
"name": "Jaspaul Bola",
17+
"email": "[email protected]",
18+
"homepage": "http://jaspaulbola.com"
19+
}
20+
],
21+
"require": {
22+
"php" : ">=7.0.10",
23+
"league/csv": "~9.0",
24+
"mustangostang/spyc": "~0.6",
25+
"illuminate/support": "5.5.x"
26+
},
27+
"require-dev": {
28+
"phpunit/phpunit": "^5.7 || ^6.5"
29+
},
30+
"autoload": {
31+
"psr-4": {
32+
"SoapBox\\Formatter\\": "src/"
33+
}
34+
},
35+
"extra": {
36+
"laravel": {
37+
"aliases": {
38+
"Formatter": "SoapBox\\Formatter"
39+
}
40+
}
41+
},
42+
"minimum-stability": "stable"
3343
}

readme.md

+13
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,16 @@
1+
Changelog
2+
================
3+
4+
- Update composer.json
5+
- Upgrade to PSR-4
6+
- add parameter newline, delimiter, enclosure, and escape to export csv
7+
- When converting a XML to an array, convert @attributes to _attribute_
8+
- add parameter encoding and formated to export xml
9+
- 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)
10+
- Add support for laravel 5
11+
- add package discovery for laravel 5
12+
- add support delimiter to a csv
13+
114
Formatter Bundle
215
================
316

src/ArrayHelpers.php

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php namespace SoapBox\Formatter;
2+
3+
use Illuminate\Support\Arr;
4+
5+
class ArrayHelpers
6+
{
7+
public static function isAssociative($array)
8+
{
9+
return array_keys($array) !== range(0, count($array) - 1);
10+
}
11+
12+
public static function dotKeys(array $data)
13+
{
14+
return array_keys(Arr::dot($data));
15+
}
16+
17+
public static function dot(array $data)
18+
{
19+
return Arr::dot($data);
20+
}
21+
22+
public static function set(array &$data, $key, $value)
23+
{
24+
Arr::set($data, $key, $value);
25+
}
26+
27+
}

src/Formatter.php

+94
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php namespace SoapBox\Formatter;
2+
3+
use InvalidArgumentException;
4+
use SoapBox\Formatter\Formatter as Formatter;
5+
use SoapBox\Formatter\Parsers\ArrayParser;
6+
use SoapBox\Formatter\Parsers\CsvParser;
7+
use SoapBox\Formatter\Parsers\JsonParser;
8+
use SoapBox\Formatter\Parsers\XmlParser;
9+
use SoapBox\Formatter\Parsers\YamlParser;
10+
11+
class Formatter
12+
{
13+
/**
14+
* Add class constants that help define input format
15+
*/
16+
const CSV = 'csv';
17+
const JSON = 'json';
18+
const XML = 'xml';
19+
const ARR = 'array';
20+
const YAML = 'yaml';
21+
22+
private static $supportedTypes = [self::CSV, self::JSON, self::XML, self::ARR, self::YAML];
23+
24+
/**
25+
* @var Parser
26+
*/
27+
private $parser;
28+
29+
/**
30+
* Make: Returns an instance of formatter initialized with data and type
31+
*
32+
* @param mixed $data The data that formatter should parse
33+
* @param string $type The type of data formatter is expected to parse
34+
* @param string $delimiter The delimitation of data formatter to csv
35+
* @return Formatter
36+
*/
37+
public static function make($data, $type, $delimiter = null)
38+
{
39+
if (in_array($type, self::$supportedTypes)) {
40+
$parser = null;
41+
switch ($type) {
42+
case self::CSV:
43+
$parser = new CsvParser($data, $delimiter);
44+
break;
45+
case self::JSON:
46+
$parser = new JsonParser($data);
47+
break;
48+
case self::XML:
49+
$parser = new XmlParser($data);
50+
break;
51+
case self::ARR:
52+
$parser = new ArrayParser($data);
53+
break;
54+
case self::YAML:
55+
$parser = new YamlParser($data);
56+
break;
57+
}
58+
return new Formatter($parser, $type);
59+
}
60+
throw new InvalidArgumentException(
61+
'make function only accepts [csv, json, xml, array] for $type but ' . $type . ' was provided.'
62+
);
63+
}
64+
65+
private function __construct($parser)
66+
{
67+
$this->parser = $parser;
68+
}
69+
70+
public function toJson()
71+
{
72+
return $this->parser->toJson();
73+
}
74+
75+
public function toArray()
76+
{
77+
return $this->parser->toArray();
78+
}
79+
80+
public function toYaml()
81+
{
82+
return $this->parser->toYaml();
83+
}
84+
85+
public function toXml($baseNode = 'xml', $encoding = 'utf-8', $formated = false)
86+
{
87+
return $this->parser->toXml($baseNode, $encoding, $formated);
88+
}
89+
90+
public function toCsv($newline = "\n", $delimiter = ",", $enclosure = '"', $escape = "\\")
91+
{
92+
return $this->parser->toCsv($newline, $delimiter, $enclosure, $escape);
93+
}
94+
}

src/FormatterServiceProvider.php

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?php namespace SoapBox\Formatter;
2+
3+
use Illuminate\Support\ServiceProvider;
4+
//use Whoops\Exception\Formatter as Formatter;
5+
//use PhpSpec\Console\Formatter as Formatter;
6+
//use phpDocumentor\Reflection\DocBlock\Tags\Formatter as Formatter;
7+
//use Mdanter\Ecc\Serializer\PublicKey\Der\Formatter as Formatter;
8+
//use Mdanter\Ecc\Serializer\Signature\Der\Formatter as Formatter;
9+
//use Psy\Formatter\Formatter as Formatter;
10+
use SoapBox\Formatter\Formatter as Formatter;
11+
//use Whoops\Exception\Formatter as Formatter;
12+
//use PhpSpec\Console\Formatter as Formatter;
13+
//use phpDocumentor\Reflection\DocBlock\Tags\Formatter as Formatter;
14+
//use Mdanter\Ecc\Serializer\PublicKey\Der\Formatter as Formatter;
15+
//use Mdanter\Ecc\Serializer\Signature\Der\Formatter as Formatter;
16+
//use Psy\Formatter\Formatter as Formatter;
17+
use SoapBox\Formatter\Formatter as Formatter;
18+
19+
/**
20+
* Used to register Authroize with service providers, mainly for Laravel.
21+
*/
22+
class FormatterServiceProvider extends ServiceProvider
23+
{
24+
/**
25+
* Indicates if loading of the provider is deferred.
26+
*
27+
* @var bool
28+
*/
29+
protected $defer = false;
30+
31+
/**
32+
* Bootstrap the application events.
33+
*
34+
* @return void
35+
*/
36+
public function boot()
37+
{
38+
$this->package('soapbox/laravel-formatter');
39+
}
40+
41+
/**
42+
* Register the service provider.
43+
*
44+
* @return void
45+
*/
46+
public function register()
47+
{
48+
$this->app['formatter'] = $this->app->share(function ($app) {
49+
return new Formatter;
50+
});
51+
}
52+
53+
/**
54+
* Get the services provided by the provider.
55+
*
56+
* @return array
57+
*/
58+
public function provides()
59+
{
60+
return ['formatter'];
61+
}
62+
63+
}

src/Parsers/ArrayParser.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php namespace SoapBox\Formatter\Parsers;
2+
3+
use InvalidArgumentException;
4+
5+
class ArrayParser extends Parser
6+
{
7+
8+
private $array;
9+
10+
public function __construct($data)
11+
{
12+
if (is_string($data)) {
13+
$data = unserialize($data);
14+
}
15+
16+
if (is_array($data) || is_object($data)) {
17+
$this->array = (array) $data;
18+
} else {
19+
throw new InvalidArgumentException(
20+
'ArrayParser only accepts (optionally serialized) [object, array] for $data.'
21+
);
22+
}
23+
}
24+
25+
public function toArray()
26+
{
27+
return $this->array;
28+
}
29+
}

src/Parsers/CsvParser.php

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php namespace SoapBox\Formatter\Parsers;
2+
3+
use InvalidArgumentException;
4+
use League\Csv\Reader;
5+
use SoapBox\Formatter\ArrayHelpers;
6+
7+
class CsvParser extends Parser
8+
{
9+
private $csv;
10+
11+
public function __construct($data, $delimiter = null)
12+
{
13+
if (is_string($data)) {
14+
$this->csv = Reader::createFromString($data);
15+
if ($delimiter) {
16+
$this->csv->setDelimiter($delimiter);
17+
}
18+
$this->csv->setEnclosure('|');
19+
} else {
20+
throw new InvalidArgumentException(
21+
'CsvParser only accepts (string) [csv] for $data.'
22+
);
23+
}
24+
}
25+
26+
public function toArray()
27+
{
28+
$temp = $this->csv->jsonSerialize();
29+
30+
$headings = $temp[0];
31+
$result = $headings;
32+
33+
if (count($temp) > 1) {
34+
$result = [];
35+
for ($i = 1; $i < count($temp); ++$i) {
36+
$row = [];
37+
for ($j = 0; $j < count($headings); ++$j) {
38+
$row[$headings[$j]] = $temp[$i][$j];
39+
}
40+
$expanded = [];
41+
foreach ($row as $key => $value) {
42+
ArrayHelpers::set($expanded, $key, $value);
43+
}
44+
$result[] = $expanded;
45+
}
46+
}
47+
48+
return $result;
49+
}
50+
}

0 commit comments

Comments
 (0)