2017 © Pedro Peláez
 

library jsoncollect

Supercharge your JSON using collections

image

jshannon63/jsoncollect

Supercharge your JSON using collections

  • Friday, February 16, 2018
  • by jshannon63
  • Repository
  • 1 Watchers
  • 16 Stars
  • 553 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 5 Versions
  • 27 % Grown

The README.md

Build Status StyleCI Software License, (*1)

Supercharge your JSON using collections in PHP

The JsonCollect package allows you to surround your JSON objects with the power of collection methods. Making it easy to traverse your data with methods like each(), pluck(), where(), tap(), reduce(), search(), filter(), map(), transform() and many others., (*2)

Framework Agnostic., (*3)

100% PHPUnit Test Coverage., (*4)

Heavily dependent on tightenco/collect, Matt Stauffer's split of Laravel's Illuminate Collections., (*5)

See the Laravel documentation here for more on available methods and usage., (*6)

Additionally, this package provides customized getters and setters for accessing keyed data elements. Described in more detail below., (*7)

Installation

composer require jshannon63/jsoncollect  

if installing in the Laravel framework, JsonCollect will depend on the frameworks copy of Illuminate Collections and tightenco/collect will not be required., (*8)

Usage

Supply your data to the JsonCollect constructor. The form of your data can be a JSON String, a stdClass object or an Array. JsonCollect will recursively dive into the deepest depths of your JSON tree and convert everything to collections., (*9)

Injecting your JSON

use Jshannon63\JsonCollect\JsonCollect;


$collection = new JsonCollect($json);  

Note: You can set the recursion depth of JsonCollect by supplying the optional second constructor argument $depth. Default value is 512., (*10)

Working with your JSON collection

JsonCollect provides custom getter and setter methods for your data. Simply call the methods "get" or "set" with the key name appended to the method name to access your data directly to retrieve or to create/update., (*11)

// to retrieve the element with the key "name"
$collection->getname(); 

// will set the value of the element with the key "phone"
$collection->setphone('123-456-7890');  

As mentioned earlier, you should visit the Laravel documentation here for more on the "~100 available methods" and their usage., (*12)

Some fun examples:, (*13)

// send an email to all friends
$collection->getfriends()->each(function ($item, $key) use ($mailer,$subject,$body){
    $mailer->sendmail($item->emailaddress,$subject,$body);
});

// total all your invoices
$total = $collection->getinvoices()->pluck('total')->sum();

// update the sales tax rate for all Kentucky stores
$collection->getstores()->where('state','KY')->transform(function ($item, $key) use ($rate) {
    return $item->settaxrate($rate);
});

Starting from scratch with an empty JsonCollect object

It is not necessary to provide data to JsonCollect if your goal is to build a new collection of JSON data. Simply "new up" an instance of JsonCollect and begin adding data. Notice how we use ArrayAccess to simplify our code, and to show flexibility we used the custom getter to retrieve the address collection for setting the city., (*14)

$collection = new JsonCollect();

$collection['names'] = 'John Doe';

// or if you have multi-level data, you may add another JsonCollect

$collection['address'] = new JsonCollect();
$collection['address']->setstreet('123 Fourth Street');
$collection->getaddress()->setcity('Louisville');
$collection['address']->setstate('KY');
$collection['address']->setzip('40201');

// and we can use the collection method dd() to view the contents...

$collection->dd();

Which generates the following output from the die-and-dump., (*15)

array(2) {
  'names' =>
  string(8) "John Doe"
  'address' =>
  class Jshannon63\JsonCollect\JsonCollect#327 (1) {
    protected $items =>
    array(4) {
      'street' =>
      string(6) "123 Fourth Street"
      'city' =>
      string(4) "Louisville"
      'state' =>
      string(5) "KY"
      'zip' =>
      string(3) "40201"
    }
  }
}

Exporting your JSON when needed

The following export() method will return a complete JSON string representation of your collection's data. Note that export will accept the standard json_encode options., (*16)

$json = $collection->export(JSON_PRETTY_PRINT);

Based on the previous example, this is what we would expect to see from our export., (*17)

{
    "names": "John Doe",
    "address": {
        "street": "123 Fourth Street",
        "city": "Louisville",
        "state": "KY",
        "zip": "40201"
    }
}

Contributing

If you would like to contribute refer to CONTRIBUTING.md, (*18)

The Versions

16/02 2018

dev-master

9999999-dev https://github.com/jshannon63/jsoncollect

Supercharge your JSON using collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php json collection

16/02 2018

v1.2

1.2.0.0 https://github.com/jshannon63/jsoncollect

Supercharge your JSON using collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php json collection

15/12 2017

v1.1

1.1.0.0 https://github.com/jshannon63/jsoncollect

Supercharge your JSON using collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php json collection

15/12 2017

dev-analysis-XlQgDd

dev-analysis-XlQgDd https://github.com/jshannon63/jsoncollect

Supercharge your JSON using collections

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel php json collection

11/12 2017

V1.0

1.0.0.0 https://github.com/jshannon63/jsoncollect

Supercharge your JSON using collections

  Sources   Download

MIT

The Requires

 

The Development Requires

php json collection