php-extended/php-json-object

This package is abandoned and no longer maintained. The author suggests using the php-extended/php-reifier-object package instead.

A php helper to manipulate json objects


README

A php helper to manipulate json objects

coverage build status

Installation

The installation of this library is made via composer. Download composer.phar from their website. Then add to your composer.json :

	"require": {
		...
		"php-extended/php-json-object": "^5",
		...
	}

Then run php composer.phar update to install this library. The autoloading of all classes of this library is made through composer's autoloader.

Basic Usage

This class is a helper, it is done to be extended. For example, an object that represents a price with the following information:

{
	"price": "199.99",
	"currency": "USD",
	"date": "2017-07-12 21:21:42"
}

This object should be parsed with the native json_decode php function, and then the returning array should be given to the following class :


use PhpExtended\Json\JsonObject;

class ExamplePrice extends JsonObject
{
	
	/**
	 * 
	 * @var float
	 */
	public $_price = null;
	
	/**
	 *
	 * @var string
	 */
	public $_currency = null;
	
	/**
	 *
	 * @var \DateTimeInterface
	 */
	public $_date = null;
	
	public function __construct(array $json, $silent = false)
	{
		// filters all error, status and code attributes for error handling
		$data = parent::__construct($json, $silent);
		
		// then iterates on the remaining elements
		foreach($data as $key => $value)
		{
			switch($key)
			{
				case 'price':
					$this->_price = $this->asFloat($value, $silent);
					break;
				case 'currency':
					$this->_currency = $this->asString($value, $silent);
					break;
				case 'date':
					$this->_date = $this->asDatetime($value, 'Y-m-d H:i:s', $silent);
					break;
				default:
					if(!$silent)
						throw new IllegalArgumentException();
			}
		}
	}
	
}

Then this class may be used with the following code :


$json_array = json_decode($json_string, true);		// important to have arrays
$json_object = new ExamplePrice($json_array, true);	// set to false if you want exceptions on errors
echo $json_object->_currency;		// should echo 'USD'
var_dump($json_object->_price);		// should echo float:199.99

The JsonObject is the base object for all the other objects. This object may also gather status codes if they are provided by the json, on status or code words, and error messages on error or message words. Note that if multiple attributes matches those words, only the last will be taken by this object, depending on the order of the attributes which are given into the json.

The JsonCollection object is made to handle [{ ... }, { ... }] situations.

In the outer object, you need to handle the key of the json object and then, create a JsonCollection object with the class name of the object inside the array.

The JsonCollection object may also gather error messages, if the key of the json that was found contains the word error. Those will not throw the JsonException that is thrown when the key values are not acceptable (i.e. they all must be integers for a collection).

The JsonPagination object is the extension of the JsonCollection. It represents metadata about a pagination, that contains count and bounds, and an inner data set which will be build using the JsonCollection.

The JsonSuccess object may also gather boolean responses from the server if they are provided by the json on success or response words.

The JsonCount object may also gather integer responses from the server if they are provided by the json on count or value words.

The JsonDate object may also gather date responses from the server if they are provided by the json on date or today words.

The JsonTime object may also gather time responses from the server if they are provided by the json on time or now words.

The JsonTimestamp object may also gather timestamp responses from the server if they are provided by the json on datetime, timestamp, date, time or now words.

The JsonDateTime object may also gather date and time responses from the server if they are provided by the json on datetime, timestamp, date, time or now words, and if they are formatted accordingly of any of the constants of format within the \DateTime class.

The JsonMac48 object may also gather mac address (48 bits) responses from the server if they are provided by the json on mac words.

The JsonMac64 object may also gather mac address (64 bits) response from the server if they are provided by the json mac words.

The JsonIpv4 object may also gather ipv4 responses from the server if they are provided by the json on ipv4 words.

The JsonIpv4Network object may also gather ipv4 network responses from the server if they are provided by the json on network words.

The JsonIpv6 object may also gather ipv6 responses from the server if they are provided by the json on ipv6 words.

The JsonIpv6Network object may also gather ipv6 network responses from the server if they are provided by the json on network words.

The JsonEmailAddress object may also gather email address responses from the server if they are provided by the json on email words.

The JsonEmailAddressList object may also gather email address list responses from the server if they are provided by the json on list words.

The JsonMailbox object may also gather mailbox responses from the server if they are provided by the json on mailbox words.

The JsonMailboxList object may also gather mailbox list responses from the server if they are provided by the json on list words.

The JsonMailboxGroup object may also gather mailbox group responses from the server if they are provided by the json group words.

The JsonMailboxGroupList object may also gather mailbox group list responses from the server if they are provided by the json list words.

The JsonMetaRecord object may also gather metadata about records of a database based on their state to rapidly check where update needs to be done.

License

MIT (See license file).