Skip to content

Commit

Permalink
Merge pull request #13 from SoapBox/2.0
Browse files Browse the repository at this point in the history
Laravel Formatter 2.0
  • Loading branch information
Jaspaul committed Oct 20, 2014
2 parents 035ebef + cc31318 commit 9149adc
Show file tree
Hide file tree
Showing 45 changed files with 853 additions and 3,996 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
composer.phar
composer.lock
.DS_Store
notes
3 changes: 2 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
language: php

php:
- 5.3
- 5.4
- 5.5
- 5.6

before_script:
- curl -s http://getcomposer.org/installer | php
Expand Down
27 changes: 12 additions & 15 deletions composer.json
Original file line number Diff line number Diff line change
@@ -1,34 +1,31 @@
{
"name": "soapbox/laravel-formatter",
"type": "library",
"description": "A Laravel 4 formatting library that converts data output between XML, CSV, JSON, TXT, YAML and a few others. ",
"keywords": ["laravel", "formatter", "data","convert","csv", "xml", "yaml"],
"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": "1.4",
"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.3.0",
"illuminate/support": ">=4.0",
"illuminate/foundation": ">=4.0",
"illuminate/config": ">=4.0",
"illuminate/session": ">=4.0",
"illuminate/filesystem": ">=4.0",
"illuminate/view": ">=4.0"
},
"require-dev": {
"orchestra/testbench": "2.1.*",
"mockery/mockery": "dev-master"
"php": ">=5.4.0",
"league/csv": "~6.0",
"mustangostang/spyc": "0.5.*@dev",
"illuminate/support": ">=4.0"
},
"autoload": {
"psr-0": {
"Spyc": "src/spyc/",
"SoapBox\\Formatter": "src/"
}
},
Expand Down
121 changes: 74 additions & 47 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,96 @@ Formatter Bundle

[![Build Status](https://travis-ci.org/SoapBox/laravel-formatter.svg?branch=master)](https://travis-ci.org/SoapBox/laravel-formatter)

A Laravel 4 Formatter Package based on the work done by @dberry37388 with FuelPHP's Formatter class.
A formatter package that will help you to easily convert between various formats such as XML, JSON, CSV, etc...

This package will help you to easily convert between various formats such as XML, JSON, CSV, etc...
# Goals
The goals of this library are to allow the transfomation of data formats from one type to another.
See Parsers and Formats to see supported input / output formats.

# Installation

Installation
------------
Through command line:

Begin by installing this package through Composer. Edit your project's `composer.json` file to require `SoapBox/laravel-formatter`.
```bash
composer require soapbox/laravel-formatter
```

Through composer.json:

"require": {
"soapbox/laravel-formatter": "dev-master"
}
```json
{
"require": {
"soapbox/laravel-formatter": "2.x"
}
}

Next, update Composer from the Terminal:
```

composer update
## Parsers
All of the following are supported formats that the formatter can read from.
* Array
* CSV
* JSON
* XML
* YAML

Once this operation completes, the final step is to add the service provider. Open `app/config/app.php`, and add a new item to the providers array.
## Formats
All of the following are formats that are supported for output.
* Array
* CSV
* JSON
* XML
* YAML

'SoapBox\Formatter\FormatterServiceProvider'
## General Usage

__Including The Formatter__

Usage
-----
The best way to learn how to use Formatter is to look through the code, where you can familiarize yourself with all of the available methods.
```php
use SoapBox\Formatter\Formatter;
```

###Calling Formatter
Formatter::make($data_to_convert, 'type of data')->to_the_format_you_want();
__Supported Types__

### Available Formats to Convert From
- Json
- Serialized Array
- XML
- CSV
```php
Formatter::JSON; //json
Formatter::CSV; //csv
Formatter::XML; //xml
Formatter::ARR; //array
Formatter::YAML; //yaml
```

### Available Formats to Convert To
- Json
- Serializaed Array
- XML
- CSV
- PHP Array
- PHP Export
- YAML
__Making Your First Formatter(s)__

```php
$formatter = Formatter::make($jsonString, Formatter::JSON);
$formatter = Formatter::make($yamlString, Formatter::YAML);
$formatter = Formatter::make($array, Formatter::ARR);
...
```
$json_string = '{"foo":"bar","baz":"qux"}';
$result = Formatter::make($json_string, 'json')->to_array();
if ( empty(Formatter::$errors) ) {
//show the results
print_r($result);
} else {
// Show the errors
print_r(Formatter::$errors);
return;
}

// Returns
Array
(
[foo] => bar
[baz] => qux
)
__Outputting From Your Formatter__

```php
$csv = $formatter->toCsv();
$json = $formatter->toJson();
$xml = $formatter->toXml();
$array = $formatter->toArray();
$yaml = $formatter->toYaml();
```

## Deprecated Functionality
The following have been deprecated from the library, however you can easily continue using them in your application

__Serialized Array__

```php
$serialized = serialize($formatter->toArray());
```

__PHP Export__

```php
$export = var_export($formatter->toArray());
```

23 changes: 23 additions & 0 deletions src/SoapBox/Formatter/ArrayHelpers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?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);
}

}
14 changes: 0 additions & 14 deletions src/SoapBox/Formatter/Facades/Formatter.php

This file was deleted.

Loading

0 comments on commit 9149adc

Please sign in to comment.