dev-master
9999999-dev http://jmsyst.com/libs/serializerExtract information from JSON, StdClass in a fashionable way
BSD-3-Clause
The Requires
- php >=5.3
The Development Requires
by Danilo Cabello
by Rafael Martins
json extract stdclass
Wallogit.com
2017 © Pedro Peláez
Extract information from JSON, StdClass in a fashionable way
If you found this project but think it's incomplete or doesn't meet your needs, try Property Access., (*1)
Imagine that you have a StdClass (possibly created by a json_decode) instance full of attributes you want to retrieve. So you do something like this:, (*2)
$username = $photo->owner->username;
What happens if username is undefined and/or owner?, (*3)
$username = isset($photo) && isset($photo->owner) && isset($photo->owner->username)
? $photo->owner->username
: 'anonymous';
Imagine this kind of logic spread all over your codebase. What a mess!, (*4)
Selector turns the horrible code above into this:, (*5)
$photoSelector = Selector($photo);
$username = $photoSelector->getOne('owner.username', 'anonymous');
You never have to worry again about checking if the StdClass have the properties you need, and as a plus you receive several ways of retrieving data., (*6)
The above example will output:, (*7)
The above example will output:, (*8)
The above example will output:, (*9)
The above example will output:, (*10)
The above example will output:, (*11)
The above example will output:, (*12)
The above example will output:, (*13)
The above example will output:, (*14)
The above example will output:, (*15)
The above example will output:, (*16)
Assuming you have composer.phar installed, it's simple to contribute to Selector, fork, clone your repository and run:, (*17)
cd selector # your clone folder composer.phar install --dev vendor/bin/phpunit tests
And you are ready to write new tests, contributions and sending pull requests. :octocat:, (*18)
Extract information from JSON, StdClass in a fashionable way
BSD-3-Clause
json extract stdclass