php-extended / php-json-object
A php helper to manipulate json objects
Requires
- php: >=7.1
- php-extended/php-email-address-object: ^3.4
- php-extended/php-ensurer-object: ^3
- php-extended/php-http-message-factory-psr17: ^3
- php-extended/php-ip-object: ^4.3
- php-extended/php-mac-object: ^3.3
- php-extended/php-uuid-object: ^3.3
- php-extended/php-version-object: ^4
- php-extended/polyfill-php80-stringable: >=1
- psr/http-factory: ^1
Requires (Dev)
- dev-master
- 5.4.7
- 5.4.6
- 5.4.5
- 5.4.4
- 5.4.3
- 5.4.2
- 5.4.1
- 5.4.0
- 5.3.5
- 5.3.4
- 5.3.3
- 5.3.2
- 5.3.1
- 5.3.0
- 5.2.17
- 5.2.16
- 5.2.15
- 5.2.14
- 5.2.13
- 5.2.12
- 5.2.11
- 5.2.10
- 5.2.9
- 5.2.8
- 5.2.7
- 5.2.6
- 5.2.5
- 5.2.4
- 5.2.3
- 5.2.2
- 5.2.1
- 5.2.0
- 5.1.5
- 5.1.4
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.3
- 5.0.2
- 5.0.1
- 5.0.0
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.1.0
- 4.0.0
- 3.14.0
- 3.13.0
- 3.12.0
- 3.11.0
- 3.10.1
- 3.10.0
- 3.9.2
- 3.9.1
- 3.9.0
- 3.8.1
- 3.8.0
- 3.7.1
- 3.7.0
- 3.6.0
- 3.5.1
- 3.5.0
- 3.4.2
- 3.4.1
- 3.4.0
- 3.3.0
- 3.2.0
- 3.1.0
- 3.0.0
- 2.4.0
- 2.3.0
- 2.2.0
- 2.1.1
- 2.1.0
- 2.0.2
- 2.0.1
- 2.0.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2022-05-14 17:12:33 UTC
README
A php helper to manipulate json objects
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).