Download the PHP package jshannon63/jsoncollect without Composer
On this page you can find all versions of the php package jshannon63/jsoncollect. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download jshannon63/jsoncollect
More information about jshannon63/jsoncollect
Files in jshannon63/jsoncollect
Package jsoncollect
Short Description Supercharge your JSON using collections
License MIT
Homepage https://github.com/jshannon63/jsoncollect
Informations about the package jsoncollect
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.
Framework Agnostic.
100% PHPUnit Test Coverage.
Heavily dependent on tightenco/collect, Matt Stauffer's split of Laravel's Illuminate Collections.
See the Laravel documentation here for more on available methods and usage.
Additionally, this package provides customized getters and setters for accessing keyed data elements. Described in more detail below.
Installation
if installing in the Laravel framework, JsonCollect will depend on the frameworks copy of Illuminate Collections and tightenco/collect will not be required.
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.
Injecting your JSON
Note: You can set the recursion depth of JsonCollect by supplying the optional second constructor argument $depth. Default value is 512.
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.
As mentioned earlier, you should visit the Laravel documentation here for more on the "~100 available methods" and their usage.
Some fun examples:
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.
Which generates the following output from the die-and-dump.
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.
Based on the previous example, this is what we would expect to see from our export.
Contributing
If you would like to contribute refer to CONTRIBUTING.md